using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
//using System.Drawing;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using dr = System.Drawing;


namespace PArena
{
    public class VecColor
    {
        public Vector2 v;
        public Color color;
        public VecColor(Vector2 v, Color c)
        {
            this.v = v;
            this.color = c;
        }
    }

    public class Bullet : GameObject
    {
        public bool toRemove = false;
        public bool prepareToRemove = false;

        public List<VecColor> positions;
        protected Texture2D tailTex;
        protected int stride = 1;
        protected float ttl = 20;
        protected Color tailColor = new Color(0, 255, 0, 255);
        protected float tailLength = 600;
        protected ShakeParam shakeParam;

        //public static int maxBullet = 1000;
        public float bulletSpeed = 1;
        public float damage = 1;
        public VecColor prevPos;
        public int TailHeight { get { return tailTex.Height; } }
        public int TailWidth { get { return tailTex.Width; } }
        public Texture2D TailTex { get { return tailTex; } }
        public ShakeParam getShakeParam { get { shakeParam.curtime = shakeParam.maxtime; return shakeParam; } }
        public Cue soundfx = null;
        


        public Bullet(Vector2 pos, Vector2 dir, Texture2D tex, float initSpeed, bool good = true)
            : base(pos, dir, tex, initSpeed)
        {
            tailTex = Cnt.game.tailTexCommon;//Cnt.game.Content.Load<Texture2D>("t2");
            /*
            tailTex = new Texture2D(Cnt.game.graphics.GraphicsDevice, tex.Width, tex.Height, false, SurfaceFormat.Color);
            stride = Math.Min(tex.Width, tex.Height) / 2;

            Color[] colors = new Color[tex.Height * tex.Width];
            for (int i = 0; i < colors.Length; i++)
            {
                colors[i] = Color.White;
            }
            tailTex.SetData<Color>(colors);
            //Stream stream = new FileStream("1.png", FileMode.Create);
            //tailTex.SaveAsPng(stream, tex.Width, tex.Height);
             */
            bulletSpeed = initSpeed;
            shakeParam.ampl = 10;
            shakeParam.curtime = 0;
            shakeParam.maxtime = 0.15f;
            shakeParam.dir = new Vector2(0, 1);
            stride = sprite.Width / 2;
        }

        public override void Update(GameTime gt)
        {
            //if (positions.Count == 0) { toRemove = true; return; }

            ttl -= (float)gt.ElapsedGameTime.TotalSeconds;

            if (ttl < 0) toRemove = true;
            base.Update(gt);
            prevPos = positions.Last();
           
            Vector2 lastdir = Pos - prevPos.v;
            float length = lastdir.Length();
            if (lastdir != Vector2.Zero) lastdir.Normalize();
            else lastdir = Dir;

            Vector2 next = prevPos.v;
            for (int i = 0; i < length / stride; i++)
            {
                next += lastdir * stride;
                if (!prepareToRemove)
                    positions.Add(new VecColor(next, tailColor));
            }

            for (int i = 0; i < positions.Count; i++)
            {

                
                positions[i].color.A = (byte)MathHelper.Clamp(positions[i].color.A - 255 * bulletSpeed / tailLength * (float)gt.ElapsedGameTime.TotalSeconds, 0, 255);
                //positions[i].color.R = (byte)MathHelper.Clamp(positions[i].color.R - 255 * bulletSpeed / tailLength * (float)gt.ElapsedGameTime.TotalSeconds, 0, 255);
                //positions[i].color.G = (byte)MathHelper.Clamp(positions[i].color.G - 255 * bulletSpeed / tailLength * (float)gt.ElapsedGameTime.TotalSeconds, 0, 255);
                //positions[i].color.B = (byte)MathHelper.Clamp(positions[i].color.B - 255 * bulletSpeed / tailLength * (float)gt.ElapsedGameTime.TotalSeconds, 0, 255);
                // if (positions[i].color.R == 0&&positions[i].color.G==0&&positions[i].color.B == 0) positions.RemoveAt(i);
                if (positions[i].color.A < 20) positions.RemoveAt(i);
            }

            //Vector2 screen = new Vector2(Game1.screenWidth , Game1.screenHeight );
            //if (Vector2.Clamp(Pos, Vector2.Zero, screen) != Pos) prepareToRemove = true;
            if (positions.Count == 0) { toRemove = true; return; }
        }

        public override void Draw(SpriteBatch sb)
        {
            foreach (VecColor v in positions)
            {
                sb.Draw(tailTex, v.v - origin, v.color);
            }
            if (!prepareToRemove) sb.Draw(sprite, Pos - origin, Color.White);

        }
        public override void Draw(SpriteBatch sb, Vector2 offset)
        {
            foreach (VecColor v in positions)
            {
                sb.Draw(tailTex, v.v - origin, v.color);
            }
            if (!prepareToRemove) sb.Draw(sprite, Pos - origin+offset, Color.White);
           
        }        
    }
}
