var MenuTypingExam = {

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

		Animal.loadResources();
	},

	create: function() {
		game.stage.backgroundColor = '#4d4d4d';

		// typing app area
		var typingBG = new AppAreaBG(AppAreaBG.COLOR_TYPING_APP, 1,
			0, 60,
			GAME_SCREEN_SIZE.x, GAME_SCREEN_SIZE.y - 110
		);

		// korean app area
		// var koreanBG = new AppAreaBG(AppAreaBG.COLOR_TYPING_KOREAN, 1,
		// 	0, 60 + AppAreaBG.GAP_Y,
		// 	GAME_SCREEN_SIZE.x, 270
		// );
		// koreanBG.printText(
		// 	"한          글          타          자",
		// 	game.world.centerX, 320,
		// 	36, "#000", 0.05
		// );

		// english app area
		// var englishBG = new AppAreaBG(AppAreaBG.COLOR_TYPING_ENGLISH, 1,
		// 	0, 340,
		// 	GAME_SCREEN_SIZE.x, 270
		// );
		// englishBG.printText(
		// 	"영          문          타          자",
		// 	game.world.centerX, 370,
		// 	36, "#000", 0.05
		// );


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

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


		// typing tab buttons
		var typingPracticeTabButton = new TypingTabButton(
			TypingTabButton.PRACTICE_BUTTON_POS_X, TypingTabButton.BUTTON_POS_Y,
			"", "타자 연습",
			(function() {
				location.href = '../../web/client/menu_typing_practice.html';
			})
		);
		// typingPracticeTabButton.setInputEnabled(false);

		var typingTestTabButton = new TypingTabButton(
			TypingTabButton.TEST_BUTTON_POS_X, TypingTabButton.BUTTON_POS_Y,
			"", "타자 시험",
			(function() {
				location.href = '../../web/client/menu_typing_test.html';
			})
		);
		typingTestTabButton.setInputEnabled(false);


		this.animalRecordList = new AnimalRecordList();
		this.animalRecordList.printScore(AnimalRecordList.TYPE_TEST);

		this.makeActiveTypingTestAppButtons();


		// bottom ui
		var screenBottomUI = new ScreenBottomUI();
		// screenBottomUI.printLeftText("게임 진행 정보");
		screenBottomUI.printCenterText("메뉴 > 타자 시험");
		screenBottomUI.printRightText(sessionStorageManager.getPlayerName());
	},


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

	makeActiveTypingTestAppButtons: function() {
		var dbService = new DBService();
		dbService.setMaestroID(sessionStorageManager.getMaestroID());
		dbService.setPlayerID(sessionStorageManager.getPlayerID());

		dbService.requestWritingPlayerList(
			(jsonData) => {		// onSucceeded
				var writingArray = jsonData["writingArray"];
				// assert.equal(writingArray[0].name, "봄 - 윤동주", "name");
				// assert.equal(writingArray[3].name, "허생전 - 박지원", "name");
				// done();
				this.downloadListSucceeded(jsonData);
			},
			(jsonData) => {		// onFailed
				this.downloadListFailed(jsonData);
			}
		);
	},

	hasApp: function(appID, appList) {
		for(var i = 0; i < appList.length; i++) {
			if(appList[i].AppID === appID)
				return true;
		}

		return false;
	},

	getAppHighestRecord: function(jsonList, appID) {
		var count = jsonList.length;	// Object.keys(jsonList).length;

		for(var i = 0; i < count; i++) {
			if(jsonList[i].AppID === appID)
				return jsonList[i].AppHighestRecord;
		}

		return 0;
	},

	getAnimalSprite: function(jsonList, appID) {
		var appHighestRecord = this.getAppHighestRecord(jsonList, appID);
		var animalSprite = null;

		if(appHighestRecord === 0) {
			animalSprite = new Animal(Animal.TYPE_ICON, 0, 0, 0);
			// animalSprite.sprite.tint = RoundRectButtonSetting.DEFAULT_BUTTON_COLORS.disabled;
		} else {
			var animalLevel = Animal.animalLevelIDByRecord(appHighestRecord, Animal.TYPE_TEST);
			animalSprite = new Animal(Animal.TYPE_ICON, animalLevel, 0, 0);
		}

		return animalSprite;
	},

	downloadListSucceeded: function(replyJSON) {
		console.log(replyJSON);

		var writingArray = replyJSON["writingArray"];
		var writingCount = writingArray.length;

		var indexKoreanWriting = 0;
		var indexEnglishWriting = 0;
		for(var i = 0; i < writingCount; i++) {
			var writingData = writingArray[i];

			if(writingData.language === "korean") {
				var typingPracticeExamButton = new TypingExamButton(
					TypingExamButton.PRACTICE_BUTTON_POS_X, 200 + 100 * indexKoreanWriting,
					"", writingData.name,
					writingData.writingID
				);
				indexKoreanWriting++;
			} else {	// english
				var typingPracticeExamButton = new TypingExamButton(
					TypingTabButton.TEST_BUTTON_POS_X, 200 + 100 * indexEnglishWriting,
					"", writingData.name,
					writingData.writingID
				);
				indexEnglishWriting++;
			}
		}
	},

	downloadListFailed: function(replyJSON) {
		console.log('download app list failed, jsonData : ' + JSON.stringify(replyJSON));
	}

}