// Cheat Code Monitor
// Copyright Jared McGuire

/********** CheatCode **********/
function CheatCode(name, hash, action) {
	this.name = name;
	this.hash = hash;
	this.action = action;
}

CheatCode.prototype.name = "";
CheatCode.prototype.hash = "";
CheatCode.prototype.action = null;


/********** CheatCodeMonitor **********/
function CheatCodeMonitor() {
	window.document.onkeypress = function(e) { monitor.onKeyPress(e); };
}

CheatCodeMonitor.prototype.buffer = "";
CheatCodeMonitor.prototype.lastKeypressTime = new Date();
CheatCodeMonitor.prototype.cheatCodes = new Array();
CheatCodeMonitor.prototype.hasher = function(str) { return str; };

function OnKeyPress(e) {
	this.lastKeypressTime = new Date();
	var charcode = e ? e.charCode : event.charCode;
	var keycode = e.keyCode ? e.keyCode : e.which;
	//ConsoleDebug("charcode = " + charcode + " = " + String.fromCharCode(charcode) + " / keycode = " + keycode);
	this.buffer += ConvertInputToString(charcode, keycode);
	//ConsoleDebug("buffer = " + this.buffer);
	this.activateMatchingCheatCodes();
}
CheatCodeMonitor.prototype.onKeyPress = OnKeyPress;

function ClearBuffer() {
	this.buffer = "";
	ConsoleInfo("Buffer cleared.");
}
CheatCodeMonitor.prototype.clearBuffer = ClearBuffer;

function ResetTimer(timeout) {
	if (this.buffer == "") return;
	var now = new Date();
	var elapsed = now.getTime() - this.lastKeypressTime.getTime();
	if (elapsed > timeout)
		this.clearBuffer();
}
CheatCodeMonitor.prototype.resetTimer = ResetTimer;

function AddCheatCode(name, hash, action) {
	var cheatCode = new CheatCode(name, hash, action);
	this.cheatCodes[this.cheatCodes.length] = cheatCode;
}
CheatCodeMonitor.prototype.addCheatCode = AddCheatCode;

function ActivateMatchingCheatCodes() {
	for (var i = 0; i < this.cheatCodes.length; i++) {
		if (this.hasher(this.buffer) == this.cheatCodes[i].hash) {
			ConsoleInfo("Activating cheat code: " + this.cheatCodes[i].name);
			this.clearBuffer();
			this.cheatCodes[i].action()
			return;
		}
	}
}
CheatCodeMonitor.prototype.activateMatchingCheatCodes = ActivateMatchingCheatCodes;


/********** Functions **********/

function ConvertInputToString(charcode, keycode) {
	var key = null;
	if (charcode == 0) {
		switch (keycode) {
			case 8: // Backspace
				key = "[BACKSPACE]";
				break;
			case 13: // Enter
				key = "[ENTER]";
				break;
			case 19: // Pause/Break
				key = "[BREAK]";
				break;
			case 27: // Escape
				key = "[ESC]";
				break;
			case 33: // Page Up
				key = "[PGUP]";
				break;
			case 34: // Page Down
				key = "[PGDOWN]";
				break;
			case 35: // End
				key = "[END]";
				break;
			case 36: // Home
				key = "[HOME]";
				break;
			case 37: // Left Arrow
				key = "[LEFT]";
				break;
			case 38: // Up Arrow
				key = "[UP]";
				break;
			case 39: // Right Arrow
				key = "[RIGHT]";
				break;
			case 40: // Down Arrow
				key = "[DOWN]";
				break;
			case 45: // Insert
				key = "[INS]";
				break;
			case 46: // Delete
				key = "[DEL]";
				break;
			case 12: // Num Pad 5 when Num Lock is off
			case 91: // Left Windows
			case 92: // Right Windows
			case 93: // Condext Menu
			case 112: // F1
			case 113: // F2
			case 114: // F3
			case 115: // F4
			case 116: // F5
			case 117: // F6
			case 118: // F7
			case 119: // F8
			case 120: // F9
			case 121: // F10
			case 122: // F11
			case 123: // F12
			case 144: // Num Lock
			case 145: // Scroll Lock
				return;
		}
	}
	return key ? key : String.fromCharCode(charcode);
}
