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

function TimerStage(x, y) {
	this.posX = x;
	this.posY = y;

	this.mainTimerText = new TimerText(x + TimerStage.TIMER_OFFSET_X, y, 60);
	this.timerButton = this.makeTimeStopButton(x, y + TimerStage.BUTTON_OFFSET_Y);
	this.timeScoreRecordArray = new Array();

	this.makeTimeScoreRecordArray();

	this.initialize();
	this.mainTimerText.setMSTimeText(this.timeLimitMS);

	return this;
}


TimerStage.prototype.setOnPlusScore = function(eventHandler) {
	this.onPlusScoreEventHandler = eventHandler;
}

TimerStage.prototype.setOnGameOver = function(eventHandler) {
	this.onGameOverEventHandler = eventHandler;
}

TimerStage.prototype.makeTimeStopButton = function(x, y) {
	var setting = new RoundRectButtonSetting(x, y, 240, 100);
	setting.fontStyle.fontWeight = "bold";

	var timerButton = new RoundRectButton(
		setting,
		RoundRectButton.NONE_ICON,
		"시작",
		(function() { this.buttonClicked(); }).bind(this)
	);
	return timerButton;
}

TimerStage.prototype.makeTimeScoreRecordArray = function() {
	for(var i = 0; i < TimerStage.RECORD_COUNT; i++) {
		var timeScoreRecord = new TimeScoreRecord(
			this.posX,
			this.posY + TimerStage.RECORD_OFFSET_Y + TimerStage.RECORD_GAP_Y * i
		);
		timeScoreRecord.setTextColorString("grey");
		this.timeScoreRecordArray.push(timeScoreRecord);
	}
}


TimerStage.prototype.initialize = function() {
	this.timeLimitMS = TimerStage.START_LIMIT_TIME_MS;

	this.state = TimerStage.STATE_READY;
	this.mainTimer = game.time.create(false);

	this.onPlusScoreEventHandler = null;
	this.onGameOverEventHandler = null;

	this.timerEvent = null;
	this.timerEvent = this.mainTimer.loop(
		1,
		(function() { this.updateTimer(); }).bind(this),
		this
	);

	this.recordArray = new Array();
}

TimerStage.prototype.updateTimer = function() {
	// console.log(game.time.totalElapsedSeconds());
	// console.log(this.mainTimer.ms);
	var timeLeft = this.timeLimitMS - this.mainTimer.ms;
	this.mainTimerText.setMSTimeText(timeLeft);
	// this.mainTimerText.setMSTimeText(this.mainTimer.ms);

	if(timeLeft < 0) {
		console.log("Game Over");

		this.onGameOverEventHandler();
	}
}

TimerStage.prototype.stop = function() {
	this.mainTimer.stop();

	this.timerButton.setInputEnabled(false);
}


TimerStage.prototype.buttonClicked = function() {
	if(this.state == TimerStage.STATE_READY) {
		// 시작
		this.mainTimer.start();

		this.timerButton.text.text = "정지";

		this.state = TimerStage.STATE_PLAYING;
	} else if(this.state == TimerStage.STATE_PLAYING) {
		// 중지
		this.decideGrade(this.timeLimitMS - this.mainTimer.ms);

		this.mainTimer.stop(false);
		// this.updateTimer();

		// this.mainTimerText.setMSTimeText(this.timeLimitMS);
		this.isTimerStarted = false;
		this.isTimerStoped = true;
		this.timerButton.text.text = "리셋";

		this.state = TimerStage.STATE_STOPPED;
	} else {
		// 리셋
		this.mainTimerText.setMSTimeText(this.timeLimitMS);
		this.timerButton.text.text = "시작";

		this.state = TimerStage.STATE_READY;
	}
}

TimerStage.prototype.decideGrade = function(timeLeft) {
	// console.log(timeLeft);
	if(timeLeft < 0) {
		console.log("Game Over")
	}
	else if(timeLeft < TimerStage.GRADE_GREAT_MS)	this.gradeGreat(timeLeft);
	else if(timeLeft < TimerStage.GRADE_GOOD_MS)	this.gradeGood(timeLeft);
	else if(timeLeft < TimerStage.GRADE_SOSO_MS)	this.gradeSoSo(timeLeft);
	else											this.gradeBad(timeLeft);

	this.updateTimeScoreRecordArray();
}

TimerStage.prototype.gradeGreat = function(timeLeft) {
	// console.log("Great")
	var startTime = Math.floor(this.timeLimitMS / 1000);
	var score = startTime * TimerStage.REWARD_GREAT;
	this.onPlusScoreEventHandler(score, timeLeft);
	var animationNote = new AnimationNote(
		this.posX, this.posY,
		startTime + "초 X " + TimerStage.REWARD_GREAT + "배 = " + score,
		"Brown"
	);

	// this.recordArray.push( { timeLeft, score } );
	var record = {};
	record.timeLeft = timeLeft;
	record.score = score;
	this.recordArray.push(record);

	this.timeLimitMS = this.timeLimitMS + TimerStage.TIME_EXTENSION_GREAT_MS;
}

