TypingExamButton.prototype = Object.create(BasicButton.prototype);
TypingExamButton.constructor = TypingExamButton;

function TypingExamButton(x, y) {
	this.appData = null;

	this.appType = Animal.TYPE_PRACTICE;
	this.writingID = -1;
	this.filename = "";

	var setting = new ButtonSetting(x, y, TypingExamButton.WIDTH, TypingExamButton.HEIGHT);
	setting.setStrokeWidth(0);
	setting.setRound(30);
	// setting.setFont("Nanum Brush Script");
	setting.setFont("Nanum Gothic");
	setting.setFontSize(20);
	setting.fontStyle.boundsAlignH = "center";	// left, center. right
	setting.fontStyle.boundsAlignV = "middle";	// top, middle, bottom
	setting.setStrokeColor(
		TypingExamButton.STROKE_COLOR_OUT_HEX,
		TypingExamButton.STROKE_COLOR_OVER_HEX,
		TypingExamButton.STROKE_COLOR_DOWN_HEX,
		TypingExamButton.STROKE_COLOR_DISABLED_HEX
	);
	setting.setButtonColor(
		TypingExamButton.BUTTON_COLOR_OUT_HEX,
		TypingExamButton.BUTTON_COLOR_OVER_HEX,
		TypingExamButton.BUTTON_COLOR_DOWN_HEX,
		TypingExamButton.BUTTON_COLOR_DISABLED_HEX
	);
	setting.setIconColor(
		TypingExamButton.BUTTON_COLOR_OUT_HEX,
		TypingExamButton.BUTTON_COLOR_OVER_HEX,
		TypingExamButton.BUTTON_COLOR_DOWN_HEX,
		TypingExamButton.BUTTON_COLOR_DISABLED_HEX
	);
	setting.setTextColor(
		TypingExamButton.TEXT_COLOR_OUT_STRING,
		TypingExamButton.TEXT_COLOR_OVER_STRING,
		TypingExamButton.TEXT_COLOR_DOWN_STRING,
		TypingExamButton.TEXT_COLOR_DISABLED_STRING
	);

	BasicButton.call(this, setting, this.clickEvent);

	this.mainText.anchor.set(0, 0.5);
	this.mainText.x = 100;
	// this.mainText.width = 100;
	// this.setMainText("");
	// this.addIcon("tile_choco");
	// this.setIconSize(40);
	// this.setShortcutText("[단축키]");

	this.letterCountText = this.makeLetterCountText();
	this.buttonSprite.addChild(this.letterCountText);

	this.writingNameText = this.makeWritingNameText();
	this.buttonSprite.addChild(this.writingNameText);

	var animalPosX = TypingExamButton.WIDTH - Animal.SPRITE_WIDTH / 2;
	var animalPosY = TypingExamButton.HEIGHT / 2 + TypingExamButton.ANIMAL_OFFSET_Y;
	this.animal = new Animal(Animal.TYPE_ICON, 0, animalPosX, animalPosY);
	this.buttonSprite.addChild(this.animal.sprite);

	this.typingRecordText = this.makeTypingRecordText(animalPosX, animalPosY + 32);
	this.buttonSprite.addChild(this.typingRecordText);
}


TypingExamButton.prototype.makeLetterCountText = function() {
	var letterCountText = game.add.text(20, this.mainText.y, "");
	letterCountText.anchor.set(0, 0.5);
	letterCountText.font = this.setting.font;
	letterCountText.fontSize = 13;
	letterCountText.style.fill = MainColor.LIGHT_CHOCO_STRING;
	letterCountText.lineSpacing = ButtonSetting.DEFAULT_TEXT_LINE_SPACING_PX;

	return letterCountText;
}

TypingExamButton.prototype.makeTypingRecordText = function(x, y) {
	var typingRecordText = game.add.text(x, y, "");
	typingRecordText.anchor.set(0.5, 0.5);
	typingRecordText.font = this.setting.font;
	typingRecordText.fontSize = 13;
	typingRecordText.style.fill = MainColor.WHITE_STRING;	// MainColor.LIGHTER_CHOCO_STRING;
	typingRecordText.lineSpacing = ButtonSetting.DEFAULT_TEXT_LINE_SPACING_PX;

	return typingRecordText;
}

TypingExamButton.prototype.setLetterCountText = function(letterCount) {
	// this.letterCountText.y = this.mainText.y;
	if(typeof(letterCount) === "undefined")
		this.letterCountText.text = "";
	else
		this.letterCountText.text = "( " + NumberUtil.numberWithCommas(letterCount) + "자 )";
}

TypingExamButton.prototype.setTypingRecordText = function(record) {
	var typingExamAppID = 200;

	var noDataAlpha = 0.3;
	if(typeof(record) === "undefined") {
		this.animal.sprite.alpha = noDataAlpha;
		this.typingRecordText.style.fill = MainColor.GRAY_STRING;
		this.typingRecordText.text = "기록 없음";
	} else if(record === 0) {
		this.animal.sprite.alpha = noDataAlpha;
		this.typingRecordText.style.fill = MainColor.GRAY_STRING;
		this.typingRecordText.text = "기록 없음";
	} else {
		this.typingRecordText.style.fill = MainColor.WHITE_SMOKE_STRING;
		var recordWithUnit = RecordUtil.getRecordValueWithUnit(record, typingExamAppID);
		this.typingRecordText.text = recordWithUnit;
	}
}

