//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 StatsBlocks()
{
	this.__proto__ = new MenuItem("Blocks");
	this.redraw = true;
	
	this.GetDrawX = function()
	{
		return this.x - (this.width/2);
	}

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

	this.Redraw = function()
	{
		this.redraw = true;
	}
	
	this.DrawClear = function()
	{
		if(this.redraw)
		{
			gDraw.menu.clearRect(this.GetDrawX(), this.GetDrawY(), this.width, this.height);
		}
	}
	
	this.Draw = function()
	{
		if(this.redraw)
		{
			gDraw.menu.save();
			var textOffset = (this.fontSize * 1.5);
			gDraw.menu.translate(this.GetDrawX(), this.GetDrawY() + textOffset);
			this.DrawBlocks(this.width, this.height - textOffset);
			gDraw.menu.restore();
			
			gDraw.menu.font	 = GetFont(this.fontSize);
			gDraw.menu.textAlign	 = 'left';
			gDraw.menu.textBaseline	 = 'top';
			var text = this.GetStatValue(EStatsType.DeletedBlocks) + " Blocks cleared";
			gDraw.menu.fillTextShadow(text, this.GetDrawX(), this.GetDrawY(), "rgb(255, 255, 255)", "rgb(0, 0, 0)");
			
			
			gDraw.menu.textAlign	 = 'right';
			var text = this.GetStatValue(EStatsType.Pushes) + " Pushes"
			gDraw.menu.fillTextShadow(text, this.GetDrawX() + this.width, this.GetDrawY(), "rgb(255, 255, 255)", "rgb(0, 0, 0)");

			this.redraw = false;
		}
	}
	
	this.GetStatValue = function(stat)
	{
		return gStatsSystem.GetLifetimeStat(stat) + gStatsSystem.GetStat(stat);
	}
	
	this.DrawBlocks = function(width, height)
	{
		gDraw.menu.shadowOffsetX = 1;
		gDraw.menu.shadowOffsetY = 1;
		gDraw.menu.shadowBlur = 2;
		gDraw.menu.shadowColor = "rgb(0, 0, 0)";

		//gDraw.menu.rect(0, 0, width, height);
		
		var blocks = this.GetStatValue(EStatsType.DeletedBlocks)
		
		var horizontalAreas = 5;
		var verticalAreas = 1;
		var areaSize = height * 0.92;
		while(blocks > horizontalAreas * verticalAreas)
		{
			horizontalAreas *= 2;
			verticalAreas *= 2;
			areaSize *= 0.5;
		}
		
		var spacing = (width - (horizontalAreas * areaSize))/Math.max(horizontalAreas - 1, 1);
		
		for(var y = 0; y < verticalAreas; y++)
		{
			for(var x = 0; x < horizontalAreas; x++)
			{
				if(x + (horizontalAreas * y) < blocks)
				{
					var rgb = gStyles.GetBlockRGB(EBlockType.Normal, Math.round(Math.randomRange(1, 9)), false);
					gDraw.menu.fillStyle = gStyles.GetColor(rgb);
					gDraw.menu.fillRoundedRect(x * (areaSize + spacing), y * (areaSize + spacing), areaSize, areaSize, areaSize * 0.14);
				}
			}
		}
	}
}

