var gControlsOverlay = new ControlsOverlay()

EControlsDisplaySetting =
{
	Never : "Never",
	Tutorial : "Tutorial",
	Always : "Always",
}

var gControlsDisplaySetting = gDemo ? EControlsDisplaySetting.Always : EControlsDisplaySetting.Tutorial;
var displaySetting = gStorage.getItem("options", "display", "controls");
if(displaySetting)
{
	gControlsDisplaySetting = displaySetting;
}

function ControlsOverlay()
{
	this.redraw = true;
	this.enabled = false;
	
	this.ShouldEnable = function()
	{
		switch(gControlsDisplaySetting)
		{
			case EControlsDisplaySetting.Never:
				return false;
			case EControlsDisplaySetting.Tutorial:
				return gLevelSystem.levelSet == "vanilla";
			case EControlsDisplaySetting.Always:
				return gLevelSystem.levelSet != "menu";
		}
		
		return false;
	}
	
	//Called when toggling display options
	this.Init = function()
	{
		if(this.ShouldEnable())
		{
			this.enabled = true;
			this.redraw = true;
		}
		else
		{
			this.redraw = true;
			this.DrawClear();
			this.redraw = false;
			this.enabled = false;
		}
	}
		
	this.GetSpacing = function()
	{
		return kTileSize * 0.1;
	}
	
	this.GetFontSize = function()
	{
		return GetFontScale(20);
	}
	
	this.GetDrawX = function(column)
	{	
		if (column == null)
		{
			column = 0;
		}
		return gDraw.canvas.width - this.GetSpacing() - (this.GetFontSize() * column * 1.4);
	}
	
	this.GetDrawY = function(line)
	{
		return gDraw.canvas.height - this.GetSpacing() - (this.GetFontSize() * line * 1.4);
	}
	
	this.GetDrawAltX = function()
	{
		return kTileSize * 0.2;
	}
	
	this.GetDrawAltY = function(line)
	{
		return (this.GetFontSize() * line * 1.4);
	}
	
	this.DrawKey = function(key, x, y)
	{
		var keytext = KeyToString(key);
		
		var fontSize = this.GetFontSize();
		var width = fontSize * 1.4;
		var height = fontSize * 1.4;
		
		gDraw.overlay.strokeStyle = "rgb(255, 255, 255)";
		gDraw.overlay.lineWidth = kTileSize * 0.02;
		gDraw.overlay.RoundedRect(x,y-(height * 0.33),width,height, 7);
		gDraw.overlay.stroke();
		
		gDraw.overlay.textAlign	 = 'center';
		gDraw.overlay.fillStyle = "rgb(255, 255, 255)";
		
		var fontScale = keytext.length == 1 ? 1 : 0.4;
		var fontSize = this.GetFontSize() * fontScale;
		gDraw.overlay.font = GetFont(fontSize);
		
		var spaceIndex = keytext.indexOf(" ")
		
		if (spaceIndex > -1)
		{
			var top = keytext.substring(0, spaceIndex);
			var bottom = keytext.substring(spaceIndex + 1);
			gDraw.overlay.fillText(top,    (x+width/2), (y + (this.GetSpacing() * 0.2)));	
			gDraw.overlay.fillText(bottom, (x+width/2), (y + (this.GetSpacing() * 1.8)));	
		}
		else
		{
			gDraw.overlay.fillText(keytext, (x+width/2), (y+this.GetSpacing()));	
		}
	}
	
	this.DrawPadButton = function(padcode, x, y)
	{
		var fontSize = this.GetFontSize();
		var size = fontSize * 1.4;
		var drawX = x + size * 0.5
		var drawY = y + this.GetSpacing();
		gDraw.overlay.beginPath();
		gDraw.overlay.strokeStyle = "rgb(255, 255, 255)";
		var lineWidth = kTileSize * 0.02;
		gDraw.overlay.lineWidth = lineWidth
		gDraw.overlay.arc(drawX - lineWidth/2, drawY - lineWidth/2, size * 0.5, 0, Math.PI * 2);
		gDraw.overlay.stroke();
		
		gDraw.overlay.textAlign	 = 'center';
		gDraw.overlay.fillStyle = "rgb(255, 255, 255)";
		
		if(padcode)
		{
			gDraw.overlay.fillText(PadCodeToString(padcode), drawX, drawY);
		}
		else
		{
			var analogSize = size * 1.2;
			gDraw.overlay.beginPath();
			gDraw.overlay.arc(drawX - lineWidth/2, drawY - lineWidth/2, analogSize * 0.5, 0, Math.PI * 2);
			gDraw.overlay.stroke();
		}
	}

	this.DrawClear = function()
	{
		if(this.redraw)
		{
			var extraShadow = kTileSize * 0.12;
			var y = this.GetDrawAltY(0);
			var width = kTileSize*6;
			var height = this.GetFontSize() * 11 * 1.2;
			var x = this.GetDrawX() - width;
			gDraw.overlay.clearRect(x, y, width + extraShadow, height+extraShadow);
		}
	}
	
	this.Draw = function()
	{
		if (this.enabled && this.redraw)
		{		
			var fontSize = this.GetFontSize();
			var spacing = this.GetSpacing();
			gDraw.overlay.font = GetFont(fontSize);
			var x = this.GetDrawX();
			gDraw.overlay.textBaseline	 = 'middle';
			gDraw.overlay.fillStyle = "rgb(255, 255, 255)";
			switch(gInputType)
			{
			case EInputType.Pad:
				var x = 1.4;
				var y = 0.9;
				var buttonSize = 1.1
				var labelOffset = 0.4;
				var vertSpace = 1.5;
				gDraw.overlay.textAlign	 = 'right';
				gDraw.overlay.fillText("Move", this.GetDrawX(x + labelOffset), this.GetDrawAltY(y) + this.GetSpacing());
				gDraw.overlay.fillText("Restart", this.GetDrawX(x + labelOffset), this.GetDrawAltY(y + vertSpace) + this.GetSpacing());
				gDraw.overlay.fillText("Undo", this.GetDrawX(x + labelOffset), this.GetDrawAltY(y + vertSpace * 2) + this.GetSpacing());
				this.DrawPadButton(null, this.GetDrawX(x), this.GetDrawAltY(y));
				this.DrawPadButton(EPadButtonCode.y, this.GetDrawX(x), this.GetDrawAltY(y + vertSpace));
				this.DrawPadButton(EPadButtonCode.x, this.GetDrawX(x), this.GetDrawAltY(y + vertSpace * 2));
				break;
			case EInputType.Touch:
				gDraw.overlay.textAlign	 = 'right';
				gDraw.overlay.fillText("Touch and drag slowly to move", this.GetDrawX(1), this.GetDrawAltY(1));
				break;
			default:
				var x = 1.4;
				var y = 0.9;
				var buttonSize = 1.1
				var labelOffset = 0.4;
				var vertSpace = 1.5;
				gDraw.overlay.textAlign	 = 'right';
				gDraw.overlay.fillText("Move", this.GetDrawX(x + (buttonSize * 2) + labelOffset), this.GetDrawAltY(y + buttonSize) + this.GetSpacing());
				gDraw.overlay.fillText("Restart", this.GetDrawX(x + labelOffset), this.GetDrawAltY(y + buttonSize + vertSpace) + this.GetSpacing());
				gDraw.overlay.fillText("Undo", this.GetDrawX(x + labelOffset), this.GetDrawAltY(y + buttonSize + vertSpace * 2) + this.GetSpacing());
				switch(gKeyboardControls)
				{
					case EKeyboardControls.wasd:
					this.DrawKey(EKeyCode.w,this.GetDrawX(x + buttonSize),this.GetDrawAltY(y));
					this.DrawKey(EKeyCode.d,this.GetDrawX(x),this.GetDrawAltY(y + buttonSize));
					this.DrawKey(EKeyCode.s,this.GetDrawX(x + buttonSize),this.GetDrawAltY(y + buttonSize));
					this.DrawKey(EKeyCode.a,this.GetDrawX(x + buttonSize * 2),this.GetDrawAltY(y + buttonSize));
					this.DrawKey(EKeyCode.r,this.GetDrawX(x),this.GetDrawAltY(y + buttonSize + vertSpace));
					this.DrawKey(EKeyCode.z,this.GetDrawX(x),this.GetDrawAltY(y + buttonSize + vertSpace * 2));
					break;
					case EKeyboardControls.arrows:
					this.DrawKey(EKeyCode.up,this.GetDrawX(x + buttonSize),this.GetDrawAltY(y));
					this.DrawKey(EKeyCode.right,this.GetDrawX(x),this.GetDrawAltY(y + buttonSize));
					this.DrawKey(EKeyCode.down,this.GetDrawX(x + buttonSize),this.GetDrawAltY(y + buttonSize));
					this.DrawKey(EKeyCode.left,this.GetDrawX(x + buttonSize * 2),this.GetDrawAltY(y + buttonSize));
					this.DrawKey(EKeyCode.pagedown,this.GetDrawX(x),this.GetDrawAltY(y + buttonSize + vertSpace));
					this.DrawKey(EKeyCode.delete,this.GetDrawX(x),this.GetDrawAltY(y + buttonSize + vertSpace * 2));
					break;
				}
				break;
			}
			this.redraw = false;
		}
	}
	

}