package  
{
	import flash.display.InteractiveObject;
	import net.flashpunk.World;
	import net.flashpunk.FP;
	import net.flashpunk.tweens.misc.VarTween;
	import net.flashpunk.utils.Ease;
	
	/**
	 * ...
	 * @author Benn
	 */
	public class GameWorld extends World 
	{
		
		private var NewWindowTimer : Number = 7;
		
		private var IsTweening : Boolean = false;
		private var IsTweeningDead : Boolean = false;
		
		private var UnusedWindows : Array = ["RED", "BLUE", "GREEN", "YELLOW"];
		
		private var CurrentWindows : Array = [];
		
		private var IsGameOver : Boolean = false;
		
		
		public function GameWorld() 
		{
			
			AddNewWindow();
			
			/*
			var newRedWindow : GameWindow = new GameWindow("RED");
			//newRedWindow.SetPosition(200, 150);
			newRedWindow.SetPosition(0, 300);
			add(newRedWindow);
			GameWindows.push(newRedWindow);
			
			var newGreenWindow : GameWindow = new GameWindow("GREEN");
			newGreenWindow.SetPosition(400, 300);
			add(newGreenWindow);
			
			var newYellowWindow : GameWindow = new GameWindow("YELLOW");
			newYellowWindow.SetPosition(0, 0);
			add(newYellowWindow);
			
			var newBlueWindow : GameWindow = new GameWindow("BLUE");
			newBlueWindow.SetPosition(400, 0);
			add(newBlueWindow);
			*/
		}
		
		private function AddNewWindow():void
		{
			
			var windowcount : int = 4 - UnusedWindows.length;
			
			var windownumber : int = FP.rand(UnusedWindows.length);
			
			var newWindow : GameWindow = new GameWindow(UnusedWindows[windownumber]);
			
			if (UnusedWindows[windownumber] == "RED")
			{
				newWindow.SetPosition(0, 0);
			}
			else if (UnusedWindows[windownumber] == "BLUE")
			{
				newWindow.SetPosition(400, 0);
			}
			else if (UnusedWindows[windownumber] == "GREEN")
			{
				newWindow.SetPosition(0, 300);
			}
			else //4th window
			{
				newWindow.SetPosition(400, 300)
			}
			
			UnusedWindows.splice(windownumber, 1);
			
			add(newWindow);
			
			CurrentWindows.push(newWindow);
			
			
		}
	
		public function OnTweenComplete():void
		{	
			IsTweening = false;
			AddNewWindow();
		}
		
		public function OnDeadTweenComplete():void
		{
			IsTweeningDead = false;
		}
		
		private function CheckGameOver():void
		{
			
			var IsAllDead : Boolean = true;
			
			//clean up pass
			for each (var Window : GameWindow in CurrentWindows)
			{
				if (Window.GetCleanMeUpNow() == false)
				{
					IsAllDead = false;
				}
			}
			
			if (IsAllDead)
			{
				IsGameOver = true;
				var GameOverText : HudText = new HudText("Game Over");
				GameOverText.x = 340;
				GameOverText.y = 200;
				add(GameOverText);
			}
			
		}
		
		private function UpdateCleanUp():void
		{
			
			
			var IsWindowDead : Boolean = false;
			
			var DeleteArray : Array = [];
			var counter : int = 0;
			
			//clean up pass
			for each (var Window : GameWindow in CurrentWindows)
			{
				if (Window.GetCleanMeUpNow())
				{
					trace("CLEAN");
					UnusedWindows.push(Window.GetWindowColour());
					DeleteArray.push(counter);
					Window.CleanUp();
					remove(Window);
					IsWindowDead = true;
					NewWindowTimer = 3;
					break;
					
				}
				counter++;
			}
			
			//delete pass
			while (DeleteArray.length > 0)
			{
				var ToDelete : int = DeleteArray.pop();
				CurrentWindows.splice(ToDelete, 1);
			}
			
			if (IsWindowDead)
			{
				//TweenOutOldWindow();
			}
			
		}
		
		private function TweenOutOldWindow():void
		{
			
			var newTween : VarTween;
			var newTweenTwo : VarTween;
			var Window : GameWindow;		
			var LeftCount : int = 0;
			var RightCount : int = 0;
			
			if (UnusedWindows.length == 4)
			{
				IsGameOver = true;
				//GameOverText.visible = true
				return;
			}
			else if (UnusedWindows.length == 1) //4 to 3
			{

				LeftCount = 0;
				RightCount = 0;
			
				for each (Window in CurrentWindows)
				{
					if (Window.x > -5 && Window.x < 5)
					{
						LeftCount++;
					}
					else
					{
						RightCount++;
					}
				}
				
				for each (Window in CurrentWindows)
				{
					
					if (((Window.x > -5 && Window.x < 5) && LeftCount == 1) || ((Window.x > 395 && Window.x < 405) && RightCount == 1))
					{
						newTween = new VarTween(OnDeadTweenComplete);
						newTween.tween(Window, "y", 150, 1, Ease.expoInOut);
						addTween(newTween, true);
						IsTweeningDead = true;
					}
					
				}
				
			}
			else if (UnusedWindows.length == 2) //3 to 2
			{
				
				LeftCount = 0;
				RightCount = 0;
			
				for each (Window in CurrentWindows)
				{
					if (Window.x > -1 && Window.x < 1)
					{
						LeftCount++;
					}
					else
					{
						RightCount++;
					}
				}
				
				if (LeftCount == 0)
				{
					newTween = new VarTween(OnDeadTweenComplete);
					newTween.tween(CurrentWindows[0], "x", 200, 1, Ease.expoInOut);
					addTween(newTween, true);
					
					newTweenTwo = new VarTween();
					newTweenTwo.tween(CurrentWindows[1], "x", 200, 1, Ease.expoInOut);
					addTween(newTweenTwo, true);
					IsTweeningDead = true;
				}
				else
				{
					newTween = new VarTween(OnDeadTweenComplete);
					newTween.tween(CurrentWindows[0], "y", 150, 1, Ease.expoInOut);
					addTween(newTween, true);
					IsTweeningDead = true;
				}
				
				
			}
			else //2 to 1
			{
				newTween = new VarTween(OnDeadTweenComplete);
				newTween.tween(CurrentWindows[0], "y", 150, 1, Ease.expoInOut);
				addTween(newTween, true);
				IsTweeningDead = true;
			}
			
			if (IsTweeningDead == false)
			{
				trace("IS TWEENINGDEAD FUCKUP");
			}
			
		}
		
		private function TweenInNewWindow():void
		{
			
			var newTween : VarTween;
			var newTweenTwo : VarTween;
			var Window : GameWindow;		
			var LeftCount : int = 0;
			var RightCount : int = 0;
			
			if (UnusedWindows.length == 3) // 1 to 2
			{
				newTween = new VarTween(OnTweenComplete);
				newTween.tween(CurrentWindows[0], "y", 300, 1, Ease.expoInOut);
				addTween(newTween, true);
				IsTweening = true;
			}
			else if (UnusedWindows.length == 2) //2 to 3
			{
				
				if (CurrentWindows[0].x >= 100)
				{
					newTween = new VarTween(OnTweenComplete);
					newTween.tween(CurrentWindows[0], "x", 400, 1, Ease.expoInOut);
					addTween(newTween, true);
					
					newTweenTwo = new VarTween();
					newTweenTwo.tween(CurrentWindows[1], "x", 400, 1, Ease.expoInOut);
					addTween(newTweenTwo, true);
					IsTweening = true;
				}
				
				
			}
			else //3 to 4
			{
				LeftCount = 0;
				RightCount = 0;
				
				for each (Window in CurrentWindows)
				{
					if ((Window.x > -5 && Window.x < 5))
					{
						LeftCount++;
					}
					else
					{
						RightCount++;
					}
				}
				
				for each (Window in CurrentWindows)
				{
					
					if (((Window.x > -5 && Window.x < 5) && LeftCount == 1) || ((Window.x > 395 && Window.x < 405) && RightCount == 1))
					{
						newTween = new VarTween(OnTweenComplete);
						newTween.tween(Window, "y", 300, 1, Ease.expoInOut);
						addTween(newTween, true);
						IsTweening = true;
					}
					
				}
			}
			
			if (IsTweening == false)
			{
				trace("TWEENING IS FALSE YOU FUCKED UP");
			}
			
		}
		
		private function UpdateTweening():void
		{
			
			for each (var Window : GameWindow in CurrentWindows)
			{
				Window.SetPosition(Window.x, Window.y);
			}

		}
		
		override public function update():void 
		{
			
			if (IsGameOver)
			{
				return;
			}
			
			if (IsTweening || IsTweeningDead)
			{
				UpdateTweening();
				super.update();
				return;
			}
			
			CheckGameOver();
			
			UpdateCleanUp();
			
			NewWindowTimer -= FP.elapsed;
			
			if (NewWindowTimer < 0 && UnusedWindows.length > 0)
			{
				//TweenInNewWindow();
				AddNewWindow();
				NewWindowTimer += 7;
			}	
			
			super.update();
			
		}
		
	}

}