var WordFlyingSaucer = {

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

		sessionStorageManager.setIsNewAppHighestRecord(false);

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

		this.initVariables();



		// game stage
		this.game.stage.backgroundColor = "#000000";	// '#4d4d4d';
		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');
		}

		this.flyingsaucers = [];
		for(var i = 0; i < 2; i++) {
			this.flyingsaucers[i] = new FlyingSaucer(i);
			this.flyingsaucers[i].setup(this.wordLevel, this.speedLevel);
			this.flyingsaucers[i].setActive(true);
		}



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


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

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




		// input text
		var INPUT_TEXT_Y = 680;
		this.inputText = new InputTypeText(game.world.centerX, INPUT_TEXT_Y);
		this.inputText.anchor.set(0.5);
		this.inputText.canvasInput.value('');
		this.inputText.canvasInput.focus();
		this.inputText.canvasInput.placeHolder("입력 후 [ Enter ]키를 누르세요");
		// inputText.canvasInput._onkeydown = this.checkInputText;
		// inputText.canvasInput._onkeyup = function() {
		var self = this;
		this.inputText.canvasInput._onkeyup = function() {
			self.checkTypingContents(event);
		}





		// bottom ui
		this.screenBottomUI = new ScreenBottomUI();
		// screenBottomUI.printBottomUILeftText("");
		this.screenBottomUI.printCenterText(sessionStorageManager.getPlayingAppKoreanName());
		this.screenBottomUI.printRightText(sessionStorageManager.getPlayerName());


		// this.startGame();
	},


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


	initVariables: function() {
		this.wordLevel = 0;
		this.speedLevel = 0;
	},

	startGame: function() {
	},

	timeOver: function() {
		var timeOverText = new TimeOverText();

		sessionStorageManager.setRecord(this.scoreManager.getScore());
		if(!isExperienceMaestroAccount())
			this.updateResultRecord();

		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';
	},


	checkTypingContents: function(event) {
		var count = this.flyingsaucers.length;
		if(event.keyCode == Phaser.Keyboard.ENTER) {
			for(var i = 0; i < count; i++) {
				this.flyingsaucers[i].attack(this.inputText.canvasInput.value());
			}

			this.inputText.canvasInput.value("");
		} else {

			for(var i = 0; i < count; i++) {
				this.flyingsaucers[i].checkInputText(this.inputText.canvasInput.value());
			}
		}
	},

}


