using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Diagnostics;
using ShootingHero.Scene;
using ShootingHero.Components.Items;

namespace ShootingHero.Components.Manager
{
	public class MonsterManager : DrawableGameComponent
	{
		List<Monster> monsterList = new List<Monster> ();
		List<Monster> addList = new List<Monster> ();
		List<Monster> removeList = new List<Monster> ();

		public List<Monster> Container { get { return monsterList; } }

		public MonsterManager ( Game game )
			: base ( game )
		{
		
		}

		public void Add ( Monster monster )
		{
			addList.Add ( monster );
			//monsterList.Add ( monster );
			//monster.Initialize ();
		}

		public void Remove ( Monster monster )
		{
			removeList.Add ( monster );
		}

		public override void Update ( GameTime gameTime )
		{
			foreach ( Monster monster in monsterList )
			{
				monster.Update ( gameTime );
				if ( monster.HealthPoint <= 0 || monster.Y > 900 || monster.Y < -100 )
					Remove ( monster );
			}

			foreach ( Monster monster in addList )
			{
				monsterList.Add ( monster );
				monster.Initialize ();
			}
			addList.Clear ();

			GameScene gameScene = ( Game.Components [ 0 ] as Transitor ).CurrentScene as GameScene;
			foreach ( Monster monster in removeList )
			{
				switch ( ( Game as ShootingHero ).Random.Next () % 10 )
				{
					case 1:
						gameScene.ItemManager.Add ( new Bomb ( Game, monster.X, monster.Y ) );
						break;

					case 5:
						gameScene.ItemManager.Add ( new Medicine ( Game, monster.X, monster.Y ) );
						break;

					case 9:
						gameScene.ItemManager.Add ( new LevelUp ( Game, monster.X, monster.Y ) );
						break;
				}
				monsterList.Remove ( monster );
			}
			removeList.Clear ();
			base.Update ( gameTime );
		}

		public override void Draw ( GameTime gameTime )
		{
			foreach ( Monster monster in monsterList )
				monster.Draw ( gameTime );
			base.Draw ( gameTime );
		}
	}
}
