function KeyData() {
	this.functionKey = null;
	this.englishKey = null;
	this.koreanKey = null;
}

function KeyTextData(normalText, shiftText) {
	this.normalText = normalText;
	this.shiftText = shiftText;
}


function KeyMapper() {
	this.keyboardKeyMap = {};
	this.inputKeyMap = {};

	this.registerFunctionKey();
	this.registerEnglishKey();
	this.registerKoreanKey();
}


KeyMapper.prototype.registerFunctionKey = function() {
	this.registerFunctionKeyTextData("Backquote", "`", "~");
	this.registerFunctionKeyTextData("Digit1", "1", "!");
	this.registerFunctionKeyTextData("Digit2", "2", "@");
	this.registerFunctionKeyTextData("Digit3", "3", "#");
	this.registerFunctionKeyTextData("Digit4", "4", "$");
	this.registerFunctionKeyTextData("Digit5", "5", "%");
	this.registerFunctionKeyTextData("Digit6", "6", "^");
	this.registerFunctionKeyTextData("Digit7", "7", "&");
	this.registerFunctionKeyTextData("Digit8", "8", "*");
	this.registerFunctionKeyTextData("Digit9", "9", "(");
	this.registerFunctionKeyTextData("Digit0", "0", ")");
	this.registerFunctionKeyTextData("Minus", "-", "_");
	this.registerFunctionKeyTextData("Equal", "=", "+");
	this.registerFunctionKeyTextData("Backspace", "backspace", "backspace");

	this.registerFunctionKeyTextData("Tab", "tab", "tab");
	this.registerFunctionKeyTextData("BracketLeft", "[", "{");
	this.registerFunctionKeyTextData("BracketRight", "]", "}");
	this.registerFunctionKeyTextData("Backslash", "\\", "|");

	this.registerFunctionKeyTextData("CapsLock", "caps lock", "caps lock");
	this.registerFunctionKeyTextData("Semicolon", ";", ":");
	this.registerFunctionKeyTextData("Quote", "'", "\"");
	this.registerFunctionKeyTextData("Enter", "enter", "enter");

	this.registerFunctionKeyTextData("ShiftLeft", "left_shift", "left_shift");
	this.registerFunctionKeyTextData("ShiftRight", "right_shift", "right_shift");
	this.registerFunctionKeyTextData("Comma", ",", "<");
	this.registerFunctionKeyTextData("Period", ".", ">");
	this.registerFunctionKeyTextData("Slash", "/", "?");

	this.registerFunctionKeyTextData("ControlLeft", "ctrl", "ctrl");
	this.registerFunctionKeyTextData("window", "window", "window");
	this.registerFunctionKeyTextData("AltLeft", "alt", "alt");
	this.registerFunctionKeyTextData("Chinese", "한자", "한자");
	this.registerFunctionKeyTextData("Space", "space", "space");
	this.registerFunctionKeyTextData("KoreanEnglish", "한/영", "한/영");
	this.registerFunctionKeyTextData("AltRight", "alt", "alt");
	this.registerFunctionKeyTextData("ControlRight", "ctrl", "ctrl");
}

