//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 gIconsOverlay = new IconsOverlay;

function IconsOverlay()
{
	this.enabled = false;
	this.icons = new Array();
	
	this.Init = function()
	{
		if(gMobile)
		{
			if (gLevelSystem.IsMenu())
			{
				this.Disable();
			}
			else
			{
				this.Resize();
	
				this.Enable();
			}
		}
	}
	
	this.Enable = function()
	{
		if(gMobile)
		{
			this.enabled = true;
			for(var i = 0; i < this.icons.length; i++)
			{
				this.icons[i].redraw = true;
			}
			var hub = (gLevelSystem.GetLevelName() == "hub")
			var arcade = gLevelSystem.IsArcade();
			this.icons[0].Enable(true);
			this.icons[1].Enable(gMenuSystem.CanHub());
			this.icons[2].Enable(gMenuSystem.CanRestart())
			this.icons[3].Enable(gMenuSystem.CanRestart())
		}
	}
	
	this.Disable = function()
	{
		this.enabled = false;
		for(var i = 0; i < this.icons.length; i++)
		{
			this.icons[i].Enable(false);
		}
	}
	
	this.Redraw = function()
	{
		for(var i = 0; i < this.icons.length; i++)
		{
			this.icons[i].redraw = true;
		}
	}
	
	this.Resize = function()
	{
		this.icons = new Array();
		var iconSize = this.GetIconSize();
		this.icons.push(new PauseIcon(this.GetIconX(1), this.GetIconY(), iconSize, iconSize, function() { gMenuSystem.Pause(true); }));
		this.icons.push(new HubIcon(this.GetIconX(2), this.GetIconY(), iconSize, iconSize, function() { gMenuSystem.Hub(); }));
		this.icons.push(new RestartIcon(this.GetIconX(3), this.GetIconY(), iconSize, iconSize, function() { gMenuSystem.Restart(); }));
		this.icons.push(new UndoIcon(this.GetIconX(4), this.GetIconY(), iconSize, iconSize, function() { gUndoSystem.EndUndoing(); }, function() { gUndoSystem.StartUndoing(); }));
	}
	
	this.InteractMove = function(x, y)
	{
		for(var i = 0; i < this.icons.length; i++)
		{
			this.icons[i].InteractMove(x, y);
		}
	}
	
	this.InteractDown = function(x, y)
	{
		for(var i = 0; i < this.icons.length; i++)
		{
			this.icons[i].InteractDown(x, y);
		}
	}
	
	this.InteractRelease = function(x, y)
	{
		for(var i = 0; i < this.icons.length; i++)
		{
			this.icons[i].InteractRelease(x, y);
		}
	}
	
	this.InteractMoveOut = function()
	{
		for(var i = 0; i < this.icons.length; i++)
		{
			this.icons[i].InteractMoveOut();
		}
	}
	
	this.GetIconSize = function()
	{
		return (kTileSize) - (this.GetPadding() * 3);
	}
	
	this.GetPadding = function()
	{
		return kTileSize * 0.1;
	}
		
	this.GetIconX = function(index)
	{
		var padding = this.GetPadding();
		var iconSize = this.GetIconSize();
		return gDraw.canvas.width - (index * (iconSize + padding));
	}
	
	this.GetIconY = function()
	{
		return this.GetPadding();
	}
	
	this.DrawClear = function()
	{
		if(gMobile)
		{
			for(var i = 0; i < this.icons.length; i++)
			{
				if (this.icons[i].redraw && this.icons[i].enabled)
				{
					this.icons[i].DrawClear();
				}
			}
		}
	}
	
	this.Draw = function()
	{
		if(this.enabled)
		{
			for(var i = 0; i < this.icons.length; i++)
			{
				if (this.icons[i].redraw && this.icons[i].enabled)
				{
					this.icons[i].Draw();
				}
			}
		}
	}
}