TypingExamButton.prototype.makeWritingNameText = function() {
	var textWidth = 232;

	var style = { };
	style.boundsAlignH = "left";
	style.boundsAlignV = "middle"
	style.wordWrap = true;
	style.wordWrapWidth = textWidth;
	style.font = this.setting.font;
	style.fontSize = this.setting.fontSize;
	style.fill = "white";
	// style.backgroundColor = "black";

	var writingNameText = game.add.text(100, 0, "", style);
	writingNameText.anchor.set(0, 0);
	writingNameText.setTextBounds(0, -1 * TypingExamButton.HEIGHT / 2, textWidth, TypingExamButton.HEIGHT);

	// mainText.fontWeight = "bolder";
	// writingNameText.lineSpacing = ButtonSetting.DEFAULT_TEXT_LINE_SPACING_PX;

	return writingNameText;
}

TypingExamButton.prototype.setWritingNameText = function(writingName) {
	this.writingNameText.y = this.mainText.y;
	this.writingNameText.text = writingName;
}

TypingExamButton.prototype.updateAnimal = function(highestRecord) {
	var animalIndex = Animal.animalIndexByRecord(highestRecord, this.appType);
	this.animal.chagneStandAnimation(animalIndex);
	this.animal.sprite.y = TypingExamButton.HEIGHT / 2 - Animal.getOffsetY(animalIndex) + TypingExamButton.ANIMAL_OFFSET_Y;
}

TypingExamButton.prototype.setInputEnabled = function(isEnabled) {
	this.strokeSprite.inputEnabled = isEnabled;
	this.buttonSprite.inputEnabled = isEnabled;

	if(isEnabled === true) {
		this.mouseOut();
		this.writingNameText.style.fill = MainColor.LINEN_STRING;
		this.animal.sprite.alpha = 1;
	} else {
		this.buttonDisabled();
		this.writingNameText.style.fill = MainColor.GRAY_STRING;
		this.animal.sprite.alpha = 0.3;
	}
}

TypingExamButton.prototype.setActive = function(flag) {
	this.setInputEnabled(flag);

	if(flag) {
		this.buttonSprite.alpha = 1;
		this.strokeSprite.alpha = 1;
		this.animal.sprite.alpha = 1;
	} else {
		this.buttonSprite.alpha = 0;
		this.strokeSprite.alpha = 0;
		this.animal.sprite.alpha = 0;
	}
}

TypingExamButton.prototype.showEmptyDisabled = function() {
	this.setInputEnabled(false);

	this.buttonSprite.alpha = 0;
	this.strokeSprite.alpha = 1;
	this.strokeSprite.tint = MainColor.SADDLE_BROWN_HEX;
}

TypingExamButton.prototype.updateAppData = function(appData) {
	this.appData = appData;

	this.appType = Animal.TYPE_TEST;

	// console.log(this.appData);
	this.writingID = this.appData.writingID;
	this.filename = this.appData.filename;

	// this.setMainText(this.appData.name);
	this.setLetterCountText(this.appData.letterCount);
	this.setWritingNameText(this.appData.name);
	this.setInputEnabled(this.appData.activated);
	this.updateAnimal(this.appData.highestRecord);
	this.setTypingRecordText(this.appData.highestRecord);
}

TypingExamButton.prototype.clickEvent = function() {

	sessionStorageManager.setPlayingAppID(200);
	sessionStorageManager.setPlayingAppName("typing_exam");
	sessionStorageManager.setPlayingAppKoreanName("긴글 시험");
	sessionStorageManager.setWritingID(this.writingID);

	if(isExperiencePlayerAccount())
		location.href = '../../web/client/ads.html';
	else
		location.href = '../../web/client/start.html';
}


TypingExamButton.loadResources = function() {
	game.load.spritesheet('keyboards', '../../../resources/image/ui/keyboard/keyboards.png', 120, 120);
}


TypingExamButton.WIDTH	= 400;
TypingExamButton.HEIGHT	= 80;

TypingExamButton.ANIMAL_OFFSET_Y	= -7;

TypingExamButton.STROKE_COLOR_OUT_HEX			= MainColor.LIGHT_CHOCO_HEX;
TypingExamButton.STROKE_COLOR_OVER_HEX			= MainColor.LIGHT_CHOCO_HEX;
TypingExamButton.STROKE_COLOR_DOWN_HEX			= MainColor.LIGHT_CHOCO_HEX;
TypingExamButton.STROKE_COLOR_DISABLED_HEX		= MainColor.LIGHT_CHOCO_HEX;

TypingExamButton.BUTTON_COLOR_OUT_HEX			= MainColor.DARKER_CHOCO_HEX;
TypingExamButton.BUTTON_COLOR_OVER_HEX			= MainColor.DARK_CHOCO_HEX;
TypingExamButton.BUTTON_COLOR_DOWN_HEX			= MainColor.LIGHTER_CHOCO_HEX;
TypingExamButton.BUTTON_COLOR_DISABLED_HEX		= MainColor.LIGHT_GRAY_HEX;

TypingExamButton.TEXT_COLOR_OUT_STRING			= MainColor.LINEN_STRING;
TypingExamButton.TEXT_COLOR_OVER_STRING			= MainColor.LINEN_STRING;
TypingExamButton.TEXT_COLOR_DOWN_STRING			= MainColor.LIGHT_CHOCO_STRING;
TypingExamButton.TEXT_COLOR_DISABLED_STRING		= MainColor.GRAY_STRING;