KeyMapper.prototype.registerEnglishKey = function() {
	this.registerEnglishKeyTextData("KeyQ", "q", "Q");
	this.registerEnglishKeyTextData("KeyW", "w", "W");
	this.registerEnglishKeyTextData("KeyE", "e", "E");
	this.registerEnglishKeyTextData("KeyR", "r", "R");
	this.registerEnglishKeyTextData("KeyT", "t", "T");
	this.registerEnglishKeyTextData("KeyY", "y", "Y");
	this.registerEnglishKeyTextData("KeyU", "u", "U");
	this.registerEnglishKeyTextData("KeyI", "i", "I");
	this.registerEnglishKeyTextData("KeyO", "o", "O");
	this.registerEnglishKeyTextData("KeyP", "p", "P");

	this.registerEnglishKeyTextData("KeyA", "a", "A");
	this.registerEnglishKeyTextData("KeyS", "s", "S");
	this.registerEnglishKeyTextData("KeyD", "d", "D");
	this.registerEnglishKeyTextData("KeyF", "f", "F");
	this.registerEnglishKeyTextData("KeyG", "g", "G");
	this.registerEnglishKeyTextData("KeyH", "h", "H");
	this.registerEnglishKeyTextData("KeyJ", "j", "J");
	this.registerEnglishKeyTextData("KeyK", "k", "K");
	this.registerEnglishKeyTextData("KeyL", "l", "L");

	this.registerEnglishKeyTextData("KeyZ", "z", "Z");
	this.registerEnglishKeyTextData("KeyX", "x", "X");
	this.registerEnglishKeyTextData("KeyC", "c", "C");
	this.registerEnglishKeyTextData("KeyV", "v", "V");
	this.registerEnglishKeyTextData("KeyB", "b", "B");
	this.registerEnglishKeyTextData("KeyN", "n", "N");
	this.registerEnglishKeyTextData("KeyM", "m", "M");
}

KeyMapper.prototype.registerKoreanKey = function() {
	this.registerKoreanKeyTextData("KeyQ", "ㅂ", "ㅃ");
	this.registerKoreanKeyTextData("KeyW", "ㅈ", "ㅉ");
	this.registerKoreanKeyTextData("KeyE", "ㄷ", "ㄸ");
	this.registerKoreanKeyTextData("KeyR", "ㄱ", "ㄲ");
	this.registerKoreanKeyTextData("KeyT", "ㅅ", "ㅆ");
	this.registerKoreanKeyTextData("KeyY", "ㅛ", "ㅛ");
	this.registerKoreanKeyTextData("KeyU", "ㅕ", "ㅕ");
	this.registerKoreanKeyTextData("KeyI", "ㅑ", "ㅑ");
	this.registerKoreanKeyTextData("KeyO", "ㅐ", "ㅒ");
	this.registerKoreanKeyTextData("KeyP", "ㅔ", "ㅖ");

	this.registerKoreanKeyTextData("KeyA", "ㅁ", "ㅁ");
	this.registerKoreanKeyTextData("KeyS", "ㄴ", "ㄴ");
	this.registerKoreanKeyTextData("KeyD", "ㅇ", "ㅇ");
	this.registerKoreanKeyTextData("KeyF", "ㄹ", "ㄹ");
	this.registerKoreanKeyTextData("KeyG", "ㅎ", "ㅎ");
	this.registerKoreanKeyTextData("KeyH", "ㅗ", "ㅗ");
	this.registerKoreanKeyTextData("KeyJ", "ㅓ", "ㅓ");
	this.registerKoreanKeyTextData("KeyK", "ㅏ", "ㅏ");
	this.registerKoreanKeyTextData("KeyL", "ㅣ", "ㅣ");

	this.registerKoreanKeyTextData("KeyZ", "ㅋ", "ㅋ");
	this.registerKoreanKeyTextData("KeyX", "ㅌ", "ㅌ");
	this.registerKoreanKeyTextData("KeyC", "ㅊ", "ㅊ");
	this.registerKoreanKeyTextData("KeyV", "ㅍ", "ㅍ");
	this.registerKoreanKeyTextData("KeyB", "ㅠ", "ㅠ");
	this.registerKoreanKeyTextData("KeyN", "ㅜ", "ㅜ");
	this.registerKoreanKeyTextData("KeyM", "ㅡ", "ㅡ");
}


