package {
	import flash.display.*;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.events.MouseEvent;
	import flash.events.Event;
	
	public class KontrolKlasa extends MovieClip {
		const monsterStartX:int = -15;
		const monsterStartY:int = 125;
		const monsterSpeed:int = 2; 
		const monsterNo:int = 10;
		
		var currTower;
		var currTile:Tile;
		var currGold:int=100;
		var currLevel:int = 0;
		var gData:Data = new Data();
		
		var nonPlacableTiles:Array = new Array();
		var level:Array = new Array();
		var monsters:Array = new Array();
		var bullets:Array = new Array();
		var towers:Array = new Array();
		var waypointsX:Array = new Array();
		var waypointsY:Array = new Array();
		var stageObjects:Array = new Array();
		
		public function KontrolKlasa() {
			playBtn.addEventListener(MouseEvent.MOUSE_UP,playBtnUpHandler);
		}
		
		function playBtnUpHandler(event:MouseEvent) {
			gotoAndStop(3);
			init();
		}
		
		function init():void {
			gData.setUp();
			level = gData.levels[currLevel];
			waypointsX = gData.waypointsX[currLevel];
			waypointsY = gData.waypointsY[currLevel];
			currGold=100;
			
			BuildMap(level);
			//menjamo tu
			for (var j:int=0;j<monsterNo;j++) {
				if (currLevel+1 == 1) {
					if (j<5) 
						createMonster(monsterStartX-j*50,waypointsY[0]);
					else 
						createMonster2(monsterStartX-j*50,waypointsY[0]);
				}
				if (currLevel+1 == 2) {
					if (j%2==0)
						createMonster(monsterStartX-j*50,waypointsY[0]);
					else
						createMonster2(monsterStartX-j*50,waypointsY[0]);
				}
				if (currLevel+1 == 3) {
					if (j==6 || j==4) {
						createMonster3(monsterStartX-j*50,waypointsY[0]);
						continue;
					}
					if (j%2==0)
						createMonster(monsterStartX-j*50,waypointsY[0]);
					else
						createMonster2(monsterStartX-j*50,waypointsY[0]);
				}
				
			}
			
			BtnTower.addEventListener(MouseEvent.MOUSE_UP,TowerBtnUpHandler);
			BtnTower2.addEventListener(MouseEvent.MOUSE_UP,Tower2BtnUpHandler);
			restartBtn.addEventListener(MouseEvent.MOUSE_UP, restartBtnUpHandler);
			stage.addEventListener(MouseEvent.MOUSE_DOWN,OnMouseDownHandler);
			stage.addEventListener(Event.ENTER_FRAME, loop);
		}
		
		function restartBtnUpHandler(event:MouseEvent):void {
			if(currLevel>=gData.levels.length) {
				clearStage();
				currLevel=0;
				stage.addEventListener(Event.ENTER_FRAME,loop);
				gotoAndStop(3);
				init();
			} else {
				clearStage();
				gotoAndStop(3);
				init();
			}
		}
		
		function createMonster(xpos:int, ypos:int):void {
			var tmpMonster = new Monster();
			tmpMonster.x=xpos;
			tmpMonster.y=ypos;
			addChild(tmpMonster);
			monsters.push(tmpMonster);
		}
		function createMonster2(xpos:int, ypos:int):void {
			var tmpMonster = new Monster2();
			tmpMonster.x=xpos;
			tmpMonster.y=ypos;
			addChild(tmpMonster);
			monsters.push(tmpMonster);
		}
		function createMonster3(xpos:int, ypos:int):void {
			var tmpMonster = new Monster3();
			tmpMonster.x=xpos;
			tmpMonster.y=ypos;
			addChild(tmpMonster);
			monsters.push(tmpMonster);
		}
		
		function moveMonster(tmpMonster, i:int):void {
			var distX:Number = waypointsX[tmpMonster.nextWayPoint]-tmpMonster.x;
			var distY:Number = waypointsY[tmpMonster.nextWayPoint]-tmpMonster.y;
			if (Math.abs(distY) + Math.abs(distX) < 1) {
				tmpMonster.nextWayPoint++;
			}
			var angle:Number = Math.atan2(distY, distX);
			tmpMonster.x += monsterSpeed*Math.cos(angle);
			tmpMonster.y += monsterSpeed*Math.sin(angle);
			tmpMonster.rotation = angle/Math.PI*180;
			if (currLevel+1 == 3) {
				if (tmpMonster.y >= 375) {
					clearStage();
					init();
				}
			} 
			else {
				if (tmpMonster.x >= 475) {
					clearStage();
					init();
				}
			}
			if (tmpMonster.hp <= 0) {
				currGold += tmpMonster.gold;
				removeChild(tmpMonster);
				monsters.splice(i,1);
			}
		}
		
		function loop(event:Event) {
			if (monsters.length <= 0) {
				currLevel++;
				if(currLevel>=gData.levels.length){
					gotoAndStop(4);
					stage.removeEventListener(Event.ENTER_FRAME,loop);
					return 0;
				}else{
					level=gData.levels[currLevel];
					clearStage();
					init();
				}
			}
			for (var i:int=0;i<monsters.length;i++) {
				moveMonster(monsters[i],i);
			}
			for (var j:int=0;j<towers.length;j++) {
				updateTowers(towers[j]);
			}
			for (var k:int=0;k<bullets.length;k++) {
				if (!bullets[k].remove) {
					bullets[k].update();
				}else{
					removeChild(bullets[k]);
					bullets.splice(k,1);
				}
			}
			if (currTower != null) {
				currTower.x = mouseX + 1 + currTower.width/2;
				currTower.y = mouseY + 1 + currTower.height/2;
			}
			txtGold.text = String(currGold);
		}
		
		function TowerBtnUpHandler(event:MouseEvent) {
			if (currTower == null) {
				currTower = new Tower();
				addChild(currTower);
			}
		}
		function Tower2BtnUpHandler(event:MouseEvent) {
			if (currTower == null) {
				currTower = new Tower2();
				addChild(currTower);
			}
		}
		
		function OnMouseDownHandler(event:MouseEvent) {
			if (currTower != null) {
				if (checkPlacable(currTile)) {
					if ((currGold-currTower.cost)>=0) {
						if (mouseX < 500) {
							txtStatus.text = "";
							currGold -= currTower.cost;
							towers.push(currTower);
							currTower.x = currTile.x + currTile.width/2;
							currTower.y = currTile.y + currTile.height/2;
							currTower = null;
							nonPlacableTiles.push(currTile);
						}
					}else{
						txtStatus.text = "Nedovoljno zlata!";
					}
				}
				if (mouseX > 500 && mouseX < 600) {
					removeChild(currTower);
					currTower = null;
				}
			}
		}
		
		function BuildMap(level):void {
			for (var i:int=0;i<level.length;i++) {
				for(var j:int=0;j<level[i].length;j++) {
					var tmpTile:Tile = new Tile();
					tmpTile.x = j*50;
					tmpTile.y = i*50;
					addChild(tmpTile);
					stageObjects.push(tmpTile);
					tmpTile.gotoAndStop(level[i][j]+1);
					if(level[i][j]==1) {
						nonPlacableTiles.push(tmpTile);
					}
					tmpTile.addEventListener(MouseEvent.ROLL_OVER,TurnOnTile);
					tmpTile.addEventListener(MouseEvent.ROLL_OUT,TurnOffTile);
					
				}
			
			
			}
		}
		
		function TurnOnTile(event:MouseEvent) {
			currTile = event.currentTarget as Tile;
			if (checkPlacable(currTile) && currTower != null) {
				currTile.gotoAndStop(3);
			}
		}
		
		function TurnOffTile(event:MouseEvent) {
			var prevTile = event.currentTarget as Tile;
			if (prevTile.currentFrame == 3) {
				prevTile.gotoAndStop(1);
			}
		}
		
		function clearStage():void {
			for (var k:int=0;k<monsters.length;k++) {
				if (monsters[k]!=null) {
					removeChild(monsters[k]);
					monsters[k]=null;
				}
			}
			for (var i:int=0;i<stageObjects.length;i++) {
				if (stageObjects[i]!=null) {
					removeChild(stageObjects[i]);
					stageObjects[i]=null;
				}
			}
			for (var j:int=0;j<towers.length;j++) {
				if (towers[j]!=null) {
					removeChild(towers[j]);
					towers[j]=null;
				}
			}
			if (currTower!=null) {
				removeChild(currTower);
				currTower=null;
			}
			stageObjects.splice(0);
			nonPlacableTiles.splice(0);
			monsters.splice(0);
			towers.splice(0);
			bullets.splice(0);
			
		}
		
		function checkPlacable(tmpTile:Tile):Boolean {
			for (var i:int=0;i<nonPlacableTiles.length;i++) {
				if (nonPlacableTiles[i]==tmpTile) {
					return false;
				}
			}
			return true;
		}
		
		function distance(A:MovieClip, B:MovieClip):Number {
			return Math.pow(Math.pow(B.x-A.x,2) + Math.pow(B.y-A.y,2), 0.5);
		}
		
		function updateTowers(tmpTower):void {
			if (tmpTower.reloadTime == 0) {
				for(var i:int=0;i<monsters.length;i++) {
					if (distance(tmpTower,monsters[i]) < tmpTower.range) {
						var angle:Number = Math.atan2(monsters[i].y - tmpTower.y, monsters[i].x - tmpTower.x);
						var tmpBullet:Bullet = new Bullet(angle,monsters[i]);
						tmpBullet.x = tmpTower.x;
						tmpBullet.y = tmpTower.y;
						addChild(tmpBullet);
						bullets.push(tmpBullet);
						tmpTower.reloadTime = tmpTower.cReloadTime;
						break;
					}
				}	
			} else {
				tmpTower.reloadTime -= 1;
			}
		}
		
		
	}
	
	
}