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

function MainMenu() {
}

MainMenu.prototype.preload = function() {
	game.load.image('tile_choco',	'../../../resources/image/background/tile_choco.png');
	game.load.image('home',	'../../../resources/image/icon/home.png');

	Animal.loadResources();

	AppGroupTab.loadResources();
	TypingAppButton.loadResources();
	PlayAppButton.loadResources();

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

MainMenu.prototype.create = function() {
	this.dbService = new DBService();
	this.dbService.setMaestroID(sessionStorageManager.getMaestroID());
	this.dbService.setPlayerID(sessionStorageManager.getPlayerID());

	// bg
	var graphics = game.add.graphics();
	graphics.beginFill(MainColor.LIGHT_CHOCO_HEX, 1);
	graphics.drawRect(0, 0,GAME_SCREEN_SIZE.x, GAME_SCREEN_SIZE.y);
	// graphics.beginFill(MainColor.BLACK_HEX, 1);
	graphics.beginFill(MainColor.CHOCO_HEX, 1);
	graphics.drawRect(0, 0, GAME_SCREEN_SIZE.x, MainMenu.MAIN_MENU_HEIGHT);

	this.appGroupTabLayout = null;
	this.typingPracticeGroupTab = null;
	this.typingTestGroupTab = null;
	this.typingExamGroupTab = null;
	this.typingPlayGroupTab = null;
	this.mousePlayGroupTab = null;

	this.languageTabLayout = null;
	this.koreanTab = null;
	this.englishTab = null;

	this.typingAppButtons = new Array();
	this.makeTypingAppButtons();
	this.typingExamButtons = new Array();
	this.makeTypingExamButtons();
	this.playAppButtons = new Array();
	this.makePlayAppButtons();

	// this.selectedAppGroup = MainMenu.APP_GROUP_TYPING_EXAM;
	// this.animalRecordList.printScore(Animal.TYPE_PRACTICE);
	if(sessionStorageManager.getAppGroup() === null)
		this.selectedAppGroup = MainMenu.APP_GROUP_TYPING_PRACTICE;
	else
		this.selectedAppGroup = parseInt(sessionStorageManager.getAppGroup());
	if(sessionStorageManager.getLanguage() === null)
		this.selectedLanguage = MainMenu.LANGUAGE_KOREAN;
	else
		this.selectedLanguage = parseInt(sessionStorageManager.getLanguage());
}

MainMenu.prototype.fontLoaded = function() {

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

	// top UI
	this.backButton = new BackButton(
		(function() { this.back(); }).bind(this)
	);
	this.backButton.applyLoadedFont();

	this.makeAppGroupTab();
	this.makeLanguageTab();
	this.appGroupTabLayout.applyLoadedFont();

	this.languageTabLayout.hide();
	this.languageTabLayout.applyLoadedFont();

	this.makeAnimalRecordList();
	this.animalRecordList.hide();
	this.animalRecordList.applyLoadedFont();


	// welcome message
	new WelcomePlayerText(sessionStorageManager.getPlayerName());
	if(isExperiencePlayerAccount()) {
		game.time.events.add(Phaser.Timer.SECOND * 2.5,
			(function() { new NoticeExperiencePlayerText(); } ),
			this
		);
	}

	this.activeSavedAppGroupTab();
	this.activeSavedLanguageTab();
	// this.loadAppData();

	this.loadAllowEditEnterCode();
}



MainMenu.prototype.back = function() {
	sessionStorageManager.clear();
	location.href = '../../web/client/login.html';
}

MainMenu.prototype.loadAllowEditEnterCode = function() {
	this.dbService.requestAllowEditEnterCode(
		(function(replyJSON) {
			if(parseInt(replyJSON.allowEditEntercode) === 1)
				this.makeSettingButton();
		}).bind(this),
		(function(replyJSON) { console.log(replyJSON); }).bind(this)
	);
}

MainMenu.prototype.loadAppData = function() {
	this.dbService.requestMainMenuAppData(
		this.selectedAppGroup,
		this.selectedLanguage,
		(function(replyJSON) {
			// this.updateAppGroupTab(replyJSON.appGroupData);
			this.updateAppButtons(replyJSON.appGroup, replyJSON.appData);
			// this.updateLanguageTab(replyJSON.appGroup);
		}).bind(this),
		(function(replyJSON) { console.log(replyJSON); }).bind(this)
	);
}



MainMenu.prototype.makeTypingAppButtons = function() {
	var offsetX = 95;
	var startY = 190;
	var offsetY = 110;

	var buttonHalfWidth = TypingAppButton.WIDTH / 2;
	for(var i = 0; i < MainMenu.TYPING_APP_COUNT; i++) {
		var x = (i % 2) === 0 ? buttonHalfWidth + offsetX : GAME_SCREEN_SIZE.x - buttonHalfWidth - offsetX;
		var y = startY + Math.floor(i / 2) * offsetY;

		var button = new TypingAppButton(x, y);
		button.setActive(false);
		this.typingAppButtons.push(button);
	}
}

MainMenu.prototype.makeTypingExamButtons = function() {
	var offsetX = 95;
	var startY = 190;
	var offsetY = 110;

	var buttonHalfWidth = TypingExamButton.WIDTH / 2;
	for(var i = 0; i < MainMenu.TYPING_APP_COUNT; i++) {
		var x = (i % 2) === 0 ? buttonHalfWidth + offsetX : GAME_SCREEN_SIZE.x - buttonHalfWidth - offsetX;
		var y = startY + Math.floor(i / 2) * offsetY;

		var button = new TypingExamButton(x, y);
		button.setActive(false);
		this.typingExamButtons.push(button);
	}
}

MainMenu.prototype.makePlayAppButtons = function() {
	var startX = 150;
	var startY = 300;
	var offsetX = PlayAppButton.WIDTH + 30;
	var offsetY = 250;

	var buttonHalfWidth = TypingAppButton.WIDTH / 2;
	for(var i = 0; i < MainMenu.PLAY_APP_COUNT; i++) {
		var x = startX + (i % 5) * offsetX;
		var y = Math.floor(i / 5) === 0 ? startY : startY + offsetY;

		var button = new PlayAppButton(x, y);
		button.setActive(false);
		this.playAppButtons.push(button);
	}
}

MainMenu.prototype.hideAllButtons = function() {
	// hide all buttons

	for(var i = 0; i < MainMenu.TYPING_APP_COUNT; i++) {
		this.typingAppButtons[i].setActive(false);
	}

	for(var i = 0; i < MainMenu.TYPING_EXAM_COUNT; i++) {
		this.typingExamButtons[i].setActive(false);
	}

	for(var i = 0; i < MainMenu.PLAY_APP_COUNT; i++) {
		this.playAppButtons[i].setActive(false);
	}
}

MainMenu.prototype.updateAppButtons = function(appGroup, appData) {
	this.hideAllButtons();

	switch(parseInt(appGroup)) {
		case MainMenu.APP_GROUP_TYPING_PRACTICE:
		this.updateTypingPracticeAppButtons(appGroup, appData);
		break;

		case MainMenu.APP_GROUP_TYPING_TEST:
		this.updateTypingTestAppButtons(appGroup, appData);
		break;

		case MainMenu.APP_GROUP_TYPING_EXAM:
		this.updateTypingExamAppButtons(appData);
		break;

		case MainMenu.APP_GROUP_TYPING_PLAY:
		this.updatePlayAppButtons(appData);
		break;

		case MainMenu.APP_GROUP_MOUSE_PLAY:
		this.updatePlayAppButtons(appData);
		break;

		default:
		console.log(appGroup);
	}
}

MainMenu.prototype.isActivatedApp = function(appID, activeAppList) {
	var count = activeAppList.length;
	for(var i = 0; i < count; i++) {
		if(appID === activeAppList[i].appID)
			return true;
	}

	return false;
}

MainMenu.prototype.isActivatedWriting = function(writingID, activeAppList) {
	var count = activeAppList.length;
	for(var i = 0; i < count; i++) {
		if(writingID === activeAppList[i].writingID)
			return true;
	}

	return false;
}

MainMenu.prototype.getAppHighestRecord = function(appID, highestRecordList) {
	var count = highestRecordList.length;
	for(var i = 0; i < count; i++) {
		if(appID === highestRecordList[i].appID)
			return highestRecordList[i].highestRecord;
	}

	return 0;
}

MainMenu.prototype.getWritingHighestRecord = function(writingID, highestRecordList) {
	var count = highestRecordList.length;
	for(var i = 0; i < count; i++) {
		if(writingID === highestRecordList[i].writingID)
			return highestRecordList[i].highestRecord;
	}

	return 0;
}

MainMenu.prototype.makeTypingPracticeAppDataList = function(appGroup, appData) {
	var appDataList = new Array();

	var appList = appData.appList;
	if(appList.length === 0)
		return [];

	var count = appList.length;
	for(var appDataIndex = 0; appDataIndex < count; appDataIndex++) {
		var data = {};
		data["appGroup"] = appGroup;
		data["appID"] = appList[appDataIndex].appID;
		data["appName"] = appList[appDataIndex].appName;
		data["koreanName"] = appList[appDataIndex].koreanName;
		data["activated"] = this.isActivatedApp(appList[appDataIndex].appID, appData.activeAppList);
		data["highestRecord"] = this.getAppHighestRecord(appList[appDataIndex].appID, appData.highestRecordList);
		appDataList.push(data);
	}

	return appDataList;
}

MainMenu.prototype.makeTypingExamAppDataList = function(appData) {
	var appDataList = new Array();

	var appList = appData.appList;
	if(appList.length === 0)
		return [];

	var count = appList.length;
	for(var appDataIndex = 0; appDataIndex < count; appDataIndex++) {
		var data = {};
		data["writingID"] = appList[appDataIndex].writingID;
		data["name"] = appList[appDataIndex].name;
		data["filename"] = appList[appDataIndex].filename;
		data["letterCount"] = appList[appDataIndex].letterCount;
		data["activated"] = this.isActivatedWriting(appList[appDataIndex].writingID, appData.activeAppList);
		data["highestRecord"] = this.getWritingHighestRecord(appList[appDataIndex].writingID, appData.highestRecordList);
		appDataList.push(data);
	}

	return appDataList;
}

MainMenu.prototype.makePlayAppDataList = function(appData) {
	var appDataList = new Array();

	var appList = appData.appList;
	if(appList.length === 0)
		return [];

	var count = appList.length;
	for(var appDataIndex = 0; appDataIndex < count; appDataIndex++) {
		var data = {};
		data["appID"] = appList[appDataIndex].appID;
		data["appName"] = appList[appDataIndex].appName;
		data["koreanName"] = appList[appDataIndex].koreanName;
		data["activated"] = this.isActivatedApp(appList[appDataIndex].appID, appData.activeAppList);
		// data["highestRecord"] = this.getAppHighestRecord(appList[appDataIndex].appID, appData.highestRecordList);
		appDataList.push(data);
	}

	return appDataList;
}

MainMenu.prototype.updateTypingPracticeAppButtons = function(appGroup, appData) {
	// console.log("updateTypingPracticeAppButtons");

	var buttonIndex = 0;
	var appDataList = this.makeTypingPracticeAppDataList(appGroup, appData);
	var count = appDataList.length;
	for(var i = 0; i < MainMenu.TYPING_APP_COUNT; i++) {
		var button = this.typingAppButtons[i];
		if(i < count) {
			button.setActive(true);
			button.updateAppData(appDataList[i]);
		} else {
			button.setActive(true);
			button.showEmptyDisabled();
		}
	}
}

MainMenu.prototype.updateTypingTestAppButtons = function(appGroup, appData) {
	// console.log("updateTypingTestAppButtons");

	var buttonIndex = 0;
	var appDataList = this.makeTypingPracticeAppDataList(appGroup, appData);
	var count = appDataList.length;
	for(var i = 0; i < MainMenu.TYPING_APP_COUNT; i++) {
		var button = this.typingAppButtons[i];
		if(i < count) {
			button.setActive(true);
			button.updateAppData(appDataList[i]);
		} else {
			button.setActive(true);
			button.showEmptyDisabled();
		}
	}
}

MainMenu.prototype.updateTypingExamAppButtons = function(appData) {
	// console.log("updateTypingExamAppButtons");

	var buttonIndex = 0;
	var appDataList = this.makeTypingExamAppDataList(appData);
	var count = appDataList.length;
	for(var i = 0; i < MainMenu.TYPING_APP_COUNT; i++) {
		var button = this.typingExamButtons[i];
		if(i < count) {
			button.setActive(true);
			button.updateAppData(appDataList[i]);
			// console.log(appDataList[i]);
			if(isExperiencePlayerAccount() === true) {
				var splitedFilename = appDataList[i].filename.split("_");
				var language = splitedFilename[0];
				if(language === "english") {
					button.setInputEnabled(false);
				}
			}
		} else {
			button.setActive(true);
			button.showEmptyDisabled();
		}
	}
}

MainMenu.prototype.updatePlayAppButtons = function(appData) {
	// console.log("updatePlayAppButtons");

	var buttonIndex = 0;
	var appDataList = this.makePlayAppDataList(appData);
	var count = appDataList.length;
	for(var i = 0; i < MainMenu.PLAY_APP_COUNT; i++) {
		var button = this.playAppButtons[i];
		if(i < count) {
			button.setActive(true);
			button.updateAppData(appDataList[i]);
		} else {
			button.setActive(true);
			button.showEmptyDisabled();
		}
	}
}


MainMenu.prototype.makeSettingButton = function() {
	this.settingButton = new SettingButton(
		(function() { location.href = '../../web/client/setup.html'; }).bind(this)
	);
	this.settingButton.applyLoadedFont();
}

MainMenu.prototype.activeSavedAppGroupTab = function() {
	switch(this.selectedAppGroup) {
		case MainMenu.APP_GROUP_TYPING_PRACTICE:
		this.appGroupTabLayout.selectedTab(this.typingPracticeGroupTab);
		this.onClickTypingPracticeGroupTab();
		break;

		case MainMenu.APP_GROUP_TYPING_TEST:
		this.appGroupTabLayout.selectedTab(this.typingTestGroupTab);
		this.onClickTypingTestGroupTab();
		break;

		case MainMenu.APP_GROUP_TYPING_EXAM:
		this.appGroupTabLayout.selectedTab(this.typingExamGroupTab);
		this.onClickTypingExamGroupTab();
		break;

		case MainMenu.APP_GROUP_TYPING_PLAY:
		this.appGroupTabLayout.selectedTab(this.typingPlayGroupTab);
		this.onClickTypingPlayGroupTab();
		break;

		case MainMenu.APP_GROUP_MOUSE_PLAY:
		this.appGroupTabLayout.selectedTab(this.mousePlayGroupTab);
		this.onClickMousePlayGroupTab();
		break;

		default:
		// console.log("appGroup - default");
		break;
	}
}

MainMenu.prototype.activeSavedLanguageTab = function() {
	switch(this.selectedLanguage) {
		case MainMenu.LANGUAGE_KOREAN:
		this.languageTabLayout.selectedTab(this.koreanTab);
		break;

		case MainMenu.LANGUAGE_ENGLISH:
		this.languageTabLayout.selectedTab(this.englishTab);
		break;

		default:
		// console.log("language - default");
		break;
	}
}

MainMenu.prototype.onClickTypingPracticeGroupTab = function() {
	sessionStorageManager.setAppGroup(MainMenu.APP_GROUP_TYPING_PRACTICE);
	this.selectedAppGroup = MainMenu.APP_GROUP_TYPING_PRACTICE;
	this.animalRecordList.show();
	this.animalRecordList.printScore(Animal.TYPE_PRACTICE);
	this.languageTabLayout.show();
	this.loadAppData();
}

MainMenu.prototype.onClickTypingTestGroupTab = function() {
	sessionStorageManager.setAppGroup(MainMenu.APP_GROUP_TYPING_TEST);
	this.selectedAppGroup = MainMenu.APP_GROUP_TYPING_TEST;
	this.animalRecordList.show();
	this.animalRecordList.printScore(Animal.TYPE_TEST);
	this.languageTabLayout.show();
	this.loadAppData();
}

MainMenu.prototype.onClickTypingExamGroupTab = function() {
	sessionStorageManager.setAppGroup(MainMenu.APP_GROUP_TYPING_EXAM);
	this.selectedAppGroup = MainMenu.APP_GROUP_TYPING_EXAM;
	this.animalRecordList.show();
	this.animalRecordList.printScore(Animal.TYPE_EXAM);
	this.languageTabLayout.show();
	this.loadAppData();
}

MainMenu.prototype.onClickTypingPlayGroupTab = function() {
	sessionStorageManager.setAppGroup(MainMenu.APP_GROUP_TYPING_PLAY);
	this.selectedAppGroup = MainMenu.APP_GROUP_TYPING_PLAY;
	this.animalRecordList.hide();
	this.languageTabLayout.show();
	this.loadAppData();
}

MainMenu.prototype.onClickMousePlayGroupTab = function() {
	sessionStorageManager.setAppGroup(MainMenu.APP_GROUP_MOUSE_PLAY);
	this.selectedAppGroup = MainMenu.APP_GROUP_MOUSE_PLAY;
	this.animalRecordList.hide();
	this.languageTabLayout.hide();
	this.loadAppData();
}

MainMenu.prototype.makeAppGroupTab = function() {
	this.appGroupTabLayout = new TabLayout(100, 0, 844, 70);

	this.typingPracticeGroupTab = new AppGroupTab(
		(function() { this.onClickTypingPracticeGroupTab(); }).bind(this)
	);
	this.typingPracticeGroupTab.reserveIcon("keyboard");
	this.typingPracticeGroupTab.reserveSubText("손가락 연습");
	this.appGroupTabLayout.add(this.typingPracticeGroupTab);

	this.typingTestGroupTab = new AppGroupTab(
		(function() { this.onClickTypingTestGroupTab(); }).bind(this)
	);
	this.typingTestGroupTab.reserveIcon("keyboard");
	this.typingTestGroupTab.reserveSubText("단어 / 문장 연습");
	this.appGroupTabLayout.add(this.typingTestGroupTab);

	this.typingExamGroupTab = new AppGroupTab(
		(function() { this.onClickTypingExamGroupTab(); }).bind(this)
	);
	this.typingExamGroupTab.reserveIcon("keyboard");
	this.typingExamGroupTab.reserveSubText("긴글 (시험)");
	this.appGroupTabLayout.add(this.typingExamGroupTab);

	/*
	this.typingCompetitionGroupTab = new AppGroupTab(
		(function() { this.onClickTypingExamGroupTab(); }).bind(this)
	);
	this.typingCompetitionGroupTab.reserveIcon("keyboard");
	this.typingCompetitionGroupTab.reserveSubText("타자 대회");
	this.appGroupTabLayout.add(this.typingCompetitionGroupTab);
	*/

	this.typingPlayGroupTab = new AppGroupTab(
		(function() { this.onClickTypingPlayGroupTab(); }).bind(this)
	);
	this.typingPlayGroupTab.reserveIcon("keyboard");
	this.typingPlayGroupTab.reserveSubText("타자 놀이");
	this.appGroupTabLayout.add(this.typingPlayGroupTab);

	this.mousePlayGroupTab = new AppGroupTab(
		(function() { this.onClickMousePlayGroupTab(); }).bind(this)
	);
	this.mousePlayGroupTab.reserveIcon("mouse");
	this.mousePlayGroupTab.reserveSubText("마우스 놀이");
	this.appGroupTabLayout.add(this.mousePlayGroupTab);

	this.appGroupTabLayout.createTabs();

	// this.typingPracticeGroupTab.setInputEnabled(false);
	// this.typingTestGroupTab.setInputEnabled(false);
	// this.typingExamGroupTab.setInputEnabled(false);
	// this.typingCompetitionGroupTab.setInputEnabled(false);
	// this.typingPlayGroupTab.setInputEnabled(false);
	// this.mousePlayGroupTab.setInputEnabled(false);
}

MainMenu.prototype.makeAnimalRecordList = function() {
	this.animalRecordList = new AnimalRecordList(AnimalRecordList.TYPE_MENU);
	this.animalRecordList.printScore(Animal.TYPE_PRACTICE);
}

MainMenu.prototype.makeLanguageTab = function() {
	var languageTabHeight = 48;
	// this.languageTabLayout = new TabLayout(0, GAME_SCREEN_SIZE.y - languageTabHeight, GAME_SCREEN_SIZE.x, languageTabHeight);
	this.languageTabLayout = new TabLayout(0, 70, GAME_SCREEN_SIZE.x, languageTabHeight);

	this.koreanTab = new LanguageTab(
		(function() {
			// console.log("Language : Korean");
			sessionStorageManager.setLanguage(MainMenu.LANGUAGE_KOREAN);
			this.selectedLanguage = MainMenu.LANGUAGE_KOREAN;
			this.loadAppData();
		}).bind(this)
	);
	this.koreanTab.reserveMainText("한글");
	this.languageTabLayout.add(this.koreanTab);

	this.englishTab = new LanguageTab(
		(function() {
			// console.log("Language : English");
			sessionStorageManager.setLanguage(MainMenu.LANGUAGE_ENGLISH);
			this.selectedLanguage = MainMenu.LANGUAGE_ENGLISH;
			this.loadAppData();
		}).bind(this)
	);
	this.englishTab.reserveMainText("영어");
	this.languageTabLayout.add(this.englishTab);

	this.languageTabLayout.createTabs();
}



MainMenu.prototype.loadFailed = function(replyJSON) {
	// console.log('login failed, jsonData : ' + JSON.stringify(replyJSON));
}


MainMenu.MAIN_MENU_HEIGHT		= 70;

MainMenu.APP_GROUP_TYPING_PRACTICE	= 0;
MainMenu.APP_GROUP_TYPING_TEST		= 1;
MainMenu.APP_GROUP_TYPING_EXAM		= 2;
MainMenu.APP_GROUP_TYPING_PLAY		= 3;
MainMenu.APP_GROUP_MOUSE_PLAY		= 4;

MainMenu.LANGUAGE_KOREAN	= 0;
MainMenu.LANGUAGE_ENGLISH	= 1;

MainMenu.TYPING_APP_COUNT	= 10;
MainMenu.TYPING_EXAM_COUNT	= 10;
MainMenu.PLAY_APP_COUNT		= 10;