KeyMapper.prototype.registerFunctionKeyTextData = function(keyID, normalText, shiftText) {
	if(this.keyboardKeyMap[keyID] === undefined) {
		this.keyboardKeyMap[keyID] = new KeyData();
	}
	this.keyboardKeyMap[keyID].functionKey = new KeyTextData(normalText, shiftText);

	if(this.inputKeyMap[keyID] === undefined)
		this.inputKeyMap[keyID] = keyID;
	if(this.inputKeyMap[normalText] === undefined)
		this.inputKeyMap[normalText] = keyID;
	if(this.inputKeyMap[shiftText] === undefined)
		this.inputKeyMap[shiftText] = keyID;
}

KeyMapper.prototype.registerEnglishKeyTextData = function(keyID, normalText, shiftText) {
	if(this.keyboardKeyMap[keyID] === undefined) {
		this.keyboardKeyMap[keyID] = new KeyData();
	}
	this.keyboardKeyMap[keyID].englishKey = new KeyTextData(normalText, shiftText);

	if(this.inputKeyMap[keyID] === undefined)
		this.inputKeyMap[keyID] = keyID;
	if(this.inputKeyMap[normalText] === undefined)
		this.inputKeyMap[normalText] = keyID;
	if(this.inputKeyMap[shiftText] === undefined)
		this.inputKeyMap[shiftText] = keyID;
}

KeyMapper.prototype.registerKoreanKeyTextData = function(keyID, normalText, shiftText) {
	if(this.keyboardKeyMap[keyID] === undefined) {
		this.keyboardKeyMap[keyID] = new KeyData();
	}
	this.keyboardKeyMap[keyID].koreanKey = new KeyTextData(normalText, shiftText);

	if(this.inputKeyMap[keyID] === undefined)
		this.inputKeyMap[keyID] = keyID;
	if(this.inputKeyMap[normalText] === undefined)
		this.inputKeyMap[normalText] = keyID;
	if(this.inputKeyMap[shiftText] === undefined)
		this.inputKeyMap[shiftText] = keyID;
}


KeyMapper.prototype.getNormalText = function(keyID, language) {
	if(this.keyboardKeyMap[keyID] === undefined) {
		console.log(keyID);
		return "?";
	}

	// console.log(keyID);
	// console.log(language);
	// console.log(this.keyboardKeyMap[keyID]);
	var keyData = this.keyboardKeyMap[keyID];
	if(language === Keyboard.ENGLISH && keyData.englishKey !== null) {
		return keyData.englishKey.normalText;
	}
	else if(language === Keyboard.KOREAN && keyData.koreanKey !== null) {
		return keyData.koreanKey.normalText;
	}
	else if(keyData.functionKey !== undefined) {
		return keyData.functionKey.normalText;
	}
	else {
		return "?";
	}
}

KeyMapper.prototype.getShiftText = function(keyID, language) {
	if(this.keyboardKeyMap[keyID] === undefined) {
		console.log(keyID);
		return "?";
	}

	// console.log(keyID);
	// console.log(language);
	// console.log(this.keyboardKeyMap[keyID]);
	var keyData = this.keyboardKeyMap[keyID];
	if(language === Keyboard.ENGLISH && keyData.englishKey !== null) {
		return keyData.englishKey.shiftText;
	}
	else if(language === Keyboard.KOREAN && keyData.koreanKey !== null) {
		return keyData.koreanKey.shiftText;
	}
	else if(keyData.functionKey !== undefined) {
		return keyData.functionKey.shiftText;
	}
	else {
		return "?";
	}
}



KeyMapper.prototype.getKeyIDOfText = function(text) {
	return this.inputKeyMap[text];
}

KeyMapper.prototype.isShiftText = function(text) {
	var keyID = this.getKeyIDOfText(text);
	var keyData = this.keyboardKeyMap[keyID];

	if(keyData.englishKey !== null && keyData.englishKey.shiftText === text)
		return true;
	else if(keyData.koreanKey !== null && keyData.koreanKey.shiftText === text && keyData.koreanKey.normalText !== text)
		return true;
	else if(keyData.functionKey !== null && keyData.functionKey.shiftText === text)
		return true;

	return false;
}