function StatsStars()
{
	this.__proto__ = new MenuItem("Stars");
	this.redraw = true;
	
	this.GetDrawX = function()
	{
		return this.x - (this.width/2);
	}

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

	this.Redraw = function()
	{
		this.redraw = true;
	}
	
	this.DrawClear = function()
	{
		if(this.redraw)
		{
			gDraw.menu.clearRect(this.GetDrawX(), this.GetDrawY(), this.width, this.height);
		}
	}
	
	this.Draw = function()
	{
		if(this.redraw)
		{
			gDraw.menu.save();
			var textOffset = (this.fontSize * 1.5);
			gDraw.menu.translate(this.GetDrawX(), this.GetDrawY() + textOffset);
			this.DrawStars(this.width, this.height - textOffset)
			gDraw.menu.restore();
			
			gDraw.menu.font	 = GetFont(this.fontSize);
			gDraw.menu.textAlign	 = 'left';
			gDraw.menu.textBaseline	 = 'top';
			var text = this.GetStarsText();
			gDraw.menu.fillTextShadow(text, this.GetDrawX(), this.GetDrawY(), "rgb(255, 255, 255)", "rgb(0, 0, 0)");
			
			
			gDraw.menu.textAlign	 = 'right';
			var text = this.GetBetterText();
			if(text)
			{
				gDraw.menu.fillTextShadow(text, this.GetDrawX() + this.width, this.GetDrawY(), "rgb(255, 255, 255)", "rgb(0, 0, 0)");
			}
			
			this.redraw = false;
		}
	}
	
	this.GetStarsText = function()
	{
		return gStatsSystem.GetStat(EStatsType.Stars) + " of " + (gLevelsCount * gRulesSystem.kStarCount) + " Stars";
	}
	
	this.GetBetterText = function()
	{
		var betters = gStatsSystem.GetStat(EStatsType.Betters);
		if(betters > 0)
		{
			var plural = betters != 1 ? "Incredibles!" : "Incredible!";
			return betters + " " + plural;
		}
		else
		{
			return null;
		}
	}
	
	this.DrawStars = function(width, height)
	{
		gDraw.menu.shadowOffsetX = 1;
		gDraw.menu.shadowOffsetY = 1;
		gDraw.menu.shadowBlur = 2;
		gDraw.menu.shadowColor = "rgb(0, 0, 0)";

		gDraw.menu.strokeStyle = "rgb(255, 255, 255)";
		gDraw.menu.fillStyle = "rgb(255, 255, 255)";
		
		var maxSize = kTileSize * 0.5;
		
		var starRadius = kTileSize * 0.2;
		var betterRadius = kTileSize * 0.25;
		
		var padding = maxSize/2;
		var curX = padding;
		var curY = padding;
	
		for(levelSet in gLevelsBySet)
		{
			if(gLevelsBySet.hasOwnProperty(levelSet) && !IsHubSet(levelSet))
			{
				for(var i = 0; i < gLevelsBySet[levelSet].length; i++)
				{
					var stars = 0;
					var levelName = gLevelsBySet[levelSet][i];
					var storedStars = gStorage.getItem(levelSet, levelName, "stars");
					if(storedStars)
					{
						stars = parseInt(storedStars);
					}
					var better = gStorage.getItem(levelSet, levelName, "better");
					
					for(var index = 1; index <= gRulesSystem.kStarCount; index++)
					{
						gDraw.menu.Star(curX, curY, starRadius);
						if(index <= stars)
						{
							gDraw.menu.fill();
						}
						else
						{
							gDraw.menu.stroke();
						}
						if(better)
						{
							gDraw.menu.Star(curX, curY, betterRadius);
							gDraw.menu.stroke();
						}
						
						curX += maxSize;
						if(curX > width)
						{
							curY += maxSize;
							curX = padding;
						}
					}
				}
			}
		}
	}
}

function StatsTime(update)
{
	this.__proto__ = new MenuItem("StatsTime");
	this.textWidth = 0;
	this.update = update;
	this.text = undefined;
	
	this.GetDrawX = function()
	{
		return this.x + (this.width/2);
	}

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

	this.Redraw = function()
	{
		
	}

	this.DrawClear = function()
	{
		var clearPadding = 10;
		var x = this.GetDrawX() - this.textWidth - clearPadding/2;
		var y = this.GetDrawY() - this.height/2 - clearPadding/2;
		gDraw.menu.clearRect(x, y, this.textWidth + clearPadding, this.height + clearPadding);
		//gDraw.menu.rect(x, y, this.textWidth + clearPadding, this.height + clearPadding);
		//gDraw.menu.fillStyle = "rgb(255, 0, 0)";
		//gDraw.menu.fill();
	}
	
	this.Draw = function()
	{
		gDraw.menu.save();
		
		if (this.update)
		{
			this.Update();
		}

		gDraw.menu.font	 = GetFont(this.fontSize);
		gDraw.menu.textAlign	 = 'right';
		gDraw.menu.textBaseline	 = 'middle';
		gDraw.menu.fillTextShadow(this.text, this.GetDrawX(), this.GetDrawY(), "rgb(255, 255, 255)", "rgb(0, 0, 0)");
		this.textWidth = gDraw.menu.measureText(this.text).width;
		gDraw.menu.restore();
	}
	
	this.Update = function()
	{
		this.seconds = gStatsSystem.GetLifetimeStat("time") + gStatsSystem.GetStat("time");
		this.text = "Time " + SecondsToString(this.seconds);
	}

	this.Update();	//Always update on creation
}

