var Setup = {

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

	create: function() {
		this.game.stage.backgroundColor = '#4d4d4d';
		this.textStyle = { font: "32px Arial", fill: "#fff", align: "right", boundsAlignH: "right", boundsAlignV: "middle" };

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

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


		var textStartY = 240;
		this.makePrevEnterCodeText(game.world.centerX, textStartY);
		this.makeNewEnterCodeInput(game.world.centerX, textStartY + 60);
		this.makeEnterCodeHint(game.world.centerX, textStartY + 100);

		this.startButton = this.makeCheckEnterCodeButton(game.world.centerX, textStartY + 200);

		this.makeInfoText();
		this.infoText.text = "새로운 입장번호를 입력하신 후, [ 변경 ] 버튼을 눌러주세요.";


		// bottom ui
		var screenBottomUI = new ScreenBottomUI();
		// ScreenBottomUI.printLeftText("게임 진행 정보");
		// var playingAppName = appInfoManager.getAppNameKorean(sessionStorageManager.playingAppName);
		// ScreenBottomUI.printCenterText(playingAppName);
		screenBottomUI.printCenterText("설정");
		screenBottomUI.printRightText(sessionStorageManager.getPlayerName());

		this.loadPrevEnterCode();
	},

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

	makePrevEnterCodeText: function(x, y) {
		var title = this.makeTextField(x - 10, y, "현재 입장번호 :", this.textStyle);
		title.anchor.x = 1;
		this.prevEnterCodeText = this.makeTextField(x + 10, y, "", this.textStyle);
	},

	makeNewEnterCodeInput: function(x, y) {
		var title = this.makeTextField(x - 10, y, "바꿀 입장번호 :", this.textStyle);
		title.anchor.x = 1;
		this.inputTextEnterCode = this.makeInputTypeText(x + 10 - 220, y, "");
		this.inputTextEnterCode.canvasInput.placeHolder("새로운 입장번호");
	},

	makeEnterCodeHint: function(x, y) {
		var textStyle = { font: "18px Arial", fill: "#0dd" };
		var hint = this.makeTextField(x + 10, y, "최소 2개 ~ 최대 8개의 숫자로 입력하세요.", textStyle);
		hint.anchor.x = 0;
	},



	makeTextField: function(x, y, text, textStyle) {
		var textfield = game.add.text(x, y, text, textStyle)
			.setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2);
		textfield.anchor.x = 0;
		textfield.anchor.y = 0.5;

		return textfield;
	},

	makeCheckEnterCodeButton: function(x, y) {
		var setting = new RoundRectButtonSetting(x, y, 200, 100);
		setting.fontStyle.fontWeight = "bold";

		var checkButton = new RoundRectButton(
			setting,
			RoundRectButton.NONE_ICON, "변경",
			(function() { this.checkNewEnterCode(); }).bind(this)
		);
		checkButton.addShortcutText("Enter");

		return checkButton;
	},

	makeInputTypeText: function(x, y, text) {
		var inputText = new InputTypeText(x + 420, y);
		inputText.anchor.set(0.5);
		inputText.canvasInput.value('');
		if(isDebugMode()) {
			inputText.canvasInput.value(text);
		}
		inputText.canvasInput._onkeyup = (function() {
			if(event.keyCode == Phaser.Keyboard.ENTER) {
				this.checkNewEnterCode();
			}
		}).bind(this);

		return inputText;
	},



	makeInfoText: function(x, y) {
		var textStyle = { font: "30px Arial", fill: "#faa" };
		this.infoText = game.add.text(game.world.centerX, 640, "", textStyle);
		this.infoText.anchor.set(0.5);
		this.infoText.stroke = "#333";
		this.infoText.strokeThickness = 3;
	},

	showInfoText: function(message) {
		this.infoText.text = message;
	},


	checkNewEnterCode: function() {
		var newEnterCode = this.inputTextEnterCode.canvasInput._value;
		if(newEnterCode == this.prevEnterCode) {
			this.showInfoText("기존 입장번호와 동일합니다.");
			return;
		}

		var accountValidator = new AccountValidator();
		if(!accountValidator.isValidPlayerPW(newEnterCode)) {
			var errorMessage = accountValidator.messageForInvalidPlayerPW(newEnterCode);
			this.showInfoText(errorMessage);
			this.inputTextEnterCode.canvasInput.focus();
			return;
		}

		this.showInfoText("입장번호를 바꾸겠습니다.");
		var dbConnectManager = new DBConnectManager();
		dbConnectManager.updatePlayerEnterCode(
			sessionStorageManager.getMaestroID(),
			sessionStorageManager.getPlayerID(),
			newEnterCode,
			(function(jsonData) {
				// this.loginSucceeded(jsonData);
				this.showInfoText("새로 입력하신 [ " + jsonData["EnterCode"] + " ]로 입장번호가 변경되었습니다.")
				this.loadPrevEnterCode();
			}).bind(this),
			(function(jsonData) {
				// this.loginFailed(jsonData);
				this.showInfoText(jsonData["error"]);
			}).bind(this)
		);
	},

	loadPrevEnterCode: function() {
		this.prevEnterCode = '';

		var dbConnectManager = new DBConnectManager();
		dbConnectManager.requestPlayerEnterCode(
			sessionStorageManager.getMaestroID(),
			sessionStorageManager.getPlayerID(),
			(function(jsonData) {
				this.prevEnterCode = jsonData['EnterCode'];
				this.prevEnterCodeText.text = this.prevEnterCode;
			}).bind(this),
			(function(jsonData) {
				this.showInfoText(jsonData['error']);
				// this.statButton.buttonDisabled();
			}).bind(this)
		);
	},

}