//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 gHintsOverlay = new HintsOverlay;

function HintsOverlay()
{
	this.fullHint = null;
	this.hint = null;
	this.hint2 = null;
	this.width = 0;
	this.redraw = false;
	
	this.Init = function()
	{
		this.Set("");
	}
	
	this.Set = function(hint)
	{
		var func = this[hint]
		if(typeof func === 'function')
		{
			hint = func();
		}
		if(hint != this.fullHint)
		{
			this.fullHint = hint;
			var hintsArray = hint.split("\\n", 2);
			switch(hintsArray.length)
			{
				case 1:
					this.hint = hintsArray[0];
					this.hint2 = null;
					break;
				case 2:
					this.hint = hintsArray[1];
					this.hint2 = hintsArray[0];
					break;
			}
			this.redraw = true;
		}
	}
	
	this.Update = function()
	{
	}
	
	this.GetTextX = function()
	{
		return (gDraw.canvas.width/2) + (kTileSizeD2);
	}
	
	this.GetTextY = function()
	{
		return gDraw.canvas.height - (this.GetFontSize() * 0.5);
	}
	
	this.GetText2Y = function()
	{
		return gDraw.canvas.height - (this.GetFontSize() * 1.5);
	}
	
	this.GetFontSize = function()
	{
		return GetFontScale(20);
	}
	
	this.DrawClear = function()
	{
		if(this.redraw)
		{
			var extraShadow = kTileSize * 0.16;
			var textx = this.GetTextX();
			var texty = this.GetTextY();
			var height = this.GetFontSize() * 2;
			var width = this.width + (kTileSize*2);
			gDraw.overlay.clearRect(textx - (width/2) - (extraShadow/2), texty - height, width + extraShadow, height + extraShadow);
			//gDraw.overlay.rect(textx - (width/2) - (extraShadow/2), texty - height, width + extraShadow, height + extraShadow);
			//gDraw.overlay.fillStyle = "rgb(255, 0, 0)";
			//gDraw.overlay.fill();
		}
	}
	
	this.Draw = function()
	{
		if(this.redraw && this.hint)
		{
			gDraw.overlay.save();
			var fontSize = this.GetFontSize();
			var x = this.GetTextX();
			var y = this.GetTextY();
			gDraw.overlay.font	 = GetFont(fontSize);
			gDraw.overlay.textAlign	 = 'center';
			gDraw.overlay.textBaseline	 = 'alphabetic';
			gDraw.overlay.shadowOffsetX = 1;
			gDraw.overlay.shadowOffsetY = 1;
			gDraw.overlay.shadowBlur = 2;
			gDraw.overlay.shadowColor = "rgb(0, 0, 0)";

			gDraw.overlay.fillStyle = "rgb(255, 255, 255)";
			gDraw.overlay.fillText(this.hint, x, y);

			this.width = gDraw.overlay.measureText(this.hint).width;

			
			if(this.hint2 != null)
			{
				var y2 = this.GetText2Y();
				gDraw.overlay.fillText(this.hint2, x, y2);
				var hint2Width = gDraw.overlay.measureText(this.hint2).width;
				this.width = Math.max(hint2Width, this.width);
				
				DrawHintIcon(x - (hint2Width/2) - (kTileSizeD2/2)-(kTileSize*0.04), y2-(kTileSizeD2/2)+(kTileSize*0.1), gDraw.overlay, true, 0.5);
			}
			else if(this.width > 0)
			{
				DrawHintIcon(x - (this.width/2) - (kTileSizeD2/2)-(kTileSize*0.04), y-(kTileSizeD2/2)+(kTileSize*0.1), gDraw.overlay, true, 0.5);
			}

			this.redraw = false;
			gDraw.overlay.restore();
		}
	}
	
	this.HowToMoveHint = function()
	{
		switch(gInputType)
		{
			case EInputType.Pad:
				return "Use the analog stick or dpad to move";
			case EInputType.Touch:
				return "Touch anywhere and slowly drag your finger to move";
			default:
				var keys = KeyToString(EKeyCode.w) + KeyToString(EKeyCode.a) + KeyToString(EKeyCode.s) + KeyToString(EKeyCode.d);
				var altKeys = KeyToString(EKeyCode.left) + KeyToString(EKeyCode.up) + KeyToString(EKeyCode.down) + KeyToString(EKeyCode.right);
				return "Hold " + keys + " or " + altKeys + " keys to move";
		}
	}
	
	this.UndoHint = function()
	{
		switch(gInputType)
		{
			case EInputType.Pad:
				return "Undo your actions by holding X or LT";
			case EInputType.Touch:
				return "Undo your actions by pressing the highlighted button in the top right";
			default:
				var key = gKeyboardControls == EKeyboardControls.wasd ? EKeyCode.z : EKeyCode.delete;
				return "Undo your actions by holding " + KeyToString(key)
		}
	}
	
	this.RestartHint = function()
	{
		switch(gInputType)
		{
			case EInputType.Pad:
				return "Restart levels by pressing Y or Back";
			case EInputType.Touch:
				return "Restart levels by pressing the highlighted button in the top right";
			default:
				var key = gKeyboardControls == EKeyboardControls.wasd ? EKeyCode.r : EKeyCode.pagedown;
				return "Restart levels by pressing " + KeyToString(key)
		}
	}
}
