//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 gUnlockSystem = new UnlockSystem();

function HubUnlock(hub, requirement)
{
	this.hub = hub;
	this.requirement = requirement;
	this.unlocked = false;
	
	gConsole.Assert(IsHubSet(this.hub));
	
	this.GetProgress = function()
	{
		return gStatsSystem.GetStarsForSet(this.hub);
	}

	this.GetRequirement = function()
	{
		return this.requirement;
	}
	
	this.IsUnlocked = function()
	{
		return this.GetProgress() >= this.GetRequirement();
	}
	
	this.GetRequirementAsText = function()
	{
		if(this.requirement == gStatsSystem.GetCountForSet(this.hub) * gRulesSystem.kStarCount)
		{
			return "Collect all stars in " + Localized(this.hub);
		}
		else
		{
			return "Collect " + this.requirement + " stars in " + Localized(this.hub);
		}
	}
}

function UnlockSystem()
{
	this.unlocks = new Array();
	this.justUnlocked = null;
	
	
	EUnlockType =
	{
		Set : "set",
		Area: "area",
		Arcade: "arcade",
		Blocks: "blocks",
	}
	
	function Unlock(type, unlockInfo)
	{
		this.type = type;
		this.unlockInfo = unlockInfo;
	}
	
	this.Init = function()
	{
		this.UpdateUnlocks();
		this.justUnlocked = null;
	}
	
	this.Add = function(name, type, unlockInfo)
	{
		this.unlocks[name] = new Unlock(type, unlockInfo);
	}
	
	this.Get = function(name)
	{
		if (this.unlocks[name])
		{
			return this.unlocks[name].unlockInfo;
		}
		
		return null;
	}
	
	//Vanilla
	this.Add("restraint",	EUnlockType.Set,	new HubUnlock("green", 4));
	this.Add("switch",		EUnlockType.Set,	new HubUnlock("green", 8));
	this.Add("vanilla2",	EUnlockType.Set,	new HubUnlock("green", 12));
	this.Add("constraint",	EUnlockType.Set,	new HubUnlock("green", 16));
	this.Add("fading",		EUnlockType.Set,	new HubUnlock("green", 20));

	//this.Add("arcade",		EUnlockType.Arcade,	new HubUnlock("green", 24));
	
	this.Add("blue",		EUnlockType.Area,	new HubUnlock("green", 30));
	
	this.Add("blockdesign_shapes",	EUnlockType.Blocks,	new HubUnlock("green", gStatsSystem.GetCountForSet("green") * gRulesSystem.kStarCount));

	//Static
	this.Add("opposites",	EUnlockType.Set,	new HubUnlock("blue", 4));
	this.Add("polyomino",	EUnlockType.Set,	new HubUnlock("blue", 8));
	this.Add("wildblocks",	EUnlockType.Set,	new HubUnlock("blue", 12));
	this.Add("switch2",		EUnlockType.Set,	new HubUnlock("blue", 16));
	this.Add("fading2",		EUnlockType.Set,	new HubUnlock("blue", 20));
	this.Add("polyomino2",	EUnlockType.Set,	new HubUnlock("blue", 24));
	
	this.Add("red",			EUnlockType.Area,	new HubUnlock("blue", 32));
	
	this.Add("blockdesign_dots",	EUnlockType.Blocks,	new HubUnlock("blue", gStatsSystem.GetCountForSet("blue") * gRulesSystem.kStarCount));

	//Polyswitch
	this.Add("tricksy",		EUnlockType.Set,	new HubUnlock("red", 4));
	this.Add("wildblocks2",	EUnlockType.Set,	new HubUnlock("red", 8));
	this.Add("vanilla3",	EUnlockType.Set,	new HubUnlock("red", 12));
	this.Add("opposites2",	EUnlockType.Set,	new HubUnlock("red", 16));
	this.Add("tricksy2",	EUnlockType.Set,	new HubUnlock("red", 20));
	this.Add("polyswitch2",	EUnlockType.Set,	new HubUnlock("red", 24));
	
	this.Add("blockdesign_aztec",	EUnlockType.Blocks,	new HubUnlock("red", gStatsSystem.GetCountForSet("red") * gRulesSystem.kStarCount));
	
	this.UpdateUnlocks = function()
	{
		for(unlockName in this.unlocks)
		{
			if (this.unlocks.hasOwnProperty(unlockName))
			{
				var unlockInfo = this.unlocks[unlockName].unlockInfo;
				unlockInfo.unlocked = unlockInfo.IsUnlocked();
			}
		}
	}
	
	this.HasJustUnlocked = function()
	{
		this.justUnlocked = null;
		for(unlockName in this.unlocks)
		{
			if (this.unlocks.hasOwnProperty(unlockName))
			{
				var unlockInfo = this.unlocks[unlockName].unlockInfo;
				if(!unlockInfo.unlocked && unlockInfo.IsUnlocked())
				{
					this.justUnlocked = new String(unlockName);
					unlockInfo.unlocked = true;
				}
			}
		}
		
		return this.justUnlocked != null;
	}
	
	this.GetJustUnlockedHint = function()
	{
		if (this.justUnlocked)
		{
			var unlockName = this.justUnlocked;
			switch(this.unlocks[unlockName].type)
			{
				case EUnlockType.Set:
					return "You've unlocked " + Localized(unlockName) + " set of puzzles!";
					break;
				case EUnlockType.Area:
					if(unlockName == "red")
					{
						return "You've unlocked the final area filled with new mechanics and lovingly crafted puzzles!";
					}
					else
					{
						return "You've unlocked the middle area filled with new mechanics and lovingly crafted puzzles!";
					}
					break;
				case EUnlockType.Arcade:
					return "You've unlocked Arcade Mode! Select it from the title screen."
					break;
				case EUnlockType.Blocks:
					return "You've unlocked " + Localized(unlockName) + " block design! Select it from the game options.";
					break;
			}
		}
		
		return "";
	}
}
