//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 gObjectivesOverlay = new ObjectivesOverlay;

function ObjectiveData()
{
	this.Clear = function()
	{
		this.completable = true;
		this.display = null;
		this.width = 0;
	}
	
	this.Update = function(display, completable)
	{
		if(this.completable != completable)
		{
			this.completable = completable;
		}
		if(display != this.display)
		{
			this.display = display;
			return true;
		}
		return false;
	}
	
	this.Clear();
}

function ObjectivesOverlay()
{
	this.enabled = false;
	this.redraw = true;
	
	this.starArrived = new Array();
	this.starArrivedSound = new Array();
	for(var i = 1; i <= gRulesSystem.kStarCount; i++)
	{
		this.starArrivedSound[i] = gSounds.Get("starHit" + i);
	}
	this.objectiveData = new Array();
	for(var i = 1; i <= gRulesSystem.kStarCount; i++)
	{
		this.objectiveData[i] = new ObjectiveData();
	}
	
	this.previousStars = null;

	this.Enable = function()
	{
		this.enabled = true;
		this.redraw = true;
		for(var i = 1; i <= gRulesSystem.kStarCount; i++)
		{
			this.objectiveData[i].Update(gRulesSystem.GetStarDescription(i), gRulesSystem.GetStarCompletable(i));
		}
		var prevStars = gStorage.getItem(gLevelSystem.GetLevelSet(), gLevelSystem.GetLevelName(), "stars");
		if(prevStars)
		{
			this.previousStars = parseInt(prevStars);
		}
		this.starArrived = new Array();
	}
	
	this.Disable = function()
	{
		this.enabled = false;
		this.redraw = true;
		this.DrawClear();
		this.redraw = false;
		for(var i = 1; i <= gRulesSystem.kStarCount; i++)
		{
			this.objectiveData[i].Clear();
		}
		this.previousStars = null;
	}
	
	this.StarArrived = function(index)
	{
		this.starArrivedSound[index].Play();
		this.starArrived[index] = true;
		this.redraw = true;
	}
	
	this.Update = function()
	{
		var redraw = false;
		for(var i = 1; i <= gRulesSystem.kStarCount; i++)
		{
			redraw = redraw || this.objectiveData[i].Update(gRulesSystem.GetStarDescription(i), gRulesSystem.GetStarCompletable(i));
		}

		if(redraw)
		{
			this.redraw = true;
		}
	}
	
	this.GetStarRadius = function()
	{
		return kTileSize * 0.2;
	}
	
	this.GetStarX = function(star)
	{
		return 5 + this.GetStarRadius();
	}
	
	this.GetStarY = function(star)
	{
		return this.GetTextY(star) - this.GetStarRadius() + 3;
	}
	
	this.GetTextX = function(star)
	{
		return 8 + (this.GetStarRadius() * 2);
	}
	
	this.GetTextY = function(star)
	{
		var y = 0;
		for(var i = star; i > 0; i--)
		{
			y += this.GetFontSize(i) + 5;
		}
		return y;
	}
	
	this.GetFontSize = function(star)
	{
		return GetFontScale(20);
	}
	
	this.DrawClear = function()
	{
		if(this.redraw)
		{
			for(var i = 1; i <= gRulesSystem.kStarCount; i++)
			{
				this.DrawClearObjective(i);
			}
		}
	}
	
	this.Draw = function()
	{
		if(this.enabled && this.redraw)
		{
			gDraw.overlay.save();
			gDraw.overlay.textAlign	 = 'left';
			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.strokeStyle = "rgb(255, 255, 255)";
			gDraw.overlay.fillStyle = "rgb(255, 255, 255)";

			for(var i = 1; i <= gRulesSystem.kStarCount; i++)
			{
				this.DrawObjective(i);
			}
			gDraw.overlay.restore();
			this.redraw = false;
		}
	}
	
	this.DrawObjective = function(index)
	{
		var objectiveData = this.objectiveData[index];
		if(objectiveData.display)
		{
			var radius = this.GetStarRadius();
			var starX = this.GetStarX(index);
			var starY = this.GetStarY(index);
			var fontSize = this.GetFontSize(index);
			var x = this.GetTextX(index);
			var y = this.GetTextY(index);
			if(objectiveData.completable)
			{
				gDraw.overlay.Star(starX, starY, radius);
				var completeStar = index <= this.previousStars || this.starArrived[index];
				if(completeStar)
				{
					gDraw.overlay.fill();
				}
				else
				{
					gDraw.overlay.stroke();
				}
				gDraw.overlay.font	 = GetFont(fontSize);
				gDraw.overlay.fillText(objectiveData.display, x, y);
				objectiveData.width = gDraw.overlay.measureText(objectiveData.display).width;
			}
		}
	}
	
	this.DrawClearObjective = function(index)
	{
		var objectiveData = this.objectiveData[index];
		if(objectiveData && objectiveData.width > 0)
		{
			var extraShadow = 6;
			var radius = this.GetStarRadius();
			var textx = this.GetStarX(index) - radius;
			var texty = this.GetTextY(index);
			var height = this.GetFontSize(index);
			var width = objectiveData.width + (radius * 3);
			gDraw.overlay.clearRect(textx - (extraShadow/2), texty - height, width + extraShadow, height + extraShadow);
			//gDraw.overlay.rect(textx - (extraShadow/2), texty - height, width + extraShadow, height + extraShadow);
			//gDraw.overlay.fillStyle = "rgb(255, 0, 0)";
			//gDraw.overlay.fill();
		}
	}
}
