var Game = {

	create: function() {
		this.dbConnectManager = new DBConnectManager();
		sessionStorageManager.setRecord(0);

		this.game.stage.backgroundColor = "#000000";	// '#4d4d4d';

		sessionStorageManager.setIsNewAppHighestRecord(false);

		var experienceAppTimer = new ExperienceAppTimer();
		experienceAppTimer.setTimeOverListener(
			(function() { this.timeOver(); }).bind(this)
		);

		// keyboard shortcut
		this.keyboardShortcut = new KeyboardShortcut();
		this.keyboardShortcut.addCallback(
			Phaser.KeyCode.ESC,	// keyCode
			null,	// keyDownHandler
			(function() { this.back(); }).bind(this),	// keyUpHandler
			"space invaders"	// callerClassName
		);

		// top ui
		var screenTopUI = new ScreenTopUI();
		screenTopUI.makeBackButton( (function() { this.back(); }).bind(this) );
		screenTopUI.makeFullScreenButton();

		var scoreBoard = new ScoreBoard();
		scoreManager.addOnChangeScoreListener( function(score) {
			sessionStorageManager.setRecord(score);
			scoreBoard.printScore(NumberUtil.numberWithCommas(score));
		});
		scoreManager.addOnChangeHighScoreListener( function(highScore) {
			// console.log(highScore);
			// sessionStorageManager.setAppHighestRecord(highScore);
			sessionStorageManager.setIsNewBestRecrd(true);
			// screenBottomUI.printLeftTextWithAppHighestRecord(sessionStorageManager.getAppHighestRecord());
		});

		var heartGauge = new HeartGauge(heartManager);
		heartManager.addOnChangeGameOverListener(
			(function() { this.gameOver(); }).bind(this)
		);
		heartGauge.start();


		// game stage
		var stars = game.add.group();

		for (var i = 0; i < 128; i++)
		{
			var randomY = game.rnd.integerInRange(60, GAME_SCREEN_SIZE.y - 80);
			stars.create(game.world.randomX, randomY, 'star');
		}


		// waiting room
		this.game.add.graphics()
			.beginFill(0xffffff, 0.3)
			.drawRect(0, 100, game.world.width, 100);


		// contents
		this.aliens = [];
		for(var i = 0; i < 10; i++) {
			var alien = new Alien(150 + 80 * i, 150,
				(function() { this.activateNextAlien(); }).bind(this),	// onGoOnstage
				(function(sprite) {
					scoreManager.plusScore(this.getScore());
					var scoreText = new ScoreText(sprite.x, sprite.y, this.getScore());
				}).bind(this),		// this.onAlienFired,
				this.onAlienEscaped
			);
			alien.goWaitingRoom();
			alien.setActive(false);
			this.aliens.push(alien);
		}

		this.alienTimer = new AlienTimer(this.aliens);


		// bottom ui
		var screenBottomUI = new ScreenBottomUI();
		screenBottomUI.printLeftTextWithAppHighestRecord(sessionStorageManager.getAppHighestRecord());
		screenBottomUI.printCenterText(sessionStorageManager.getPlayingAppKoreanName());
		screenBottomUI.printRightText(sessionStorageManager.getPlayerName());


		this.startGame();
		// this.countDown();
	},


	back: function() {
		sessionStorageManager.resetPlayingAppData();
		location.href = '../../web/client/main_menu.html';
	},

	initListeners: function() {
	},

	/*
	countDown() {
		var style = { font: "bold 200px Arial", fill: "#fff", boundsAlignH: "center", boundsAlignV: "middle" };
		this.countDownText = game.add.text(0, 0, "", style);
		this.countDownText.setTextBounds(0, 0, game.world.width, game.world.height);
		this.countDownText.stroke = "#333";
		this.countDownText.strokeThickness = 50;

		this.countDownNumber = 3;
		if(isDebugMode())
			this.countDownNumber = 1;
		this.tweenCountDown();
	}

	tweenCountDown() {
		if(this.countDownNumber === 0) {
			this.startGame();
			return;
		}

		this.countDownText.text = this.countDownNumber.toString();
		this.countDownText.alpha = 1;

		var countDownTween = game.add.tween(this.countDownText);
		countDownTween.to( { alpha: 0 }, 1000, Phaser.Easing.Linear.None, true);
		countDownTween.onComplete.add(this.tweenCountDown, this);

		this.countDownNumber--;
	}
	*/

	startGame: function() {
		// activate first alien
		this.alienTimer.start();
	},

	timeOver: function() {
		var timeOverText = new TimeOverText();
		this.stopAndGoResult();
	},

	gameOver: function() {
		var gameOverText = new GameOverText();
		this.updateResultRecord();
		this.stopAndGoResult();
	},

	stopAndGoResult: function() {
		this.alienTimer.stop();

		game.time.events.add(Phaser.Timer.SECOND * 2, this.goResult, this);
	},

	updateResultRecord: function() {
		if(sessionStorageManager.getMaestroAccountType() < 100) {	// experience account
			this.dbConnectManager.updateResultRecord(
				sessionStorageManager.getMaestroID(),
				sessionStorageManager.getPlayerID(),
				sessionStorageManager.getPlayingAppID(),
				sessionStorageManager.getRecord()
			);
		}
	},

	goResult: function() {
		location.href = '../../web/client/result.html';
	},

	activateNextAlien: function() {
		this.alienTimer.next();
	},

	getScore: function() {
		return (this.alienTimer.activatedAlienIndex) * 2;
	},

	onAlienEscaped: function() {
		heartManager.breakHearts(1);
	}

}

Game.GAME_OVER_WAIT_TIME_MS		= 2000;