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

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

	this.appGroup = 0;
	this.appType = Animal.TYPE_PRACTICE;

	var setting = new ButtonSetting(x, y, TypingAppButton.WIDTH, TypingAppButton.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(
		TypingAppButton.STROKE_COLOR_OUT_HEX,
		TypingAppButton.STROKE_COLOR_OVER_HEX,
		TypingAppButton.STROKE_COLOR_DOWN_HEX,
		TypingAppButton.STROKE_COLOR_DISABLED_HEX
	);
	setting.setButtonColor(
		TypingAppButton.BUTTON_COLOR_OUT_HEX,
		TypingAppButton.BUTTON_COLOR_OVER_HEX,
		TypingAppButton.BUTTON_COLOR_DOWN_HEX,
		TypingAppButton.BUTTON_COLOR_DISABLED_HEX
	);
	setting.setIconColor(
		TypingAppButton.BUTTON_COLOR_OUT_HEX,
		TypingAppButton.BUTTON_COLOR_OVER_HEX,
		TypingAppButton.BUTTON_COLOR_DOWN_HEX,
		TypingAppButton.BUTTON_COLOR_DISABLED_HEX
	);
	setting.setTextColor(
		TypingAppButton.TEXT_COLOR_OUT_STRING,
		TypingAppButton.TEXT_COLOR_OVER_STRING,
		TypingAppButton.TEXT_COLOR_DOWN_STRING,
		TypingAppButton.TEXT_COLOR_DISABLED_STRING
	);

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

	this.mainText.x = TypingAppButton.WIDTH / 2 + TypingAppButton.KEYBOARD_WIDTH / 6;
	// this.setMainText("");
	// this.addIcon("tile_choco");
	// this.setIconSize(40);
	// this.setShortcutText("[단축키]");

	this.keyboardSprite = game.add.sprite(
		TypingAppButton.KEYBOARD_WIDTH / 2 + 10,
		TypingAppButton.HEIGHT / 2,
		"keyboards"
	);
	this.keyboardSprite.anchor.set(0.5);
	this.keyboardSprite.frame = 1;
	this.keyboardSprite.width = TypingAppButton.KEYBOARD_WIDTH;
	this.keyboardSprite.height = TypingAppButton.KEYBOARD_WIDTH;
	this.buttonSprite.addChild(this.keyboardSprite);

	var animalPosX = TypingAppButton.WIDTH - Animal.SPRITE_WIDTH / 2;
	var animalPosY = TypingAppButton.HEIGHT / 2 + TypingAppButton.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);
}

TypingAppButton.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.LIGHTER_CHOCO_STRING;
	typingRecordText.lineSpacing = ButtonSetting.DEFAULT_TEXT_LINE_SPACING_PX;

	return typingRecordText;
}

TypingAppButton.prototype.setTypingRecordText = function(record) {
	var typingAppID = 1;

	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, typingAppID);
		this.typingRecordText.text = recordWithUnit;
	}
}

TypingAppButton.prototype.updateKeyboard = function(appID) {
	switch(appID) {
		case 1:
		case 11:
		case 21:
		case 31:
		this.keyboardSprite.frame = 0;
		break;

		case 2:
		case 12:
		case 22:
		case 32:
		this.keyboardSprite.frame = 1;
		break;

		case 3:
		case 13:
		case 23:
		case 33:
		this.keyboardSprite.frame = 2;
		break;

		case 4:
		case 14:
		case 24:
		case 34:
		this.keyboardSprite.frame = 3;
		break;

		case 5:
		case 15:
		case 25:
		case 35:
		this.keyboardSprite.frame = 4;
		break;

		case 6:
		case 16:
		case 26:
		case 36:
		this.keyboardSprite.frame = 5;
		break;

		case 7:
		case 27:
		case 37:
		this.keyboardSprite.frame = 6;
		break;

		case 17:
		this.keyboardSprite.frame = 10;
		break;

		case 8:
		case 18:
		case 28:
		case 38:
		this.keyboardSprite.frame = 7;
		break;

		case 9:
		case 19:
		case 29:
		case 39:
		this.keyboardSprite.frame = 8;
		break;

		case 10:
		case 20:
		case 30:
		case 40:
		this.keyboardSprite.frame = 9;
		break;

		default:
		this.keyboardSprite.frame = 0;
		break;
	}
}

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

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

	if(isEnabled === true) {
		this.mouseOut();
		// this.keyboardSprite.tint = MainColor.WHITE_HEX;
		// this.animal.sprite.tint = MainColor.WHITE_HEX;
		this.keyboardSprite.alpha = 1;
		this.animal.sprite.alpha = 1;
	} else {
		this.buttonDisabled();
		// this.keyboardSprite.tint = MainColor.DIM_GRAY_HEX;
		// this.animal.sprite.tint = MainColor.DIM_GRAY_HEX;
		this.keyboardSprite.alpha = 0.3;
		this.animal.sprite.alpha = 0.3;
	}
}

TypingAppButton.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;
	}
}

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

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

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

	this.appGroup = this.appData.appGroup;
	if(Number(this.appGroup) === 0)
		this.appType = Animal.TYPE_PRACTICE;
	else
		this.appType = Animal.TYPE_TEST;

	// console.log(this.appData);
	this.updateKeyboard(this.appData.appID);
	this.setMainText(this.appData.koreanName);
	this.setInputEnabled(this.appData.activated);
	this.updateAnimal(this.appData.highestRecord);
	this.setTypingRecordText(this.appData.highestRecord);
}

TypingAppButton.prototype.clickEvent = function() {
	sessionStorageManager.setPlayingAppID(this.appData.appID);
	sessionStorageManager.setPlayingAppName(this.appData.appName);
	sessionStorageManager.setPlayingAppKoreanName(this.appData.koreanName);

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


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


TypingAppButton.WIDTH	= 400;
TypingAppButton.HEIGHT	= 80;

TypingAppButton.KEYBOARD_WIDTH	= 100;

TypingAppButton.ANIMAL_OFFSET_Y	= -7;

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

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

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