//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


EConditions =
{
	Equals : "eq",
	LessThan : "lt",
	MoreThan : "mt",
	LessThanOrEqual : "lte",
	MoreThanOrEqual : "mte"
}

function Rule(index)
{
	this.statType;
	this.condition;
	this.target;
	this.index = index;
	
	this.valid = false;
	
	this.InitXml = function(node)
	{
		this.Init(node.getAttribute("stat"), node.getAttribute("condition"), node.getAttribute("target"));
	}
	
	this.Init = function(statType, condition, target)
	{
		if(statType == "none")
		{
			this.valid = false;
		}
		else
		{
			this.statType = statType;
			this.condition = condition;
			this.target = parseInt(target);
		
			this.valid = true;
		}
	}
	
	this.Clear = function()
	{
		this.valid = false;
	}

	this.IsComplete = function()
	{
		var stat = gStatsSystem.GetStat(this.statType);
		switch(this.condition)
		{
			case EConditions.Equals:
				{
					return (stat == this.target);
				}
				break;
			case EConditions.LessThan:
				{
					return (stat < this.target);
				}
				break;
			case EConditions.MoreThan:
				{
					return (stat > this.target);
				}
				break;
			case EConditions.LessThanOrEqual:
				{
					return (stat <= this.target);
				}
				break;
			case EConditions.MoreThanOrEqual:
				{
					return (stat >= this.target);
				}
				break;
			default:
				gConsole.AssertNow("should be unreachable");
				break;
		}
	}
	
	this.IsBetter = function()
	{
		var stat = gStatsSystem.GetStat(this.statType);
		switch(this.condition)
		{
			case EConditions.Equals:
				{
					return false;
				}
				break;
			case EConditions.LessThan:
				{
					return (stat < this.target-1);
				}
				break;
			case EConditions.MoreThan:
				{
					return (stat > this.target+1);
				}
				break;
			case EConditions.LessThanOrEqual:
				{
					return (stat <= this.target-1);
				}
				break;
			case EConditions.MoreThanOrEqual:
				{
					return (stat >= this.target+1);
				}
				break;
			default:
				gConsole.AssertNow("should be unreachable");
				break;
		}
	}
	
	this.GetCompletable = function()
	{
		var stat = gStatsSystem.GetStat(this.statType);
		switch(this.condition)
		{
			case EConditions.Equals:
				{
					if(this.statType == "undeletedBlocks")
					{
						return true;
					}
					else
					{
						return (stat <= this.target);
					}
				}
				break;
			case EConditions.LessThan:
				{
					return (stat < this.target);
				}
				break;
			case EConditions.MoreThan:
				{
					return true;
				}
				break;
			case EConditions.LessThanOrEqual:
				{
					return (stat <= this.target);
				}
				break;
			case EConditions.MoreThanOrEqual:
				{
					return true;
				}
				break;
			default:
				gConsole.AssertNow("should be unreachable");
				break;
		}
	}
	
	this.Debug = function()
	{
		gDraw.Watch("Rule (" + this.statType + " " + this.condition + " " + this.target + ") = " + this.IsComplete());
	}
	
	this.GetDescription = function()
	{
		switch(this.statType)
		{
			case EStatsType.Blocks:
			case EStatsType.UndeletedBlocks:
			return this.GetDescriptionBlocks();
			
			case EStatsType.Score:
			return this.GetDescriptionScore();
			
			case EStatsType.Pushes:
			return this.GetDescriptionPushes();
			
			case EStatsType.Time:
			case EStatsType.GameTime:
			return this.GetDescriptionTime();
			
			case EStatsType.DeletedBlocks:
			return this.GetDescriptionDeletedBlocks();
			
			case EStatsType.Chain:
			return this.GetDescriptionChain();
			
			case EStatsType.ArcadeTime:
			return this.GetDescriptionArcadeTime();
		}
		
		return "Rules.GetDescription needs to handle this...";
	}
	
	this.GetDescriptionBlocks = function()
	{
		switch(this.target)
		{
			case 0:
			return "Clear the Blocks";
			
			case 1:
			return "Clear the Block";
			
			default:
			return "Clear " + this.target + " Blocks";
		}
	}
	
	this.GetDescriptionScore = function()
	{
		var stat = gStatsSystem.GetStat(this.statType);
		var scoreLeft = (this.target - stat);
		if(scoreLeft >= 0)
		{
			if(scoreLeft != 1)
			{
				return "Score " + scoreLeft + " Points";
			}
			else
			{
				return "Score " + scoreLeft + " Point";
			}
		}
		else
		{
			return this.GetFailedString();
		}
	}
	
	this.GetDescriptionPushes = function()
	{
		var stat = gStatsSystem.GetStat(this.statType);
		var pushesLeft = (this.target - stat);
		if(pushesLeft >= 0)
		{
			if(this.target != 1)
			{
				return stat + " of " + this.target + " " + this.GetPluralPushes();
			}
			else
			{
				return stat + " of " + this.target + " " + this.GetSingularPushes();
			}
		}
		else
		{
			var pushesOver = stat - this.target;
			if(pushesOver != 1)
			{
				return pushesOver + " " + this.GetPluralPushes() + " Over";
			}
			else
			{
				return pushesOver + " " + this.GetSingularPushes() + " Over";
			}
		}
	}
	
	this.GetSingularPushes = function()
	{
		var levelSet = gLevelSystem.levelSet;
		switch(levelSet)
		{
			case "switch":
			case "switch2":
			case "polyswitch":
			case "polyswitch2":
				return "Switch";
			case "tricksy":
			case "tricksy2":
				return "Trick";
			default:
				return "Push";
		}
	}
	
	this.GetPluralPushes = function()
	{
		var levelSet = gLevelSystem.levelSet;
		switch(levelSet)
		{
			case "switch":
			case "switch2":
			case "polyswitch":
			case "polyswitch2":
				return "Switches";
			case "tricksy":
			case "tricksy2":
				return "Tricks";
			default:
				return "Pushes";
		}
	}
	
	this.GetDescriptionTime = function()
	{
		var stat = gStatsSystem.GetStat(this.statType);
		var timeLeft = this.target - stat;
		if(timeLeft >= 0)
		{
			if(timeLeft != 1)
			{
				return "In " + timeLeft + " seconds";
			}
			else
			{
				return "In " + timeLeft + " second";
			}
		}
		else
		{
			switch (this.condition)
			{
				case EConditions.MoreThan:
				case EConditions.MoreThanOrEqual:
					return "Complete";
				default:
					return this.GetFailedString();
			}
		}
	}
	
	this.GetDescriptionArcadeTime = function()
	{
		var stat = gStatsSystem.GetStat(this.statType);
		var timeLeft = this.target - stat;
		if(timeLeft >= 0)
		{
			if(timeLeft != 1)
			{
				return timeLeft + " seconds left";
			}
			else
			{
				return timeLeft + " second left";
			}
		}
		else
		{
			return this.GetFailedString();
		}
	}
	
	this.GetDescriptionDeletedBlocks = function()
	{
		var stat = gStatsSystem.GetStat(this.statType);
		var blocksLeft = (this.target - stat);
		if(blocksLeft >= 0)
		{
			if(blocksLeft != 1)
			{
				return "In " + blocksLeft + " Blocks";
			}
			else
			{
				return "In " + blocksLeft + " Block";
			}
		}
		else
		{
			return this.GetFailedString();
		}
	}
	
	this.GetDescriptionChain = function()
	{
		var stat = gStatsSystem.GetStat(this.statType);
		return stat + " of " + this.target + " Chain";
	}
	
	this.GetFailedString = function()
	{
		return this.isBonus ? "" : "Failed";
	}
	
}