//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 gRulesSystem = new RulesSystem();

function RulesSystem()
{
	this.stars = new Array();
	this.kStarCount = 3;
	
	this.complete = undefined;
	this.completeStars = 0;
	this.better = false;
	
	this.completeSounds = new Array();
	for(var i = 1; i <= this.kStarCount; i++)
	{
		this.completeSounds[i] = gSounds.Get("complete" + i);
	}
	
	this.LoadStars = function(starArray)
	{
		this.Clear();
		
		var count = starArray.length;
		for(var i = 0; i < count; i++)
		{
			var starNode = starArray[i];
			var type = starNode.getAttribute("type")
			var index = parseInt(type)
			var star = new Rule(index);
			star.InitXml(starArray[i]);
			this.stars[index] = star;
		}
		
		if(gEditor)
		{
			//TODO
			//EditorLoadStarsIntoHTML();
		}
	}
		
	this.WriteStars = function()
	{
		var xml = '\t<Stars>\r';
		for(var i = 1; i <= this.kStarCount; i++)
		{
			var star = this.stars[i];
			if(star && star.valid)
			{
			 xml += '\t\t<Star type="' + i + '" stat="' +  star.statType + '" condition="' +  star.condition + '" target="' + star.target + '" />\r';	
			}
		}
		xml += '\t</Stars>\r';
		
		return xml;
	}
	

	this.HasComplete = function()
	{
		return this.stars[1] && this.stars[1].valid;
	}
	
	this.GetStars = function()
	{
		for(var i = 1; i <= this.kStarCount; i++)
		{
			if(!this.stars[i] || !this.stars[i].valid)
			{
				return i;
			}
		}
		return this.kStarCount;
	}
	
	this.GetCompleteStars = function()
	{
		return this.completeStars;
	}

	this.ScriptLoaded = function()
	{
		this.complete = false;
		if(gLevelScript)
		{
			if(gLevelScript.Load)
			{
				gLevelScript.Load();
			}
		}
	}
	
	this.Clear = function()
	{
		this.complete = undefined;
		this.completeStars = 0;
		this.stars = new Array();
		this.better = false;
	}
	
	this.Update = function()
	{
		var star = this.stars[1];
		if(star && star.valid)
		{
			if(star.IsComplete())
			{
				this.Complete();
			}
		}
	}
	
	this.Debug = function()
	{
		for(var i = 1; i <= this.kStarCount; i++)
		{
			var star = this.stars[i];
			if(star && star.valid)
			{
				star.Debug();
			}
		}
	}
	
	this.IsBetter = function()
	{
		return this.better;
	}
	
	this.IsComplete = function()
	{
		return this.complete;
	}

	this.Complete = function()
	{
		if(!this.complete)
		{
			var stars = 1;
			for(var i = 2; i <= this.kStarCount; i++)
			{
				var star = this.stars[i];
				if(star && star.valid)
				{
					if(star.IsComplete())
					{
						stars = i;
					}
				}
			}
			
			this.MarkStarsComplete(stars);
			
			if(stars == this.kStarCount)
			{
				if(this.stars[this.kStarCount].IsBetter())
				{
					this.MarkBetterComplete();
				}
			}
			
			//MarkComplete (and MarkStarsComplete) needs to be before gCompleteOverlay.Enable();
			this.MarkComplete();
			
			if(!gEditor)
			{
				gCompleteOverlay.Enable();
			}
			
			this.completeSounds[stars].Play();
			
			//Clear all blocks
			gBlockSystem.LevelComplete();
			gSpawnerManager.Clear();
		}
	}
	
	
	this.MarkStarsComplete = function(stars)
	{
		this.completeStars = stars;

		var levelSet = gLevelSystem.GetLevelSet();
		var levelName = gLevelSystem.GetLevelName();
		
		gTelemetry.PostStat("stars" + stars);
		var previousStars = gStorage.getItem(levelSet, levelName, "stars");
		if(previousStars)
		{
			previousStars = parseInt(previousStars);
			if(stars > previousStars)
			{
				gStorage.setItem(levelSet, levelName, "stars", stars);
			}
		}
		else
		{
			gStorage.setItem(levelSet, levelName, "stars", stars);
		}
		
		gParticleSystem.Spawn("CompletedLevel", {completeStars:this.completeStars});
	}
	
	this.MarkBetterComplete = function()
	{
		this.better = true;
		
		var levelSet = gLevelSystem.GetLevelSet();
		var levelName = gLevelSystem.GetLevelName();
		
		gTelemetry.PostStat("better");
		gTelemetry.PostGlobalStat("better", 1);
		gStorage.setItem(levelSet, levelName, "better", true);
		
		//TODO better particle effect
		//gParticleSystem.Spawn("CompletedLevel", {completeStars:this.completeStars});
	}
	
	this.MarkComplete = function()
	{
		this.complete = true;
		gTelemetry.PostStat("completed");
		
		if(gLevelSystem.IsArcade())
		{
			gHighscoreSystem.Save();
		}
	}
	
	this.GetStarDescription = function(star)
	{
		var description = "";
		var completable = true;
		
		var starRule = gRulesSystem.stars[star];
		if (starRule && starRule.valid)
		{
			description = starRule.GetDescription();
			completable = starRule.GetCompletable();
		}
		
		if(description != "")
		{
			if(gRulesSystem.IsComplete())
			{
				completable = gRulesSystem.completeStars >= star;
			}
			
			return description;
		}
		
		return null;
	}
	
	this.GetStarCompletable = function(star)
	{
		var completable = false;
		
		var starRule = gRulesSystem.stars[star];
		if (starRule && starRule.valid)
		{
			completable = starRule.GetCompletable();
		}
		
		if(gRulesSystem.IsComplete())
		{
			completable = gRulesSystem.completeStars >= star;
		}
		
		return completable;
	}
}
