//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 gButtonRemapping = null;

function StartRemapping(button)
{
	button.name = "";
	gKeyRemapping = true;
	gButtonRemapping = button;
}

function KeyRemap(keycode)
{
	gButtonRemapping.SetKeyCode(keycode);
	
	//Update the names of all the other remap buttons (If we remap the same key twice we need to update the other remap button)
	UpdateRemapButtons();
	gKeyRemapping = false;
}

function UpdateRemapButtons()
{
	var length = gRemapButtons.length;
	for(var i = 0; i < length; i++)
	{
		gRemapButtons[i].SetName();
	}
}

var gRemapControlButtonIndex = 1;

function RemapControlButton(name, key, altKey, name2, key2, altKey2)
{
	this.name = name;
	
	this.buttons = new Array();
	this.texts = new Array();

	this.AddText = function(label)
	{
		var text = new Text(label, 'right');
		text.SetText(label);
		this.texts.push(text)
	}
	
	this.AddText(name);
	if(name2)
	{
		this.AddText(name2);
	}

	this.AddButton = function(keycode)
	{
		var button = new RemapButton(keycode, function() { StartRemapping(this) });
		this.buttons.push(button)
	}
	
	this.AddResetButton = function()
	{
		var button = new Button("Reset to Default", function() { gKeyboard.ResetToDefault(); UpdateRemapButtons(); });
		this.buttons.push(button)
	}
	
	this.AddButton(key);
	this.AddButton(altKey);
	if(key2)
	{
		this.AddButton(key2);
		this.AddButton(altKey2);
	}
	else
	{
		this.AddResetButton();
	}
	
	this.x = 0;
	this.y = 0;
	this.width = 0;
	this.height = 0;
	
	this.highlightSound = gSounds.Get("menuHighlight");

	this.CanSelect = function()
	{
		return true;
	}
	
	this.PlayHighlightSound = function()
	{
		this.highlightSound.Play();
	}

	this.GetX = function()
	{
		return this.x;
	}
	
	this.GetY = function()
	{
		return this.y;
	}

	this.GetWidth = function()
	{
		return this.width;
	}
	
	this.GetHeight = function()
	{
		return this.height;
	}
	
	this.SetPosition = function(x, y)
	{
		this.x = x;
		this.y = y;
		var textWidth = kTileSize * 3;
		var buttonWidth = kTileSize + 10;
		var halfTotalWidth = textWidth + (buttonWidth * 2);
		var px = this.x - halfTotalWidth;
		this.texts[0].SetPosition(px + textWidth - (kTileSize * 0.7), y);
		px += textWidth;
		this.buttons[0].SetPosition(px, y);
		px += buttonWidth;
		this.buttons[1].SetPosition(px, y);
		px += buttonWidth;
		if(this.texts[1])
		{
			this.texts[1].SetPosition(px + textWidth - (kTileSize * 0.7), y);
		}
		else
		{
			this.buttons[2].SetPosition(px + textWidth - (kTileSize * 0.5), y);
		}
		px += textWidth;
		if(this.buttons[3])
		{
			this.buttons[2].SetPosition(px, y);
		}
		px += buttonWidth;
		if(this.buttons[3])
		{
			this.buttons[3].SetPosition(px, y);
		}
		px += buttonWidth;
	}

	this.SetSize = function(width, height)
	{
		this.width = width;
		this.height = height;
		for(var i = 0; i < this.texts.length; i++)
		{
			this.texts[i].SetSize(kTileSize * 3, height);
		}
		for(var i = 0; i < this.buttons.length; i++)
		{
			this.buttons[i].SetSize(kTileSize, kTileSize);
		}
		if(this.buttons.length == 3)
		{
			this.buttons[2].SetSize(kTileSize * 4.5, kTileSize);
		}
	}

	this.SetFontSize = function(size)
	{
		for(var i = 0; i < this.texts.length; i++)
		{
			this.texts[i].SetFontSize(size);
		}
		for(var i = 0; i < this.buttons.length; i++)
		{
			this.buttons[i].SetFontSize(size);
		}
	}
	
	this.SelectedItem = function()
	{
		if(this.buttons.length == 3)
		{
			if(gRemapControlButtonIndex == 3)
			{
				gRemapControlButtonIndex = 2;
			}
		}
	}
	
	this.SelectLeft = function()
	{
		this.SetSelectedIndex(gRemapControlButtonIndex - 1);
	}
	
	this.SelectRight = function()
	{
		this.SetSelectedIndex(gRemapControlButtonIndex + 1);
	}
	
	this.SetSelectedIndex = function(index)
	{
		if(index >= 0 && index < this.buttons.length)
		{
			if(index != gRemapControlButtonIndex)
			{
				gRemapControlButtonIndex = index;
				this.highlightSound.Play();
			}
		}
	}
	
	this.Interact = function()
	{
		this.buttons[gRemapControlButtonIndex].Interact();
	}
	
	this.CanInteract = function(x, y)
	{
		for(var i = 0; i < this.buttons.length; i++)
		{
			if(this.buttons[i].CanInteract(x, y))
			{
				return true;
			}
		}
		return false;
	}
	
	this.InteractMove = function(x, y)
	{
		for(var i = 0; i < this.buttons.length; i++)
		{
			if(this.buttons[i].CanInteract(x, y))
			{
				if(gRemapControlButtonIndex != i)
				{
					gRemapControlButtonIndex = i;
					this.highlightSound.Play()
				}
			}
		}
	}
	
	this.InteractRelease = function (x, y)
	{
		for(var i = 0; i < this.buttons.length; i++)
		{
			if(this.buttons[i].InteractRelease(x, y))
			{
				gRemapControlButtonIndex = i;
				return true;
			}
		}
		return false;
	}


	this.Redraw = function()
	{
		for(var i = 0; i < this.texts.length; i++)
		{
			this.texts[i].Redraw();
		}
		for(var i = 0; i < this.buttons.length; i++)
		{
			this.buttons[i].Redraw();
		}
	}

	this.DrawClear = function()
	{
		for(var i = 0; i < this.texts.length; i++)
		{
			this.texts[i].DrawClear();
		}
		for(var i = 0; i < this.buttons.length; i++)
		{
			this.buttons[i].DrawClear();
		}
	}
	
	this.Draw = function(selected)
	{
		for(var i = 0; i < this.texts.length; i++)
		{
			this.texts[i].Draw();
		}
		for(var i = 0; i < this.buttons.length; i++)
		{
			this.buttons[i].Draw(selected && i == gRemapControlButtonIndex);
		}
	}

	this.ForceUpdate = function()
	{
		for(var i = 0; i < this.buttons.length; i++)
		{
			this.buttons[i].ForceUpdate();
		}
	}
	
	
	this.Update = function(selected)
	{
		for(var i = 0; i < this.buttons.length; i++)
		{
			this.buttons[i].Update(selected && i == gRemapControlButtonIndex);
		}
	}
	
	this.IsVisible = function()
	{
		return this.texts[0].IsVisible()
	}
	
	this.SetVisible = function(set)
	{
		for(var i = 0; i < this.texts.length; i++)
		{
			this.texts[i].SetVisible(set);
		}
		for(var i = 0; i < this.buttons.length; i++)
		{
			this.buttons[i].SetVisible(set);
		}
	}
}