function PauseIcon(x, y, width, height, callback, downCallback)
{
    this.__proto__ = new Icon(x, y, width, height, callback, downCallback);
	
	this.Draw = function()
	{
		this.DrawBackground();
		var x = this.GetX();
		var y = this.GetY();

		var padding = kTileSize * 0.1;
		gDraw.menu.lineWidth = padding;
		gDraw.menu.lineCap = 'round';
		gDraw.menu.lineJoin = 'round';
		var isPaused = gPause.IsPaused(EPauseReason.Player);
		if (isPaused)
		{
			padding = kTileSize * 0.15;
			gDraw.menu.beginPath();
			gDraw.menu.moveTo(x + padding, y + padding);
			gDraw.menu.lineTo(x + this.width - padding, y + (this.height/2));
			gDraw.menu.lineTo(x + padding, y + this.height - padding);
			gDraw.menu.closePath();
			gDraw.menu.fillStyle = "rgb(255, 255, 255)";
			gDraw.menu.fill();
		}
		else
		{
			gDraw.menu.beginPath();
			var verticalPadding = (padding * 1.5)
			gDraw.menu.moveTo(x + (this.width/2) - padding, y + verticalPadding);
			gDraw.menu.lineTo(x + (this.width/2) - padding, y + this.height - verticalPadding);
			gDraw.menu.moveTo(x + (this.width/2) + padding, y + verticalPadding);
			gDraw.menu.lineTo(x + (this.width/2) + padding, y + this.height - verticalPadding);
			gDraw.menu.strokeStyle = "rgb(255, 255, 255)";
			gDraw.menu.stroke();
		}
	}
}

function RestartIcon(x, y, width, height, callback, downCallback)
{
    this.__proto__ = new Icon(x, y, width, height, callback, downCallback);
	
	this.Draw = function()
	{
		this.DrawBackground();
		
		var x = this.GetX();
		var y = this.GetY();
		var padding = kTileSize * 0.1;
		var radius = padding * 2;
		gDraw.menu.lineWidth = padding;
		gDraw.menu.lineCap = 'round';
		gDraw.menu.strokeStyle = "rgb(255, 255, 255)";
		
		gDraw.menu.beginPath();
		gDraw.menu.arc(x + (this.width/2), y + (this.height/2), radius, Math.PI * 0.5, Math.PI * 1.2, false);
		gDraw.menu.stroke();

		gDraw.menu.beginPath();
		gDraw.menu.arc(x + (this.width/2), y + (this.height/2), radius, Math.PI * 1.5, Math.PI * 2.2, false);
		gDraw.menu.stroke();
	}
}

function UndoIcon(x, y, width, height, callback, downCallback)
{
    this.__proto__ = new Icon(x, y, width, height, callback, downCallback);
	
	this.Draw = function()
	{
		this.DrawBackground();
		
		var x = this.GetX();
		var y = this.GetY();
		var padding = kTileSize * 0.1;
		var radius = padding * 2;
		gDraw.menu.lineWidth = padding;
		gDraw.menu.lineCap = 'round';
		gDraw.menu.strokeStyle = "rgb(255, 255, 255)";
		
		gDraw.menu.beginPath();
		gDraw.menu.arc(x + (this.width/2), y + (this.height/2), radius, Math.PI * 1.2, Math.PI * 2.2, false);
		gDraw.menu.stroke();
	}
}

function HubIcon(x, y, width, height, callback, downCallback)
{
    this.__proto__ = new Icon(x, y, width, height, callback, downCallback);
	
	this.Draw = function()
	{
		this.DrawBackground();
		var x = this.GetX();
		var y = this.GetY();
		var padding = kTileSize * 0.1;
		var radius = padding * 2;
		gDraw.menu.lineWidth = padding;
		gDraw.menu.strokeStyle = "rgb(255, 255, 255)";
		
		gDraw.menu.beginPath();
		gDraw.menu.arc(x + (this.width/2), y + (this.height/2), radius, 0, Math.PI * 2, false);
		gDraw.menu.stroke();
	}
}