var Start = {

	preload: function() {
		game.load.image('icon_fullscreen', '../../../resources/image/icon/fullscreen_white.png');
	},

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

		this.dbService = new DBService();
		this.dbService.setMaestroID(sessionStorageManager.getMaestroID());
		this.dbService.setPlayerID(sessionStorageManager.getPlayerID());

		this.game.stage.backgroundColor = '#4d4d4d';
		this.howToPlay = new HowToPlay();
		this.chart = new Chart();


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

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


		// contents
		this.loadHowToPlay(sessionStorageManager.getPlayingAppID());
		this.makeStartButton();
		this.makeRankingButton();

		// if(sessionStorageManager.getMaestroAccountType() >= 100) {	// experience account
		if(isExperiencePlayerAccount() || isExperienceMaestroAccount()) {	// experience account
			this.printSampleChart();
			this.announceBox = new AnnounceBox(50, 648);
			this.announceBox.drawBox("체험 계정에서는 기록이 저장되지 않습니다.");
		} else {
			this.loadChart();
		}


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

		if(isTypingExamApp()) {
			this.dbService.getTypingExamHighestRecord(
				sessionStorageManager.getWritingID(),

				(function(replyJSON) {
					// console.log(replyJSON);
					var highestRecordData = replyJSON["highestRecordData"];
					var highestRecord = highestRecordData["highestRecord"];
					this.onReceiveAppHighestRecord(highestRecord);
				}).bind(this),

				(function(replyJSON) {	// no data
					this.onReceiveAppHighestRecord(0);
				}).bind(this)
			);
		} else {
			this.dbConnectManager.requestAppHighestRecord(
				sessionStorageManager.getMaestroID(),
				sessionStorageManager.getPlayerID(),
				sessionStorageManager.getPlayingAppID(),

				(function(replyJSON) {
					var highestRecord = replyJSON["AppHighestRecord"];
					this.onReceiveAppHighestRecord(highestRecord);
				}).bind(this),

				(function(replyJSON) {	// no data
					this.onReceiveAppHighestRecord(0);
				}).bind(this)
			);
		}
	},

	onReceiveAppHighestRecord: function(highestRecord) {
		sessionStorageManager.setAppHighestRecord(Number(highestRecord));
		this.screenBottomUI.printLeftTextWithAppHighestRecord(highestRecord);
	},


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

		/*
		sessionStorageManager.resetPlayingAppData();

		if(isTypingExamApp())
			location.href = '../../web/client/menu_typing_exam.html';
		else if(isTypingPracticeApp())
			location.href = '../../web/client/menu_typing_practice.html';
		else if(isTypingTestApp())
			location.href = '../../web/client/menu_typing_test.html';
		else
			location.href = '../../web/client/menu_app.html';
		*/
	},

	loadHowToPlay: function(appID) {
		this.dbConnectManager.requestHowToPlay(
			sessionStorageManager.getPlayingAppID(),	// space_invaders app ID
			(function(jsonData) {
				var howToPlayText = jsonData["HowToPlay"].replace(/\\n/g, "\n");
				this.howToPlay.printHowToPlay(howToPlayText);
			}).bind(this),
			(function(jsonData) {
				this.howToPlay.printHowToPlay("No data");
			}).bind(this)
		);

	},

	loadChart: function() {
		var today = new Date();
		var date = DateUtil.getYYYYMMDD(today);

		if(isTypingExamApp()) {
			this.dbService.requestTypingExamPlayerHistory(
				sessionStorageManager.getWritingID(),
				date,

				(function(historyRecordManager) {
					this.onReceiveHistoryRecordManager(historyRecordManager);
				}).bind(this),

				(function(jsonData) {
					console.log(jsonData);
				}).bind(this)
			);
		} else {
			this.dbConnectManager.requestPlayerHistory(
				date,

				(function(historyRecordManager) {
					this.onReceiveHistoryRecordManager(historyRecordManager);
				}).bind(this),

				(function(jsonData) {
					console.log(jsonData);
				}).bind(this)
			);
		}
	},

	onReceiveHistoryRecordManager: function(historyRecordManager) {
		if(historyRecordManager.count == 0) {
			console.log("start - history record : no data");
			return;
		}

		var underValue = historyRecordManager.underValueForGraph();
		var upperValue = historyRecordManager.upperValueForGraph();
		// console.log("underValue : " + underValue);
		// console.log("upperValue : " + upperValue);

		var minValue = underValue - (upperValue - underValue) / 10;
		var maxValue = upperValue;// + (upperValue - underValue) / 10;

		var countRecord = historyRecordManager.count;
		for(var i = 0; i < Chart.CHART_COUNT; i++) {
			// if(i > 6)
			// 	break;

			var historyRecord = historyRecordManager.getAt(i);

			var dateText = historyRecord === undefined ? "-" : historyRecord.date;
			var record = historyRecord === undefined ? "" : historyRecord.bestRecord;
			// console.log("record : " + record);
			// console.log("minValue : " + minValue);
			// console.log("maxValue : " + maxValue);
			this.chart.drawRecordGraph(i, dateText, record, minValue, maxValue);
		}

		this.chart.printChartBaseLine();
	},

	calcDate: function(daysBefore) {
		var date = new Date();
		date.setDate(date.getDate() - daysBefore);
		return date;
	},

	printSampleChart: function() {
		var minValue = 0;
		var maxValue = 1000;
		this.chart.drawRecordGraph(0, DateUtil.getYYYYMMDD(this.calcDate(1)), 980, minValue, maxValue);
		this.chart.drawRecordGraph(1, DateUtil.getYYYYMMDD(this.calcDate(2)), 760, minValue, maxValue);
		this.chart.drawRecordGraph(2, DateUtil.getYYYYMMDD(this.calcDate(5)), 740, minValue, maxValue);
		this.chart.drawRecordGraph(3, DateUtil.getYYYYMMDD(this.calcDate(7)), 690, minValue, maxValue);
		this.chart.drawRecordGraph(4, DateUtil.getYYYYMMDD(this.calcDate(8)), 710, minValue, maxValue);
		this.chart.drawRecordGraph(5, DateUtil.getYYYYMMDD(this.calcDate(10)), 560, minValue, maxValue);
		this.chart.drawRecordGraph(6, DateUtil.getYYYYMMDD(this.calcDate(20)), 480, minValue, maxValue);

		this.chart.printChartBaseLine();
	},

	makeStartButton: function() {
		var setting = new RoundRectButtonSetting(game.world.centerX, game.world.centerY, 200, 100);
		setting.fontStyle.fontWeight = "bold";

		var startButton = new RoundRectButton(setting, RoundRectButton.NONE_ICON, "시작", this.startStage);
		startButton.addShortcutText("Enter");
	},

	makeRankingButton: function() {
		var setting = new RoundRectButtonSetting(game.world.centerX + 160, game.world.centerY, 60, 60);
		setting.fontStyle = {
			font: "18px Arial",
			boundsAlignH: "center",		// left, center. right
			boundsAlignV: "middle", 	// top, middle, bottom
			fill: "#fff"
		};

		var rankingButton = new RoundRectButton(
			setting, RoundRectButton.NONE_ICON, "순위\n보기",
			(function() {
				location.href = '../../web/client/ranking.html';
			})
		);
		if(sessionStorageManager.getMaestroAccountType() == 100)
			rankingButton.inputEnabled = false;
	},


	startStage: function() {
		location.href = "../../web/client/" + getGameAppName() + ".html";
	}

}