function InputScore(licenseDataManager, timer, chart) {
	this.licenseDataManager = licenseDataManager;
	this.timer = timer;
	this.chart = chart;

	this.dbConnectManager = new DBConnectManager();

	this.initVariables();

	Phaser.Device.whenReady(function () {
		game.plugins.add(PhaserInput.Plugin);
	});

	// bar
	var bar = game.add.graphics();
	bar.beginFill(0x303030); //, 0.2);
	bar.drawRect(0, this.inputScorePosY, GAME_SCREEN_SIZE.x, 70);


	// buttons
	this.subjectButton = this.makeSubjectButton();

	var subjectTextSetting = this.makeInputTextSetting(280, "과목 선택 or 직접 입력");
	this.subjectFullnameText = this.makeInputText(120, this.inputScorePosY + 10, subjectTextSetting);
	this.setSubjectFullname(this.subjectFullname);

	var titleTextStyle = { font: "36px Arial", fill: "#fff", align: "center"};
	var scoreTitleText = game.add.text(440, this.inputScorePosY + 40, "점수", titleTextStyle);
	scoreTitleText.anchor.set(0, 0.5);

	var scoreTextSetting = this.makeInputTextSetting(150, "점수 입력");
	scoreTextSetting.min = 0;
	scoreTextSetting.max = 9999;
	scoreTextSetting.type = PhaserInput.InputType.number;
	this.scoreText = this.makeInputText(game.world.centerX, this.inputScorePosY + 10, scoreTextSetting);

	var passwordTitleText = game.add.text(game.world.centerX + 190, this.inputScorePosY + 40, "마에", titleTextStyle);
	passwordTitleText.anchor.set(0, 0.5);

	var passwordTextSetting = this.makeInputTextSetting(110, "비밀번호");
	passwordTextSetting.type = PhaserInput.InputType.password;
	this.passwordText = this.makeInputText(game.world.centerX + 260, this.inputScorePosY + 10, passwordTextSetting);

	var saveButton = this.makeSaveButton();


	// comapny and subject panel + buttons
	this.makeCompanyPanel();
	this.makeSubjectPanel();
	this.setVisibleCompanyPanel(false);
	this.setVisibleSubjectPanel(false, "");
}

InputScore.prototype.initVariables = function() {
	this.inputScorePosY = GAME_SCREEN_SIZE.y - InputScore.INPUT_SCORE_GROUP_OFFSET_Y;

	this.subjectFullname = "";
	this.recentSubject = this.subjectFullname;

	this.companyButtonList = [];
	this.licenseSubjectButtonList = [];

	this.maestroPassword;
}

InputScore.prototype.loadDataFromServer = function(subjectName) {
	this.setSubjectFullname(subjectName);
	this.recentSubject = this.subjectFullname;
}

InputScore.prototype.makeInputTextSetting = function(width, placeHolder) {
	return {
		font: "32px Arial",
		fill: "#000000",
		fillAlpha: 1,
		fontWeight: "bold",
		forceCase: "",	// PhaserInput.ForceCase.upper,
		width: width,
		max: 100,
		padding: 8,
		borderWidth: 1,
		borderColor: "#aaa",
		borderRadius: 6,
		placeHolder: placeHolder,
		textAlign: "center",
		zoom: true
	}
}

InputScore.prototype.makeInputText = function(x, y, setting) {
	var inputText = game.add.inputField(x, y, setting);
	inputText.setText('');
	inputText.blockInput = false;

	return inputText;
}

InputScore.prototype.setSubjectFullname = function(fullname) {
	this.subjectFullname = fullname;
	// this.subjectFullnameText.text = fullname;
	this.subjectFullnameText.setText(this.subjectFullname);
}

InputScore.prototype.makeSubjectButton = function() {
	var setting = new RoundRectButtonSetting(60, this.inputScorePosY + 35, 100, 50);
	setting.strokeWidthPx = 5;
	setting.roundAmount = 10;
	setting.fontStyle.fontSize = 24;
	setting.fontStyle.fontWeight = "bold";

	// yellow
	setting.setStrokeColor(
		0xd0d020,
		0xb0b010,
		0x909000,
		0x333333
	);
	setting.setButtonColor(
		0xf0f040,
		0xd0d030,
		0xb0b020,
		0x666666
	);
	setting.setIconColor(
		0x906020,
		0x704010,
		0x502000,
		0x333333
	);
	setting.setTextColor(
		"#962",
		"#741",
		"#520",
		"#333"
	);

	var button = new RoundRectButton(
		setting,
		null, "과목",
		(function() { this.onClickSubjectButton(); }).bind(this)
	);

	return button;
}

InputScore.prototype.makeSaveButton = function() {
	var setting = new RoundRectButtonSetting(GAME_SCREEN_SIZE.x - 60, this.inputScorePosY + 35, 100, 50);

	// cyan
	setting.setStrokeColor(
		0x309090,
		0x208080,
		0x205050,
		0x666666
	);
	setting.setButtonColor(
		0x40c0c0,
		0x30a0a0,
		0x208080,
		0x666666
	);
	setting.setTextColor(
		"#cee",
		"#acc",
		"#8aa",
		"#333"
	);

	var button = new RoundRectButton(
		setting,
		null, "저장",
		(function() { this.onClickSendToServerButton(); }).bind(this)
	);
	return button;
}

