using System;
using System.Collections.Generic;
using System.Text;
using Vortex;
using Vortex.Drawing;

namespace Vortex.Tutorials.GameDemo
{
    class GradientColor
    {
        public float Position;
        public ColorU Color;
    }

    class ColorGradient
    {
        List<GradientColor> Colors;
        public ColorGradient()
        {
            Colors = new List<GradientColor>();
        }

        public void AddColor(float Position, ColorU Color)
        {
            GradientColor GC=new GradientColor();
            GC.Position=Position;
            GC.Color=Color;
            Colors.Add(GC);
        }

        public ColorU GetColor(float Position)
        {
            ColorU c1,c2;
            float p1, p2;
            float eps=0.002f;

            if (Position >= 1.0f)
            {
                return Colors[Colors.Count - 1].Color;
            }

            for (int i = 0; i < Colors.Count; i++)
            {
                if (Math.Abs(Colors[i].Position - Position) < eps)
                {
                    return Colors[i].Color;

                }
            }

            for (int i = 0; i < Colors.Count; i++)
            {
                if (Colors[i].Position > Position && i > 0)
                {
                    c1 = Colors[i - 1].Color;
                    p1 = Colors[i - 1].Position;
                    c2 = Colors[i].Color;
                    p2 = Colors[i].Position;
                    float b = (Position - p1) / (p2 - p1);
                    c1.R = Convert.ToByte(c1.R*(1-b) + c2.R * b);
                    c1.G = Convert.ToByte(c1.G * (1 - b) + c2.G * b);
                    c1.B = Convert.ToByte(c1.B * (1 - b) + c2.B * b);
                    return c1;
                }
            }
            return ColorU.White;
        }
    }

    class FireParticle
    {
        public int SpriteIndex;
        public float InitialLife;
        public float Life=0.0f;
        public Vector2 Position;
        public float Speed;
        public Vector2 Direction;
        public float Angle;
        public float Alpha;
        public float Size;
        public ColorU Color;

        public void Update(GameTime time)
        {
            if (InitialLife > 0)
            {
                Life -= time.FrameTimeMs;
            }
            else
            {
                Life = 0;
            }
        }
    } 
}