TimerStage.prototype.gradeGood = function(timeLeft) {
	// console.log("Good")
	var startTime = Math.floor(this.timeLimitMS / 1000);
	var score = startTime * TimerStage.REWARD_GOOD;
	this.onPlusScoreEventHandler(score, timeLeft);
	var animationNote = new AnimationNote(
		this.posX, this.posY,
		startTime + "초 X " + TimerStage.REWARD_GOOD + "배 = " + score,
		"Chocolate"
	);

	// this.recordArray.push( { timeLeft, score } );
	var record = {};
	record.timeLeft = timeLeft;
	record.score = score;
	this.recordArray.push(record);

	this.timeLimitMS = this.timeLimitMS + TimerStage.TIME_EXTENSION_GOOD_MS;
}

TimerStage.prototype.gradeSoSo = function(timeLeft) {
	// console.log("SoSo")
	var startTime = Math.floor(this.timeLimitMS / 1000);
	var score = startTime * TimerStage.REWARD_SOSO;
	this.onPlusScoreEventHandler(score, timeLeft);
	var animationNote = new AnimationNote(
		this.posX, this.posY,
		startTime + "초 X " + TimerStage.REWARD_SOSO + "배 = " + score,
		"GoldenRod"
	);

	// this.recordArray.push( { timeLeft, score } );
	var record = {};
	record.timeLeft = timeLeft;
	record.score = score;
	this.recordArray.push(record);

	this.timeLimitMS = this.timeLimitMS + TimerStage.TIME_EXTENSION_SOSO_MS;
}

TimerStage.prototype.gradeBad = function(timeLeft) {
	// console.log("Bad")
	var startTime = Math.floor(this.timeLimitMS / 1000);
	var score = startTime * TimerStage.REWARD_BAD;
	this.onPlusScoreEventHandler(score, timeLeft);
	var animationNote = new AnimationNote(
		this.posX, this.posY,
		startTime + "초 X " + TimerStage.REWARD_BAD + "배 = " + score,
		"LightGoldenRodYellow"
	);

	// this.recordArray.push( { timeLeft, score } );
	var record = {};
	record.timeLeft = timeLeft;
	record.score = score;
	this.recordArray.push(record);

	this.timeLimitMS = this.timeLimitMS + TimerStage.TIME_EXTENSION_BAD_MS;
}


TimerStage.prototype.updateTimeScoreRecordArray = function() {
	// console.log(this.recordArray);

	for(var i = 0; i < TimerStage.RECORD_COUNT; i++) {
		var timeScoreRecordIndex = i;
		var recordIndex = this.recordArray.length - 1 - i;
		if(recordIndex < 0)
			break;

		var record = this.recordArray[recordIndex];
		// console.log(record);
		this.timeScoreRecordArray[timeScoreRecordIndex].setContent(record.timeLeft, record.score);
		if(record.timeLeft < TimerStage.GRADE_GREAT_MS)
			this.timeScoreRecordArray[timeScoreRecordIndex].setTextColorString("Brown");
		else if(record.timeLeft < TimerStage.GRADE_GOOD_MS)
			this.timeScoreRecordArray[timeScoreRecordIndex].setTextColorString("Chocolate");
		else if(record.timeLeft < TimerStage.GRADE_SOSO_MS)
			this.timeScoreRecordArray[timeScoreRecordIndex].setTextColorString("GoldenRod");
		else
			this.timeScoreRecordArray[timeScoreRecordIndex].setTextColorString("LightGoldenRodYellow");
	}
}



TimerStage.STATE_READY			= 0;
TimerStage.STATE_PLAYING		= 1;
TimerStage.STATE_STOPPED		= 2;

TimerStage.START_LIMIT_TIME_MS	= 1000;

TimerStage.TIMER_OFFSET_X		= 120;

TimerStage.BUTTON_OFFSET_Y		= 100;

TimerStage.RECORD_OFFSET_Y		= 200;
TimerStage.RECORD_GAP_Y			= 50;
TimerStage.RECORD_COUNT			= 5;


TimerStage.GRADE_GREAT_MS		= 200;
TimerStage.GRADE_GOOD_MS		= 500;
TimerStage.GRADE_SOSO_MS		= 1000;

TimerStage.REWARD_GREAT			= 10;
TimerStage.REWARD_GOOD			= 3;
TimerStage.REWARD_SOSO			= 2;
TimerStage.REWARD_BAD			= 1;

TimerStage.TIME_EXTENSION_GREAT_MS	= 3000;
TimerStage.TIME_EXTENSION_GOOD_MS	= 1000;
TimerStage.TIME_EXTENSION_SOSO_MS	= 500;
TimerStage.TIME_EXTENSION_BAD_MS	= 0;