//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 gPatternIndex = 0;

function PrevPattern()
{
	gPatternIndex--;
	if(gPatternIndex == -1)
	{
		gPatternIndex = gBlockRenderer.overlays.length - 1;
	}
}

function NextPattern()
{
	gPatternIndex++;
	if(gPatternIndex == gBlockRenderer.overlays.length)
	{
		gPatternIndex = 0;
	}
}

function PatternCenterButton(name, callback)
{
	this.__proto__ = new Button(name, callback);
	
	this.blockYPos = 4;
	gBlockSystem.menuBlocks = new Array();
	
	gBlockSystem.menuBlocks.push(gBlockSystem.CreateBlock(EBlockType.Normal, EBlockSubType.Normal, 0, 2, 6, this.blockYPos, false, false, false));
	gBlockSystem.menuBlocks.push(gBlockSystem.CreateBlock(EBlockType.Normal, EBlockSubType.Normal, 0, 3, 7, this.blockYPos, false, false, false));
	gBlockSystem.menuBlocks.push(gBlockSystem.CreateBlock(EBlockType.Normal, EBlockSubType.Normal, 0, 5, 8, this.blockYPos, false, false, false));
	gBlockSystem.menuBlocks.push(gBlockSystem.CreateBlock(EBlockType.Normal, EBlockSubType.Normal, 0, 8, 9, this.blockYPos, false, false, false));
	
	this.DrawText = function()
	{
		var additional = this.selected ? 1 : 0;
		gDraw.menu.font	 = GetFont(this.fontSize);
		gDraw.menu.textAlign	 = 'center';
		gDraw.menu.textBaseline	 = 'top';
		var padding = 5;
		var top = this.y - (this.height/2) + padding + additional;
		
		var overlayName = gBlockRenderer.GetOverlayName(gPatternIndex);

		var unlockInfo = gUnlockSystem.Get(overlayName)
		if(!unlockInfo || unlockInfo.IsUnlocked())
		{
			gBlockRenderer.SetOverlay(gPatternIndex);
			gBlockSystem.RebuildRenderCommands();
			
			var text = Localized(overlayName);
			gDraw.menu.fillTextShadow(text, this.x, top, this.GetTextColor(!this.locked), this.GetTextColor(this.locked));
			var previousContext = gBlockRenderer.context;
			gBlockRenderer.context = gDraw.menu;
			
			gBlockRenderer.context.save();
			gBlockRenderer.context.translate(gDraw.horizontalCenterOffset, gDraw.verticalCenterOffset);
			var length = gBlockSystem.menuBlocks.length;
			for(var i = 0; i < length; i++)
			{
				var block = gBlockSystem.menuBlocks[i];
				block.redraw = true;
				block.y = this.selected ? this.blockYPos + 0.02 : this.blockYPos;
				gDraw.menu.lineWidth = 1;
				gDraw.menu.shadowOffsetX = 0;
				gDraw.menu.shadowOffsetY = 0;
				gDraw.menu.shadowBlur = 0;
	
				gBlockRenderer.Draw(block);
				
				gDraw.menu.font	 = GetFont(32);
				gDraw.menu.textAlign	 = 'center';
				gDraw.menu.textBaseline	 = 'middle';
		
				gDraw.menu.shadowOffsetX = 1;
				gDraw.menu.shadowOffsetY = 1;
				gDraw.menu.shadowBlur = 2;
			
				gBlockRenderer.DrawOverlay(block);
			}
			
			gBlockRenderer.context.restore();
			gBlockRenderer.context = previousContext;
		}
		else
		{
			text = "Locked";
			gDraw.menu.fillTextShadow(text, this.x, top, this.GetTextColor(!this.locked), this.GetTextColor(this.locked));
			
			gDraw.menu.font	 = GetFont(this.fontSize * 0.7);
			var unlockText = unlockInfo.GetRequirementAsText();
			gDraw.menu.fillTextShadow(unlockText, this.x, top + this.height/2, this.GetTextColor(!this.locked), this.GetTextColor(this.locked));
		}
	}
}

function PatternButton()
{
	gPatternIndex = gBlockRenderer.overlays.indexOf(gBlockRenderer.overlay);
	
	this.__proto__ = new TripleButton("Blocks",
		new Button("<", function(){ PrevPattern(); gOptionsMenu.Redraw();  }),
		new PatternCenterButton("Blocks", function(){ gBlockRenderer.NextOverlay(); gOptionsMenu.Redraw(); }),
		new Button(">", function(){ NextPattern(); gOptionsMenu.Redraw(); })
	);
}
