package  
{
	import controllers.ObjectController;
	import enemies.Creator;
	import enemies.EnemyHelicopter;
	import enemies.EnemyPepelaz;
	import enemies.EnemyTiny;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import levels.Level;
	import levels.Lvl1;
	import levels.Lvl2;
	import levels.Lvl3;
	
	/**
	 * ...
	 * @author Fiodar Paulavets
	 */
	public class World extends Sprite 
	{		
		public static var world: World;	
		
		public var backClouds: ObjectController;
		public var frontClouds: ObjectController;
		public var backBrushes: ObjectController;
		public var frontBrushes: ObjectController;
		public var bulletsList: ObjectController;
		public var enemiesList: ObjectController;
		public var particlesList: ObjectController;
		
		public var hero: Hero;
		
		public var hud: HUD;
		
		private var level: Level;
		
		private var _cloudsTimer: Number = 0;
		private var _brushesTimer: Number = 0;
		private var _enemiesTimer: Number = 0;
		
		private var _showMenu: Boolean = true, start: Boolean = false;
		
		public function World() 
		{			
			world = this; // !!!
			
			var sky: SkyClip = new SkyClip();
				sky.x = sky.width / 2;
				sky.y = sky.height / 2;
			var ground: GroundClip = new GroundClip();
				ground.x = ground.width / 2;
				ground.y = Game.screenHeight - ground.height / 2;
				
			backClouds = new ObjectController();
			frontClouds = new ObjectController();
			for (var i: uint = 0; i < 5; i++)
			{
				var cloud: Cloud = placeCloud(Math.random() > 0.5);
				cloud.x = Math.random() * Game.screenWidth;
			}
			backBrushes = new ObjectController();
			frontBrushes = new ObjectController();
			for (i = 0; i < 20; i++)
			{
				var brush: Brush = placeBrush();
				brush.x = Math.random() * Game.screenWidth;
			}
			
			particlesList = new ObjectController();
			
			hud = new HUD();
			hud.visible = false;
			
			hud.menu.visible = true;
			
			addChild(sky);
			addChild(ground);
			addChild(backClouds.sprite);
			addChild(backBrushes.sprite);
			/*addChild(bulletsList.sprite);
			addChild(enemiesList.sprite);
			addChild(hero);*/
			addChild(particlesList.sprite);
			addChild(frontClouds.sprite);
			addChild(frontBrushes.sprite);
			addChild(hud);
			addChild(hud.menu);
		}
		
		public function Play(): void
		{
			start = true;
		}
		
		private function Start(): void
		{
			level = new Level(0, "");
			hero = new Hero();
			bulletsList = new ObjectController();
			enemiesList = new ObjectController();
			
			addChildAt(bulletsList.sprite, 4);
			addChildAt(enemiesList.sprite, 5);
			addChildAt(hero, 6);
			hud.visible = true;
			hud.hp();
			_showMenu = false;
			hud.menu.visible = false;
		}
		
		private function GameOver(win: Boolean): void
		{
			if (win)
				Sounds.manager.playSound("win");
			
			Creator.pauseWave();
			Creator.destroyWave();
			removeChild(enemiesList.sprite);
			removeChild(bulletsList.sprite);
			removeChild(hero);
			enemiesList = null;
			bulletsList = null;
			hero = null;
			
			hud.GameOver(!win);
			
			hud.visible = false;
			_showMenu = true;
			hud.menu.visible = true;
			hud.menu.alpha = 1;			
			start = false;
		}
		
		public function Update(dt: Number): void
		{
			backClouds.update(dt);
			frontClouds.update(dt);
			backBrushes.update(dt);
			frontBrushes.update(dt);
			particlesList.update(dt);
			
			_cloudsTimer -= dt;
			if (_cloudsTimer <= 0)
			{
				placeCloud();
				if (Math.random() > 0.7)
					placeCloud(true);
				_cloudsTimer = 2;
			}
			_brushesTimer -= dt;
			if (_brushesTimer <= 0)
			{
				placeBrush();
				_brushesTimer = 0.3;
			}
			
			if (_showMenu)
			{
				if (start)
					hud.menu.alpha -= dt * 2;
				if (hud.menu.alpha <= 0)
					Start();
				return;
			}
			hero.Update(dt);
			bulletsList.update(dt);
			enemiesList.update(dt);
			
			/*_enemiesTimer -= dt;
			if (_enemiesTimer <= 0)
			{
				enemiesList.add(new EnemyHelicopter());
				_enemiesTimer = 2;
			}*/
			
			if (enemiesList.objects.length == 0 && Creator.getCount() == 0)
			{
				switch (level.number)
				{
					case 0: level = new Lvl1(); break;
					case 1: level = new Lvl2(); break;
					case 2: level = new Lvl3(); break;
					case 3: GameOver(true); return;
				}
				hud.levelCaption(level.number, level.caption);
			}
			
			if (hero.life <= 0)
				GameOver(false);
		}
		
		private function placeCloud(front: Boolean = false): Cloud
		{
			var cloud: Cloud = new Cloud(front);
			if (!front)
				backClouds.add(cloud);
			else
				frontClouds.add(cloud);
			return cloud;
		}
		
		private function placeBrush(): Brush
		{
			var brush: Brush = new Brush();
			if (brush.front)
				frontBrushes.add(brush);
			else
				backBrushes.add(brush);
			return brush;
		}
		
	}

}