//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 GetLevelIfLoadedOrHub(targetSet, index)
{
	var nextLevel =  gLevelsBySet[targetSet][index];
	if(!gStorage.getItem(targetSet, nextLevel, "loaded"))
	{
		return Capatilize(nextLevel);
	}
	else
	{
		return "Hub";
	}
}

function GetHubSet()
{
	var levelName = gLevelSystem.GetLevelName().toLowerCase();
	var set = gLevelSystem.GetLevelSet();
	if(levelName != "hub")
	{
		if(!gStorage.getItem(set, "hub", "loaded"))
		{
			var hubSet = "green"
			var previousHubSet = gStorage.getItem("levelSystem", "previousHubSet");
			if(previousHubSet)
			{
				hubSet = previousHubSet;
			}
			
			gStorage.setItem(set, "hub", "loaded", true);
			
			return hubSet;
		}
	}
	else
	{
		var parentHubSet = GetParentHub(set);
		if(parentHubSet)
		{
			return parentHubSet;
		}
	}
	return set;
}

function GetNextLevelAndSet(targetSet, currentLevelName)
{
	if(gDemo)
	{
		var demoLevels = [{set: "Vanilla", level:"Blocks"},
		{set: "Vanilla", level:"Multi"},
		{set: "Vanilla2", level:"Warm"},
		{set: "Switch", level:"Backwards"},
		{set: "Switch", level:"Swap"},
		{set: "Polyomino", level:"Slot"},
		{set: "Polyswitch", level:"Lockdown"},
		{set: "Menu", level:"Stats"}]
		for(var i = 0; i < demoLevels.length; i++)
		{
			if(demoLevels[i].level.toLowerCase() == currentLevelName.toLowerCase())
			{
				return demoLevels[i + 1];
			}
		}

		return {set: "Menu", level:"Demo"};
	}
	else
	{
		var complete = gStorage.getItem("complete", "shown");
		if(!complete && gStatsSystem.GetStat(EStatsType.Stars) == (gLevelsCount * gRulesSystem.kStarCount))
		{
			gStorage.setItem("complete", "shown", true);
			gTelemetry.PostGlobalStat("finished", 1)
			return {set: "Menu", level:"Stats"}
		}
		targetSet = targetSet.toLowerCase();
		var nextLevel = GetNextLevelOrHub(targetSet, currentLevelName);
		if(nextLevel.toLowerCase() == "hub" && currentLevelName.toLowerCase() != "hub")
		{
			if(!gStorage.getItem(targetSet, nextLevel, "loaded"))
			{
				var hubSet = "green"
				var previousHubSet = gStorage.getItem("levelSystem", "previousHubSet");
				if(previousHubSet)
				{
					hubSet = previousHubSet;
				}
				
				gStorage.setItem(targetSet, "hub", "loaded", true);
				
				return {set: hubSet, level:nextLevel};
			}
		}
		
		return {set: targetSet, level:nextLevel};
	}
}

function GetNextLevelOrHub(targetSet, currentLevelName)
{
	targetSet = targetSet.toLowerCase();
	currentLevelName = currentLevelName.toLowerCase();
	if(gLevelsBySet.hasOwnProperty(targetSet))
	{
		if(IsHubSet(targetSet))
		{
			return "Hub";
		}
		else if(currentLevelName != "hub")
		{
			//find next level name
			var setLength = gLevelsBySet[targetSet].length;
			for(var i = 0; i < setLength - 1; i++)
			{
				var levelName = gLevelsBySet[targetSet][i];
				if(levelName == currentLevelName)
				{
					return GetLevelIfLoadedOrHub(targetSet, i + 1);
				}
			}
		}
		else
		{
			return GetLevelIfLoadedOrHub(targetSet, 0);
		}
	}

	return "Hub";
}


function Exit()
{
	this.position = new Vec2();
	this.show = false;
	this.valid = false;

	this.InitXml = function(node)
	{
		this.hubDef = new HubDefinition();
		this.position.x = node.getAttribute("x");
		this.position.y = node.getAttribute("y");
		this.show = (node.getAttribute("show") == 1);
		
		if(this.show)
		{
			this.Activate();
		}
		
		this.valid = true;
	}
	
	this.Activate = function()
	{
		this.hubDef.AddLevel(this.GetSet(), this.GetLevel(), this.position.x, this.position.y);
	}

	this.GetSet = function()
	{
		return gLevelSystem.GetLevelSet();
	}
	
	this.GetLevel = function()
	{
		return GetNextLevelOrHub(gLevelSystem.GetLevelSet(), gLevelSystem.GetLevelName());
	}
	
	this.Update = function()
	{
		this.hubDef.Update();
	}
	
	this.Redraw = function()
	{
		this.hubDef.Redraw();
	}
	
	this.DrawClear = function()
	{
		this.hubDef.DrawClear();
	}
	
	this.Draw = function()
	{
		this.hubDef.Draw();
	}
	
	this.Clear = function()
	{
		this.valid = false;
	}

	this.Debug = function()
	{
		gDraw.Watch("Exit (" + this.position.x + ", " + this.position.y + ")  " + this.show);
	}
}
