//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

function Button(name, callback)
{
	this.__proto__ = new MenuItem(name);
	
	this.onPressCallback = callback;

	this.locked = false;
	this.mouseOver = false;
	this.mouseDown = false;
	this.updatedColor = false;
	this.color = new RGB(0, 0, 0);
	this.redraw = true;

	this.highlightSound = gSounds.Get("menuHighlight");
	this.selectSound = gSounds.Get("menuSelect");
	
	this.CanSelect = function()
	{
		return true;
	}
	
	this.PlayHighlightSound = function()
	{
		this.highlightSound.Play();
	}
	
	this.CanInteract = function(x, y)
	{
		var widthD2 = this.width/2;
		if((this.x - widthD2) < x && x < (this.x + widthD2))
		{
			var heightD2 = this.height/2;
			if((this.y - heightD2) < y && y < (this.y + heightD2))
			{
				return true;
			}
		}
		return false;
	}
	
	this.InteractRelease = function (x, y)
	{
		if(this.locked)
		{
			gConsole.Log(7, "[BTN] pressed but locked " + this.name);
			return false;
		}
		
		if(this.CanInteract(x, y))
		{
			this.Interact();
			return true;
		}

		return false;
	}
	
	this.Interact = function()
	{
		gConsole.Log(5, "[BTN] Callback - Pressed " + this.name);
		this.selectSound.Play();
		this.onPressCallback(this);
	}

	this.GetDrawX = function()
	{
		return this.x - (this.width/2);
	}

	this.GetDrawY = function()
	{
		return this.y - (this.height/2);
	}

	this.Redraw = function()
	{
		this.redraw = true;
	}

	this.DrawClear = function()
	{
		if(this.redraw)
		{
			var clearPadding = 20;
			var x = this.GetDrawX() - clearPadding/2;
			var y = this.GetDrawY() - clearPadding/2;
			gDraw.menu.clearRect(x, y, this.width + clearPadding, this.height + clearPadding);
		}
	}
	
	this.Draw = function(selected)
	{
		if(this.redraw)
		{
			if(this.updatedColor == false)
			{
				this.color.Copy(this.GetRGBColor());
				this.updatedColor = true;
			}

			gDraw.menu.save();
	
			var shadowOffset = selected ? -1 : 1;
			gDraw.menu.shadowOffsetX = shadowOffset;
			gDraw.menu.shadowOffsetY = shadowOffset;
			gDraw.menu.shadowBlur = 2;
			gDraw.menu.shadowColor = "rgb(0, 0, 0)";
			gDraw.menu.fillStyle = this.color.GetColor();
			var additional = selected ? 1 : 0;
			var x = this.GetDrawX() + additional;
			var y = this.GetDrawY() + additional;
			gDraw.menu.fillRoundedRect(x, y, this.width, this.height, kTileSize * 0.14);
	
			this.DrawText(selected);
	
			gDraw.menu.restore();
			this.redraw = false;
		}
	}
	
	this.DrawText = function(selected)
	{
		var additional = selected ? 1 : 0;
		gDraw.menu.font	 = GetFont(this.fontSize);
		gDraw.menu.textAlign	 = 'center';
		gDraw.menu.textBaseline	 = 'middle';
		gDraw.menu.fillTextShadow(this.name, this.x + additional, this.y + additional, this.GetTextColor(!this.locked), this.GetTextColor(this.locked));
	}
	
	this.ForceUpdate = function()
	{
		this.updatedColor = false;
		this.Update(false);
	}
	
	this.Update = function(selected)
	{
		if(!this.locked)
		{
			var updated = false;
			if(selected)
			{
				var highlightColor = this.GetRGBHighlightColor();
				if(!this.color.Equal(highlightColor))
				{
					this.color.Copy(highlightColor);
					updated = true;
				}
			}
			else
			{
				var baseColor = this.GetRGBColor();
				if(!this.color.Equal(baseColor))
				{
					this.color.Copy(baseColor);
					updated = true;
				}
			}
			this.redraw = this.redraw || updated;
		}
	}
	
	this.GetRGBColor = function()
	{
		return gStyles.baseColor;
	}
	
	this.GetRGBHighlightColor = function()
	{
		return this.GetRGBColor().GetAdjusted(128);
	}
	
	this.SetHighlighted = function()
	{
		this.color = this.GetRGBHighlightColor();
	}

	this.GetTextColor = function(white)
	{
		return white ? "rgb(255, 255, 255)" : "rgb(0, 0, 0)";
	}
}
