//Copyright (C) Ben Parbury - All Rights Reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Written by Ben Parbury <ben@gridlockinteractive.com> 2014

var gKeyboard = new Keyboard();

EKeyCode =
{
	a : 65,
	d : 68,
	s : 83,
	w : 87,
	
	left : 37,
	right : 39,
	up : 38,
	down : 40,
	
	return : 13,
	space : 32,
	escape : 27,
	home : 36,
	pagedown: 34,
	delete : 46,
	
	r : 82,
	p : 80,
	c : 67,
	z : 90,
	
	add : 107,
	subtract : 109,
	equal: 187,
	dash: 189,

	F4: 115,
	F8: 119,
	F12: 123,
	
	closemenu : 543,
}

function Keyboard()
{
	this.keyDownCallback;
	this.keyUpCallback;
	this.keyPressCallback;

	this.keyState = new Array();
	
	this.remap = new Array();
	
	this.Init = function(keyDownCallback, keyUpCallback, keyPressCallback)
	{
		gConsole.Log(1, "[KEY] Init");
		
		document.addEventListener("keydown", this.OnKeyDown, false);
		document.addEventListener("keyup", this.OnKeyUp, false);
		
		this.keyDownCallback = keyDownCallback;
		this.keyUpCallback = keyUpCallback;
		this.keyPressCallback = keyPressCallback;

		this.LoadRemaps();
	}
	
	this.LoadRemaps = function()
	{
		var remapCountStr = gStorage.getItem("keyboard", "remaps");
		if(remapCountStr)
		{
			var remapCount = parseInt(remapCountStr);
			for(var i = 0; i < remapCount; i++)
			{
				var remap = gStorage.getItem("keyboard", "remap" + i);
				if(remap)
				{
					this.remap[i] = parseInt(remap);
				}
			}
		}
	}
	
	this.SaveRemaps = function()
	{
		var remaps = this.remap.length;
		var maxRemaps = remaps;
		var previousLength = gStorage.getItem("keyboard", "remaps");
		if(previousLength)
		{
			maxRemaps = Math.max(remaps, parseInt(previousLength));
		}
		gStorage.setItem("keyboard", "remaps", remaps);
		for(var i = 0; i < maxRemaps; i++)
		{
			var remap = this.remap[i];
			if(remap)
			{
				gStorage.setItem("keyboard", "remap" + i, remap);
			}
			else
			{
				gStorage.removeItem("keyboard", "remap" + i);
			}
		}
		
		gControlsOverlay.redraw = true;
	}
	
	this.Remap = function(newkeycode, keycode)
	{
		if(newkeycode == keycode)
		{
			if(this.remap[newkeycode])
			{
				this.Demap(newkeycode);
			}
			return;
		}
		this.remap[newkeycode] = keycode;
		this.SaveRemaps();
	}
	
	this.Demap = function(keycode)
	{
		this.remap[keycode] = undefined;
		this.SaveRemaps();
	}
	
	this.ResetToDefault = function()
	{
		var remaps = this.remap.length;
		for(var i = 0; i < remaps; i++)
		{
			this.remap[i] = null;
		}
		this.SaveRemaps();
	}
	
	this.GetMap = function(keycode)
	{
		var remap = this.remap[keycode];
		if(remap)
		{
			return remap;
		}
		
		return null;
	}
	
	this.GetUnmap = function(keycode)
	{
		var index = this.remap.indexOf(keycode);
		if(index > -1)
		{
			return index;
		}
		return null;
	}
	
	this.GetKey = function(keycode)
	{
		//If you're remapping keys, just return
		if(gKeyRemapping)
		{
			return keycode;
		}
		
		//Returned remapped key
		var remap = this.remap[keycode];
		if(remap)
		{
			return remap;
		}
		else
		{
			//block orign of remaps
			var index = this.remap.indexOf(keycode);
			if(index > -1)
			{
				return null;
			}
			else
			{
				return keycode;
			}
		}
	}
	
	this.OnKeyDown = function(evt)
	{
		var keycode = gKeyboard.GetKey(evt.keyCode);
		if(keycode)
		{
			gKeyboard.OnMappedKeyDown(keycode);
		}
	}
	
	this.OnMappedKeyDown = function(keycode)
	{
		gConsole.Log(11, "[KEY] KeyDown " + keycode);
		gKeyboard.keyDownCallback(keycode);
		if(!gKeyboard.keyState[keycode])
		{
			gKeyboard.keyState[keycode] = true;
			gKeyboard.OnKeyPress(keycode);
		}
		SetInputType(EInputType.Keyboard);
	}

	this.OnKeyUp = function(evt)
	{
		var keycode = gKeyboard.GetKey(evt.keyCode);
		if(keycode)
		{
			gKeyboard.OnMappedKeyUp(keycode);
		}
	}
	
	this.OnMappedKeyUp = function(keycode)
	{
		gConsole.Log(11, "[KEY] KeyUp " + keycode);
		gKeyboard.keyUpCallback(keycode);
		gKeyboard.keyState[keycode] = false;
	}

	this.OnKeyPress = function(keycode)
	{
		gConsole.Log(11, "[KEY] KeyPress " + keycode);
		gKeyboard.keyPressCallback(keycode);
	}
	
	this.Debug = function()
	{
		if(gKeyRemapping)
		{
			gDraw.Watch("Remapping!");
		}
		
		var remaps = this.remap.length;
		for(var i = 0; i < remaps; i++)
		{
			var remap = this.remap[i];
			if(remap)
			{
				gDraw.Watch("Remap (" + i + ") -> (" + remap + ") " + KeyCodeToString(i) + " to " + KeyCodeToString(remap));
			}
		}
	}
}

