//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
EOverlayMode =
{
	None : "none",
	Score : "score",
	Completed : "completed",
}

var gScoreOverlay = new ScoreOverlay;

function ScoreOverlay()
{
	this.scoreWidth = 0;
	this.redraw = true;
	this.completedText = "";
	this.mode = EOverlayMode.None;
	
	this.SetMode = function(mode)
	{
		gConsole.Assert(ValidMode(mode));

		this.redraw = true;
		this.DrawClear();
		this.mode = mode;
		
		switch(mode)
		{
			case EOverlayMode.Completed:
			{
				this.completedText = this.GetCompletedText();
			}
			break;
		}
	}
	
	
	function ValidMode(setMode)
	{
		for(var mode in EOverlayMode)
		{
			if(EOverlayMode[mode] == setMode)
			{
				return true;
			}
		}
		return false;
	}

	this.Clear = function()
	{
		this.displayedScore = 0;
		this.redraw = true;
	}
	
	this.Update = function()
	{
		var delta = gStatsSystem.GetStat(EStatsType.Score) - this.displayedScore
		if(delta)
		{
			var inc = Math.ceil(delta/20);
			this.displayedScore += inc;
			this.redraw = true;
		}
	}
	
	this.GetSpacing = function()
	{
		return kTileSize * 0.1;
	}
	
	this.GetFontSize = function()
	{
		return GetFontScale(26);
	}
	
	this.GetDrawX = function()
	{
		return gDraw.canvas.width - this.GetSpacing();
	}
	
	this.GetDrawY = function()
	{
		return gDraw.canvas.height - this.GetSpacing();
	}
	
	this.GetDrawCompletedX = function()
	{
		return kTileSize * 0.2;
	}
	
	this.GetDrawCompletedY = function()
	{
		return kTileSize * 0.1;
	}
	
	this.DrawClear = function()
	{
		var fontSize = this.GetFontSize();
		var spacing = this.GetSpacing();
		var x = this.GetDrawX();
		var y = this.GetDrawY();
		if(this.redraw)
		{
			var extraShadow = kTileSize * 0.12;
			if(this.mode == EOverlayMode.Score)
			{
				var height = this.GetFontSize();
				var width = this.scoreWidth;
				gDraw.overlay.clearRect(x - width - (extraShadow/2), y - height, width + extraShadow, height + extraShadow);
				//gDraw.context.rect(x - width - (extraShadow/2), y - height, width + extraShadow, height + extraShadow);
				//gDraw.context.fillStyle = "rgb(255, 0, 0)";
				//gDraw.context.fill();
			}
			else if(this.mode == EOverlayMode.Completed)
			{
				x = this.GetDrawCompletedX();
				y = this.GetDrawCompletedY();
				var width = kTileSize * 3;
				var height = this.GetFontSize() * 2;

				gDraw.overlay.clearRect(x, y, width, height - extraShadow);
				//gDraw.context.rect(x, y, width, height - extraShadow);
				//gDraw.context.fillStyle = "rgb(255, 0, 0)";
				//gDraw.context.fill();
			}
		}
	}
	
	this.Draw = function()
	{
		if(this.redraw && this.mode != EOverlayMode.None)
		{
			var fontSize = this.GetFontSize();
			var spacing = this.GetSpacing();
			gDraw.overlay.font = GetFont(fontSize);
			if(this.mode == EOverlayMode.Score)
			{
				var x = this.GetDrawX();
				var y = this.GetDrawY();
				gDraw.overlay.textAlign	 = 'right';
				gDraw.overlay.textBaseline	 = 'alphabetic';
				gDraw.overlay.fillTextShadow(this.displayedScore, x, y, "rgb(255, 255, 255)", "rgb(0, 0, 0)");
				this.scoreWidth = gDraw.overlay.measureText(this.displayedScore).width;
				this.redraw = false;
			}
			else if(this.mode == EOverlayMode.Completed)
			{
				var x = this.GetDrawCompletedX();
				var y = this.GetDrawCompletedY();
				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.textAlign	 = 'left';
				gDraw.overlay.textBaseline	 = 'hanging';
				var outerRadius = kTileSize * 0.2;
				var starSpace = kTileSize * 0.06;

				gDraw.overlay.fillText(this.completedText, x, y);
				var width = gDraw.overlay.measureText(this.completedText).width;
				gDraw.overlay.fillStar(x + width + outerRadius + starSpace, y + fontSize - (outerRadius * 1.5), outerRadius);
				
				this.redraw = false;
			}
		}
	}
	
	this.GetCompletedText = function()
	{
		var set = gLevelSystem.GetLevelSet();
		if(set == "hub")
		{
			return gStatsSystem.GetStat(EStatsType.Stars) + " of " + (gLevelsCount * gRulesSystem.kStarCount);
		}
		else
		{
			return gStatsSystem.GetStarsForSet(set) + " of " + (gStatsSystem.GetCountForSet(set) * gRulesSystem.kStarCount);
		}
	}
}
