using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using ShootingHero.Utilities;
using Microsoft.Xna.Framework.Graphics;

namespace ShootingHero.Components.Manager
{
	public class EffectManager : DrawableGameComponent
	{
		static Texture2D boomTexture, changeTexture;
		List<EffectAnimation> effectList = new List<EffectAnimation> ();

		public EffectManager ( Game game )
			: base ( game )
		{
			if ( boomTexture == null || boomTexture.IsDisposed )
				boomTexture = Game.Content.Load<Texture2D> ( "Effect/boomEffect" );
			if ( changeTexture == null || changeTexture.IsDisposed )
				changeTexture = Game.Content.Load<Texture2D> ( "Effect/changeEffect" );
		}

		public void Add ( float x, float y, bool isBoomEffect )
		{
			effectList.Add ( new EffectAnimation ( isBoomEffect ? boomTexture : changeTexture, x, y, isBoomEffect ? 8 : 5 ) );
		}

		public override void Update ( GameTime gameTime )
		{
			List<EffectAnimation> removeList = new List<EffectAnimation> ();
			foreach ( EffectAnimation animation in effectList )
			{
				animation.Update ( gameTime );
				if ( animation.IsEnded )
					removeList.Add ( animation );
			}

			foreach ( EffectAnimation animation in removeList )
				effectList.Remove ( animation );

			base.Update ( gameTime );
		}

		public override void Draw ( GameTime gameTime )
		{
			SpriteBatch spriteBatch = ( Game as ShootingHero ).SpriteBatch;

			foreach ( EffectAnimation animation in effectList )
				animation.DrawAnimation ( spriteBatch );

			base.Draw ( gameTime );
		}
	}
}
