package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.net.URLRequest;
	import flash.media.SoundChannel;
	import flash.display.MovieClip;
	import flash.events.EventDispatcher;
	
	public class Game extends Sprite {
		
		public var game:Controller;
		private var screen:Sprite;
		private var sound:Sound = new Sound();
		private var req:URLRequest = new URLRequest("sounds/music.mp3");
		private var channel:SoundChannel;
		private var main_screen:Sprite;
		public var zombie_obj:Zombie;
		public var zombie:MovieClip;
		private var score:Number;
		
		
		public function Game() {
			////req = 
			sound.load(req);
			channel = sound.play();
			
			startScreen();
		}
		
		public function startScreen():void {
			screen = new m_screen();
			screen.x = 500;
			screen.y = 500;
			addChild(screen);
			m_screen(screen).play_button.addEventListener( MouseEvent.CLICK, playGame, false, 0, true );
		}
		
		public function playGame(ev:MouseEvent):void {
			
			removeChild(screen);
			
			main_screen = new Bg();
			
			//var ms:Sprite = new screen_main();
			
			zombie_obj = new Zombie(-1, 28, 630, 28, 372);
			//var zombie2:Zombie = new Zombie(1, main_screen, 372, 730, 198, 372);
			zombie = zombie_obj.returnZombie();
			
			screen = new Sprite();
			screen.x = 170;
			screen.y = 100;
			screen.addChild(zombie);
			
			main_screen.addChild(screen);
			
			//main_screen.addChild(ms);
			//addChild(main_screen);
			addChild(main_screen);
			game = new Controller(screen, new Config());
			game.startGame();
			game.addEventListener( TetrisEvent.SCORE_CHANGE, scoreChanged, false, 0, true );
			game.addEventListener( TetrisEvent.GAME_OVER, gameOver, false, 0, true );
			
			game._tetris.addEventListener( TetrisEvent.UPDATE_CELLS, gog, false, 0, true );
				
			
			
		}
		
		public function scoreChanged( ev:TetrisEvent ):void
		{
			score = ev.score;
			Bg(main_screen).score_text.text = ev.score.toString();
		}
		
		public function gameOver(ev:TetrisEvent):void {
			game.removeEventListener( TetrisEvent.SCORE_CHANGE, scoreChanged );
			game.removeEventListener( TetrisEvent.GAME_OVER, gameOver );
			score = game.score;
			removeChild( main_screen );
			screen = new EndScreen();
			addChild( screen );
			
			EndScreen( screen ).play_btn.addEventListener( MouseEvent.CLICK, playGame );
			EndScreen( screen ).score_text.text = score.toString();
		}
		
		public function gog(ev:TetrisEvent):void {
			//trace(ev.cells[0].location.y);
			//преобразуем в нормальные координаты
			/*if(zombie.x > 28 && zombie.x < 68) {
					zombie_obj.math_x = 0;
				} else if(zombie.x >= 68 && zombie.x < 108) {
					zombie_obj.math_x = 1;
				} else if(zombie.x >= 148 && zombie.x < 188) {
					zombie_obj.math_x = 2;
				} else if(zombie.x >= 188 && zombie.x < 228) {
					zombie_obj.math_x = 3;
				} else if(zombie.x >= 228 && zombie.x < 268) {
					zombie_obj.math_x = 4;
				} else if(zombie.x >= 268 && zombie.x < 308) {
					zombie_obj.math_x = 5;
				} else if(zombie.x >= 308 && zombie.x < 348) {
					zombie_obj.math_x = 6;
				} else if(zombie.x >= 348 && zombie.x < 388) {
					zombie_obj.math_x = 7;
				} else if(zombie.x >= 388 && zombie.x < 428) {
					zombie_obj.math_x = 8;
				}
				//trace(zombie.scaleX);
				if((zombie.scaleX == 1) && !game._tetris.isCellClear(new CellPos((zombie_obj.math_x), zombie_obj.num_line))) {
					//trace('обратно1');
					zombie_obj.rotate();
					zombie.scaleX == -1;
				} else if((zombie.scaleX == -1) && game._tetris.isCellClear(new CellPos((zombie_obj.math_x), zombie_obj.num_line)) == false){
					zombie_obj.rotate();
					zombie.scaleX == 1;
					
				}*/
			for(var i:Number = 0; i<4; i++) {
				var y_f:Number = ev.cells[i].location.y*40 + 170;
				
				var y_zombie:Number = zombie.y+ 100;
				
				var x_f:Number = ev.cells[i].location.x*40 + 200;
				var x_zombie:Number = zombie.x + 170;
				var d:Number = x_f - x_zombie;
				
				
				if(y_f == y_zombie && ((d > -40) && (d < 40))) {
					
					zombie_obj.blood();
					
					//scoreChange();
					
					zombie_obj = new Zombie(-1, 28, 630, 28, 372);
					zombie = zombie_obj.returnZombie();
					screen.addChild(zombie);
				}
			}
		}
		
		public function scoreChange():void {
			var ev:TetrisEvent = new TetrisEvent( TetrisEvent.SCORE_CHANGE );
			ev.score = score + 1;
			scoreChanged(ev);
		}
		
	}
}