InputScore.prototype.onClickSubjectButton = function() {
	if(!this.companyPanel.visible)
		this.setVisibleCompanyPanel(true);
	else {
		this.setVisibleCompanyPanel(false);
		this.setVisibleSubjectPanel(false, "");
	}
}



InputScore.prototype.makeCompanyPanel = function() {
	var companyCount = this.licenseDataManager.getCompanyCount();
	var buttonStackCount = Math.floor(companyCount / 7) + 1;
	var companyPanelWidth =
		InputScore.BUTTON_GAP_WIDTH
		+ (InputScore.COMPANY_BUTTON_WIDTH
		+ InputScore.BUTTON_GAP_WIDTH) * buttonStackCount;

	this.companyButtonStartPosX = InputScore.COMPANY_BUTTON_WIDTH / 2
		+ InputScore.BUTTON_GAP_WIDTH;

	// license company panel
	this.companyPanel = this.makePanelSprite(0, 300, companyPanelWidth, 398, 0x303030);


	var setting = new RoundRectButtonSetting(0, 0, 0, 0);
	setting.x = -100;
	setting.y = -100;
	setting.height = 50;
	setting.strokeWidthPx = 5;
	setting.roundAmount = 10;
	setting.fontStyle.fontSize = 24;
	setting.fontStyle.fontWeight = "bold";

	// license company buttons
	for(var i = 0; i < companyCount; i++) {
		setting.width = InputScore.COMPANY_BUTTON_WIDTH;

		var self = this;
		var companyName = this.licenseDataManager.getCompanyName(i);
		this.companyButtonList[i] = new RoundRectButton(
			setting, null, companyName,
			(function() { self.setVisibleSubjectPanel(true, this.text.text); })
		);
	}
}

InputScore.prototype.makeSubjectPanel = function() {
	var companyCount = this.licenseDataManager.getCompanyCount();
	var buttonStackCount = Math.floor(companyCount / 7) + 1;
	var companyPanelWidth =
		InputScore.BUTTON_GAP_WIDTH
		+ (InputScore.COMPANY_BUTTON_WIDTH
		+ InputScore.BUTTON_GAP_WIDTH) * buttonStackCount;

	// license subject panel
	this.subjectPanel = this.makePanelSprite(
		companyPanelWidth, 300,
		game.world.width - companyPanelWidth, 398, 0x202020);


	this.subjectButtonStartPosX =
		companyPanelWidth + InputScore.SUBJECT_BUTTON_WIDTH / 2
		+ InputScore.BUTTON_GAP_WIDTH;

	var setting = new RoundRectButtonSetting(0, 0, 0, 0);
	setting.x = -100;
	setting.y = -100;
	setting.height = 50;
	setting.strokeWidthPx = 5;
	setting.roundAmount = 10;
	setting.fontStyle.fontSize = 24;
	setting.fontStyle.fontWeight = "bold";

	// license subject buttons
	var subjectCount = this.licenseDataManager.getSubjectCount();
	for(var i = 0; i < subjectCount; i++) {
		setting.width = InputScore.SUBJECT_BUTTON_WIDTH;

		var subjectData = this.licenseDataManager.getSubjectDataByIndex(i);
		var subjectName = subjectData.companyName + " " + subjectData.subjectName;
		this.licenseSubjectButtonList[i] = new RoundRectButton(setting, null, subjectName, null);
	}
}

InputScore.prototype.setVisibleCompanyPanel = function(flag) {
	this.companyPanel.visible = flag;

	if(flag) {
		for(var i = 0; i < this.companyButtonList.length; i++) {
			this.companyButtonList[i].move(
				this.companyButtonStartPosX,
				this.inputScorePosY - 40 - InputScore.BUTTON_GAP_HEIGHT * i
			);
		}
	} else {
		for(var i = 0; i < this.companyButtonList.length; i++) {
			this.companyButtonList[i].move(-100, 900);
		}
	}
}

InputScore.prototype.getLicenseSubjectButtonPosX = function(index) {
	var rowCount = Math.floor(index / InputScore.SUBJECT_BUTTON_MAX_COUNT);
	return this.subjectButtonStartPosX
		+ (InputScore.SUBJECT_BUTTON_WIDTH + InputScore.BUTTON_GAP_WIDTH) * rowCount;
}

InputScore.prototype.getLicenseSubjectButtonPosY = function(index) {
	var colIndex = index % InputScore.SUBJECT_BUTTON_MAX_COUNT;
	return this.inputScorePosY - 40 - InputScore.BUTTON_GAP_HEIGHT * colIndex;
}

