//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 gStorage = new Storage();

function Storage()
{
	this.kProfileCount = 4;
	this.profileKeys = new Array("Polyology", "Polyology1", "Polyology2", "Polyology3")
	
	this.activeProfileIndex = 0;	//Override at end of class
	
	this.SetProfileActive = function(index)
	{
		this.activeProfileIndex = index;
		localStorage.setItem("ActiveProfile", index);
		gConsole.Log(3, "[STR] SetProfileActive " + index);
		gStatsSystem.Init();
		gUnlockSystem.Init();
	}
	
	this.IsProfileActive = function(index)
	{
		return this.activeProfileIndex == index;
	}
	
	this.IsProfileEmpty = function(index)
	{
		var profileKey = this.profileKeys[index]
		for (var key in localStorage)
		{
			var results = key.split(".");
			if(results[0] == profileKey)
			{
				return false;
			}
		}
		
		return true;
	}
	
	this.GetPreviewText = function(index)
	{
		var previewText = "";
		
		var previouslyActive = this.activeProfileIndex;
		this.activeProfileIndex = index;
		var empty = gStorage.IsProfileEmpty(index);
		if(empty)
		{
			previewText = "Empty";
		}
		else
		{
			var starCount = gStatsSystem.GetStarsAllLevels();
			var stars = starCount == 1 ? gCompleteOverlay.GetSingularStar() : gCompleteOverlay.GetPluralStar();
			previewText = starCount + " " + stars;
		}
		this.activeProfileIndex = previouslyActive;
		
		return previewText;
	}
	
	this.setItem = function()
	{
		localStorage.setItem(this.getKey(arguments, arguments.length - 1), arguments[arguments.length - 1]);
	}
	
	this.getItem = function()
	{
		return localStorage.getItem(this.getKey(arguments, arguments.length));
	}
	
	this.removeItem = function()
	{
		localStorage.removeItem(this.getKey(arguments, arguments.length));
	}
	
	this.getKey = function(parameters, length)
	{
		var key = this.profileKeys[this.activeProfileIndex]
		for (var i = 0; i < length; i++)
		{
			key += "." + parameters[i].toLowerCase();
		}
		
		return key;
	}
	
	this.clearProfile = function(index)
	{
		gConsole.Log(3, "[STR] ClearProfile " + index);
		
		var profileKey = this.profileKeys[index]
		for (var key in localStorage)
		{
			var results = key.split(".");
			if(results[0] == profileKey)
			{
				localStorage.removeItem(key);
			}
		}
		gStatsSystem.Init();
		gUnlockSystem.Init();
	}
	
	
	//Load last saved profile on storage creation
	var lastActiveProfile = localStorage.getItem("ActiveProfile");
	if(lastActiveProfile)
	{
		var index = parseInt(lastActiveProfile);
		gConsole.Log(3, "[STR] LoadProfileActive " + index);
		this.activeProfileIndex = index;
	}
}

