JustOnTime.prototype = Object.create(Phaser.State.prototype);
JustOnTime.constructor = JustOnTime;

function JustOnTime() {
}

JustOnTime.prototype.preload = function() {
	game.load.image('icon_fullscreen',	'../../../resources/image/icon/fullscreen_white.png');

	game.load.image('god_angry', '../../../resources/image/character/god/god_hungry.png');
	game.load.image('god_smile', '../../../resources/image/character/god/god_smile.png');
	game.load.image('god_happy', '../../../resources/image/character/god/god_happy.png');

	game.load.image('tile_choco',	'../../../resources/image/background/tile_choco.png');
	game.load.image('heart_full',	'../../../resources/image/ui/heart_full.png');
	game.load.image('heart_empty',	'../../../resources/image/ui/heart_empty.png');

	game.load.script('webfont', '//ajax.googleapis.com/ajax/libs/webfont/1.4.7/webfont.js');
}

JustOnTime.prototype.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
		"card matching"	// callerClassName
	);

	// 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)
	);

	this.stageTimer = new StageTimer(
		JustOnTime.GAME_TIME_SEC,
		(function() {
			// this.setClickEnable(false);

			this.timeOver();
		}).bind(this)
	);

	this.initVariables();


	// game stage
	game.stage.backgroundColor = '#4d4d4d';
	graphics = game.add.graphics(0, 0);


	this.makeGuideButton(
		225, JustOnTime.GUIDE_BUTTON_POS_Y,
		"시작 ~ 1초",
		"점수 : 시작 초의 " + TimerStage.REWARD_BAD + "배",
		0xFAFAD2
	);

	this.makeGuideButton(
		418, JustOnTime.GUIDE_BUTTON_POS_Y,
		"1초 ~ 0.5초",
		"점수 : 시작 초의 " + TimerStage.REWARD_SOSO + "배",
		0xDAA520
	);

	this.makeGuideButton(
		612, JustOnTime.GUIDE_BUTTON_POS_Y,
		"0.5초 ~ 0.2초",
		"점수 : 시작 초의 " + TimerStage.REWARD_GOOD + "배",
		0xD2691E
	);

	this.makeGuideButton(
		805, JustOnTime.GUIDE_BUTTON_POS_Y,
		"0.2초 ~ 0초",
		"점수 : 시작 초의 " + TimerStage.REWARD_GREAT + "배",
		0xA52A2A
	);

	this.god = new God();

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


	// // this.countDown();
}

JustOnTime.prototype.initVariables = function() {
}

JustOnTime.prototype.makeGuideButton = function(x, y, guideNote, rewardNote, colorHex) {
	var setting = new RoundRectButtonSetting(x, y, 180, 80);
	setting.roundAmount = 3;
	setting.fontStyle.fontWeight = "bold";
	setting.setStrokeColor(
		colorHex,
		colorHex,
		colorHex,
		colorHex
	);
	setting.setButtonColor(
		colorHex,
		colorHex,
		colorHex,
		colorHex
	);
	setting.setTextColor(
		"#fec",
		"#dca",
		"#dca",
		"#333"
	);

	var guideButton = new RoundRectButton(
		setting,
		RoundRectButton.NONE_ICON,
		guideNote,
		(function() { this.buttonClicked(); }).bind(this)
	);
	guideButton.addShortcutText(rewardNote);
	guideButton.setInputEnabled(false);
}



JustOnTime.prototype.fontLoaded = function() {
	this.leftTimerStage = new TimerStage(
		JustOnTime.TIMER_STAGE_POS_X,
		JustOnTime.TIMER_STAGE_POS_Y
	);
	this.leftTimerStage.setOnPlusScore(
		(function(score, timeLeft) { this.plusScore(score, timeLeft); }).bind(this)
	);
	this.leftTimerStage.setOnGameOver(
		(function() { this.gameOver(); }).bind(this)
	);

	this.rightTimerStage = new TimerStage(
		GAME_SCREEN_SIZE.x - JustOnTime.TIMER_STAGE_POS_X,
		JustOnTime.TIMER_STAGE_POS_Y
	);
	this.rightTimerStage.setOnPlusScore(
		(function(score, timeLeft) { this.plusScore(score, timeLeft); }).bind(this)
	);
	this.rightTimerStage.setOnGameOver(
		(function() { this.gameOver(); }).bind(this)
	);

	this.startGame();
}


JustOnTime.prototype.plusScore = function(score, timeLeft) {
	this.scoreManager.plusScore(score);

	if(timeLeft < TimerStage.GRADE_GREAT_MS)
		this.god.animateBigHappy();
	else if(timeLeft < TimerStage.GRADE_GOOD_MS)
		this.god.animateBigSmile();
	else if(timeLeft < TimerStage.GRADE_SOSO_MS)
		this.god.animateLittleSmile();
	else
		this.god.animateLittleAngry();
}

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

/*
JustOnTime.prototype.countDown = function() {
	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();
}

JustOnTime.prototype.tweenCountDown = function() {
	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--;
}
*/

JustOnTime.prototype.startGame = function() {
	this.stageTimer.start();
}



JustOnTime.prototype.timeOver = function() {
	this.leftTimerStage.stop();
	this.rightTimerStage.stop();

	this.god.animateBigAngry();

	var timeOverText = new TimeOverText();
	if(!isExperienceMaestroAccount())
		this.updateResultRecord();

	// game.time.events.remove(this.timeEvent);

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

JustOnTime.prototype.gameOver = function() {
	this.leftTimerStage.stop();
	this.rightTimerStage.stop();

	this.god.animateBigAngry();

	var gameOverText = new GameOverText();
	if(!isExperienceMaestroAccount())
		this.updateResultRecord();

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


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

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



JustOnTime.GAME_TIME_SEC		= 60;

JustOnTime.GUIDE_BUTTON_POS_Y	= 130;

JustOnTime.TIMER_STAGE_POS_X	= 250;
JustOnTime.TIMER_STAGE_POS_Y	= 250;