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

namespace ShootingHero.Components.Manager
{
	public class BulletManager : DrawableGameComponent
	{
		List<Bullet> bulletList = new List<Bullet> ();
		List<Bullet> addList = new List<Bullet> ();
		List<Bullet> removeList = new List<Bullet> ();

		public BulletManager ( Game game )
			: base ( game )
		{

		}

		public void Add ( Bullet bullet )
		{
			addList.Add ( bullet );
			//bulletList.Add ( bullet );
			//bullet.Initialize ();
		}

		public void Remove ( Bullet bullet )
		{
			removeList.Add ( bullet );
		}

		public override void Initialize ()
		{
			base.Initialize ();
		}

		protected override void LoadContent ()
		{
			base.LoadContent ();
		}

		protected override void UnloadContent ()
		{
			base.UnloadContent ();
		}

		public override void Update ( GameTime gameTime )
		{
			foreach ( Bullet bullet in bulletList )
			{
				bullet.Update ( gameTime );
				if ( ( bullet.Y >= 850 || bullet.Y <= -50 ) || ( bullet.X >= 530 || bullet.X <= -50 ) )
					Remove ( bullet );
			}
			foreach ( Bullet bullet in addList )
			{
				bulletList.Add ( bullet );
				bullet.Initialize ();
			}
			addList.Clear ();
			foreach ( Bullet bullet in removeList )
				bulletList.Remove ( bullet );
			removeList.Clear ();
			base.Update ( gameTime );
		}

		public override void Draw ( GameTime gameTime )
		{
			foreach ( Bullet bullet in bulletList )
				bullet.Draw ( gameTime );
			base.Draw ( gameTime );
		}
	}
}