function StatsThanks()
{
	this.__proto__ = new MenuItem("StatsThanks");
	this.textWidth = 0;
	
	this.GetDrawX = function()
	{
		return this.x;
	}

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

	this.Redraw = function()
	{
		
	}

	this.DrawClear = function()
	{
		var clearPadding = 10;
		var x = this.GetDrawX() - this.textWidth/2 - clearPadding/2;
		var y = this.GetDrawY() - this.height/2 - clearPadding/2;
		gDraw.menu.clearRect(x, y, this.textWidth + clearPadding, this.height + clearPadding);
		//gDraw.menu.rect(x, y, this.textWidth + clearPadding, this.height + clearPadding);
		//gDraw.menu.fillStyle = "rgb(255, 0, 0)";
		//gDraw.menu.fill();
	}
	
	this.Draw = function()
	{
		if(gStatsSystem.GetStat(EStatsType.Stars) == (gLevelsCount * gRulesSystem.kStarCount))
		{
			gDraw.menu.save();
	
			gDraw.menu.font	 = GetFont(this.fontSize);
			gDraw.menu.textAlign	 = 'center';
			gDraw.menu.textBaseline	 = 'middle';
			var text = "Thank you for playing!";
			gDraw.menu.fillTextShadow(text, this.GetDrawX(), this.GetDrawY(), "rgb(255, 255, 255)", "rgb(0, 0, 0)");
			this.textWidth = gDraw.menu.measureText(text).width;
	
			gDraw.menu.restore();
		}
	}
}

function StatsMenu()
{
	this.__proto__ = new Menu("StatsMenu", EMenuLayout.Grid, new MenuGrid(5, 18))
	
	this.Registered = function()
	{
		this.menuItems = new Array();
		this.statsStars = new StatsStars();
		
		this.AddMenuItem(this.statsStars, EMenuItemStyle.StatsBlock, new Vec2(2, 3));
		this.AddMenuItem(new StatsBlocks(), EMenuItemStyle.StatsBlock, new Vec2(2, 10));
		this.AddMenuItem(new StatsThanks(), EMenuItemStyle.StatsText, new Vec2(2, 14));
		this.AddMenuItem(new StatsTime(true), EMenuItemStyle.StatsText, new Vec2(2, 14));
	
		if(gDemo)
		{
			this.AddMenuItem(new Button("Purchase",
			function()
			{
				window.open("http://www.polyologygame.com/play");
			}),
			EMenuItemStyle.Small, new Vec2(1, 16));
			
			this.AddMenuItem(new Button("Finished!",
			function()
			{
				gLevelSystem.LoadLevel("Menu", "demo");
			}),
			EMenuItemStyle.Small, new Vec2(3, 16));
		}
		else
		{
			this.AddMenuItem(new Button("Back", function() { gLevelSystem.LoadLevel("Menu", "Extras"); }), EMenuItemStyle.SmallThin, new Vec2(1, 16));
			this.AddMenuItem(new Button("Tweet", function()
			{
				var text = this.statsStars.GetStarsText();
				var better = this.statsStars.GetBetterText();
				if(better)
				{
					text += " with " + better;
				}
				text += " %23Polyology"
				text += "%0A"
				text += "http://www.polyologygame.com"
				text.replace(" ", "%20");
				window.open("https://twitter.com/home?status=" + text);
			}.bind(this)), EMenuItemStyle.SmallThin, new Vec2(2, 16));
			
			this.AddMenuItem(new Button("Facebook", function()
			{
				window.open("http://www.facebook.com/sharer/sharer.php?u=http://www.polyologygame.com");
			}.bind(this)), EMenuItemStyle.SmallThin, new Vec2(3, 16));
		}
		this.__proto__.Registered.call(this);
	}

	this.Unregistered = function()
	{
		this.__proto__.Unregistered.call(this);
	}
}

function LevelScript()
{
	this.Load = function()
	{
		this.menu = new StatsMenu();
		gMenuSystem.Register(this.menu);
	}
	
	this.Unload = function()
	{
		gMenuSystem.Unregister(this.menu);
	}
	
	this.Update = function()
	{
	}
	
	this.DrawClear = function()
	{
	}
	
	this.Draw = function()
	{
	}
	
	this.Debug = function()
	{
	}
}

