using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace GraphiXNA.ParticleSystems
{
    public class FireParticleSystem : ParticleSystem
    {
        private ParticleEmitter FlameEmitter = null;
        private ParticleEmitter SmokeEmitter = null;
        private bool _EnableFlames = true;
        private bool _EnableSmokes = true;


        public FireParticleSystem(GraphicsDevice graphicsDevice)
            : base(graphicsDevice)
        {
            FlameEmitter = new ParticleEmitter(GraphicsDevice, 2400);
            SmokeEmitter = new ParticleEmitter(GraphicsDevice, 300);

            SetupFlame();
            SetupSmoke();
        }

        private void SetupFlame()
        {
            FlameEmitter.SetTexture(ResourceManager.GetFlameTexture(GraphicsDevice));
            FlameEmitter.Duration = TimeSpan.FromSeconds(2);
            FlameEmitter.DurationRandomness = 1;
            FlameEmitter.MinHorizontalVelocity = 0;
            FlameEmitter.MaxHorizontalVelocity = 1.5f;
            FlameEmitter.MinVerticalVelocity = -1.0f;
            FlameEmitter.MaxVerticalVelocity = 1.0f;
            FlameEmitter.MinColor = new Color(255, 255, 255, 40);
            FlameEmitter.MaxColor = new Color(255, 255, 255, 120);
            FlameEmitter.MinStartSize = 0.5f;
            FlameEmitter.MaxStartSize = 1.0f;

            FlameEmitter.MinEndSize = 1.0f;
            FlameEmitter.MaxEndSize = 4.0f;
            FlameEmitter.Gravity = new Vector3(0, 1.5f, 0);

            FlameEmitter.SourceBlend = Blend.SourceAlpha;
            FlameEmitter.DestinationBlend = Blend.One;
        }
        private void SetupSmoke()
        {
            SmokeEmitter.SetTexture(ResourceManager.GetSmokeTexture(GraphicsDevice));
            SmokeEmitter.Duration = TimeSpan.FromSeconds(5);

            SmokeEmitter.MinHorizontalVelocity = 0;
            SmokeEmitter.MaxHorizontalVelocity = 1.5f;

            SmokeEmitter.MinVerticalVelocity = -1.0f;
            SmokeEmitter.MaxVerticalVelocity = 1.0f;

            SmokeEmitter.Gravity = new Vector3(0, 10.75f, 0);

            SmokeEmitter.EndVelocity = 0.075f;

            SmokeEmitter.MinRotateSpeed = -1;
            SmokeEmitter.MaxRotateSpeed = 1;

            SmokeEmitter.MinStartSize = 0.5f;
            SmokeEmitter.MaxStartSize = 1.0f;

            SmokeEmitter.MinEndSize = 5.0f;
            SmokeEmitter.MaxEndSize = 20.0f;

            //SmokeEmitter.SourceBlend = Blend.One;
            //SmokeEmitter.DestinationBlend = Blend.Zero;
        }

        internal override void InternalDraw()
        {
            if (_EnableFlames)
            {
                FlameEmitter.Draw();
            }

            if (_EnableSmokes)
            {
                SmokeEmitter.Draw();
            }
        }

        

        public ParticleEmitter Flame
        {
            get { return FlameEmitter; }
        }

        public ParticleEmitter Smoke
        {
            get { return SmokeEmitter; }
        }

        public bool EnableFlames
        {
            get { return _EnableFlames; }
            set { _EnableFlames = value; }
        }

        public bool EnableSmokes
        {
            get { return _EnableSmokes; }
            set { _EnableSmokes = value; }
        }
    }
}