InputScore.prototype.setVisibleSubjectPanel = function(flag, companyName) {
	this.subjectPanel.visible = flag;

	for(var i = 0; i < this.licenseSubjectButtonList.length; i++) {
		this.licenseSubjectButtonList[i].move(-100, 900);
	}

	if(!flag)
		return;

	var buttonIndex = 0;
	var subjectCount = this.licenseDataManager.getSubjectCount();
	for(var i = 0; i < subjectCount; i++) {
		var subjectData = this.licenseDataManager.getSubjectDataByIndex(i);

		if(subjectData.companyName == companyName) {
			var button = this.licenseSubjectButtonList[buttonIndex];

			var buttonName = companyName + " " + subjectData.subjectName;
			button.text.text = buttonName;
			button.move(
				this.getLicenseSubjectButtonPosX(buttonIndex),
				this.getLicenseSubjectButtonPosY(buttonIndex)
			);
			var self = this;
			button.clickEvent = (function() {
				self.onClickCompanySubjectButton(this.text.text);
			});

			buttonIndex++;
		}
	}
}

InputScore.prototype.onClickCompanySubjectButton = function(subjectFullname) {
	this.setSubjectFullname(subjectFullname);
	this.selectedSubjectData = this.licenseDataManager.getSubjectDataByName(subjectFullname);
	// console.log(this.selectedSubjectData);

	this.timer.setSubjectName(this.subjectFullname);
	this.timer.setStartTime(this.selectedSubjectData.timeLimit * Timer.TIME_SECONDS_FOR_MINUTE);

	this.setVisibleCompanyPanel(false);
	this.setVisibleSubjectPanel(false, "");
}


InputScore.prototype.makePanelSprite = function(x, y, width, height, color) {
	var btnTexture = new Phaser.Graphics()
		.beginFill(color, 0.8)
		.drawRect(0, 0, width, height)
		.endFill()
		.generateTexture();

	return game.add.sprite(x, y, btnTexture);
}

InputScore.prototype.showAlertWindow = function(message) {
	alert(message);
}

InputScore.prototype.onClickSendToServerButton = function() {
	if(this.subjectFullname == "") {
		this.showAlertWindow("과목을 입력해 주세요.");
		return;
	}

	if(this.scoreText.value == "") {
		this.showAlertWindow("점수를 입력해 주세요.");
		return;
	}

	if(sessionStorageManager.getMaestroID() == 1 && sessionStorageManager.getPlayerID() == 1 && this.passwordText.value != "1111") {
		this.showAlertWindow("체험용 마에스트로 비밀번호는 1111 입니다.");
		return;
	} if(this.passwordText.value == "") {
		this.showAlertWindow("비밀번호를 입력해 주세요.\n마에스트로 관리자 페이지에 가서, 자격증 타이머용 비밀번호를 확인하고 입력해 주세요.");
		return;
	}
	this.maestroPassword = this.passwordText.value;

	// console.log("send data to server");
	this.checkMaestroPasswordFromServer();
}



InputScore.prototype.addScoreToServer = function(subjectName, score) {
	this.dbConnectManager.addLicenseScore(
		sessionStorageManager.getMaestroID(),
		sessionStorageManager.getPlayerID(),
		subjectName,
		score,
		(function(replyJson) {
			console.log(replyJson);

			this.scoreText.setText("");
			// this.showAlertWindow("점수가 정상적으로 등록되었습니다.");

			this.chart.loadScoreFromServer();
		}).bind(this),

		(function(replyJson) {
			console.log(replyJson);
		})
	);
}

InputScore.prototype.checkMaestroPasswordFromServer = function() {
	this.dbConnectManager.requestLicenseMaestroPassword(
		sessionStorageManager.getMaestroID(),
		(function(replyJson) {
			// console.log(replyJson);

			if(replyJson.maestroPassword == null || replyJson.maestroPassword == "") {
				this.showAlertWindow("자격증 타이머용 비밀번호가 설정되지 않았습니다.\n마에스트로 관리자 페이지에 가서, 자격증 타이머용 비밀번호를 설정해 주세요.");
				return;
			} else if(this.maestroPassword != replyJson.maestroPassword) {
				this.showAlertWindow("비밀번호가 틀렸습니다.\n마에스트로 관리자 페이지에 가서, 자격증 타이머용 비밀번호를 확인해 주세요.");
				return;
			}

			this.addScoreToServer(this.subjectFullname, this.scoreText.value);
		}).bind(this),

		(function(replyJson) {
			// console.log(replyJson);
			this.maestroPassword = InputScore.DEFAULT_MAESTRO_PASSWORD;
		})
	);
}

InputScore.INPUT_SCORE_GROUP_OFFSET_Y	= 70;

InputScore.COMPANY_BUTTON_WIDTH	= 160;
InputScore.SUBJECT_BUTTON_WIDTH	= 260;

InputScore.BUTTON_GAP_WIDTH			= 10;
InputScore.BUTTON_GAP_HEIGHT		= 64;
InputScore.SUBJECT_BUTTON_MAX_COUNT	= 6;

InputScore.DEFAULT_MAESTRO_PASSWORD = "1111";