/*
 * A class used by the military manager that returns an intention of building advanced unit
 *
 */

var UnitSupport = function(HeadQuarters, priority, amount,queue) {
	this.HeadQuarters = HeadQuarters;
	this.priority = priority;	// note : this is the individual priority compared to the priority of the queue
	this.targetAmount = amount;
	if (this.targetAmount == -1)
		this.targetAmount = Math.min();	// infinity
	
	this.amount = 0;
	this.virtualTrainingQueueAmount = 0;
	this.queue = queue;
	this.units = [];
	
	this.countReferer = undefined;
};
UnitSupport.prototype.setCountReferer = function (referer)
{
	this.countReferer = referer;
};
// this sets a specific unit to maintain
UnitSupport.prototype.setUnit = function(unit) {
	// TODO: should check the unit actually exists.
	this.units = [unit];
};
UnitSupport.prototype.addUnit = function(unit) {
	// TODO: should check the unit actually exists.
	this.units = this.units.concat([unit]);
};
UnitSupport.prototype.setTargetAmount = function(target) {
	this.targetAmount = target;
};
// category is "civilian, super or hero"
// it returns false if there is no unit.
UnitSupport.prototype.setByCategory = function(category,infantry,melee,type,gameState) {
	// returns templates of units
	var totalUnits = this.HeadQuarters.getPossibleUnitsTemplate(gameState);
	for (i in totalUnits)
	{
		// TODO: change this once Alpha 9 is out.
		var unit = totalUnits[i][1];
		var name = totalUnits[i][0];
		if (unit.hasClass(capitaliseFirstLetter(category)) && infantry != "Siege") {
			if (unit.hasClass(capitaliseFirstLetter(infantry)))
				if (unit.hasClass(capitaliseFirstLetter(melee)))
				{
					if (capitaliseFirstLetter(type) == "Javelin") {
						if (isJavelin(name)) {
							this.units.push(name);
						}
					} else if (capitaliseFirstLetter(type) == "Archer") {
						if (isArcher(name)) {
							this.units.push(name);
						}
					} else if (capitaliseFirstLetter(type) == "Slinger") {
						if (isSlinger(name)) {
							this.units.push(name)
						}
					} else if (capitaliseFirstLetter(type) == "Spearman") {
						if (isSpearman(name)) {
							this.units.push(name);
						}
					} else if (capitaliseFirstLetter(type) == "Swordsman") {
						if (isSwordsman(name)) {
							this.units.push(name);
						}
					} else if (capitaliseFirstLetter(type) == "Pikeman") {
						if (isPikeman(name)) {
							this.units.push(name);
						}
					} else if (capitaliseFirstLetter(type) == "Unit") {	// unit means we accept anything
						this.units.push(name);
					} else if (type == "noSling") {
						if (!isSlinger(name)) {
							this.units.push(name)
						}
					} else if (type == "noPike") {
						if (!isPikeman(name)) {
							this.units.push(name);
						}
					} else if (type == "noSling_noPike")
					{
						if (!isPikeman(name) && !isSlinger(name)) {
							this.units.push(name);
						}
					}
				}
		} else if (unit.hasClass(category) && unit.hasClass(infantry))
		{
			this.units.push(name);			
		}
		// OKay so now we've got all the units that qualify to maintain "amount". I say we count them
	}
	if (this.units.length == 0)
		return false;
	
	return true;
};
UnitSupport.prototype.getCountReferer = function () {
	return this.countReferer;
};
UnitSupport.prototype.getTrainingQueueAmount = function () {
	return this.virtualTrainingQueueAmount;
};
UnitSupport.prototype.TrainingQueueIncrement = function(amount) {
	this.virtualTrainingQueueAmount += amount;
};
UnitSupport.prototype.TrainingQueueDecrement = function(amount) {
	this.virtualTrainingQueueAmount -= amount;
};
UnitSupport.prototype.checkCountPlusQueued = function(gameState, name) {
	return (this.HeadQuarters.CountTotalUnitsWithRole(gameState,this.units, "PreparationPlan_" + name, this.queue) +
			this.HeadQuarters.CountTotalUnitsWithRole(gameState,this.units, "ReadyForPlan_" + name, this.queue)) >= this.targetAmount;
};
UnitSupport.prototype.getUnits = function() {
	return this.units;
};
UnitSupport.prototype.getQueue = function() {
	return this.queue;
};
UnitSupport.prototype.getPriority = function() {
	return this.priority;
};
UnitSupport.prototype.setPriority = function(prio) {
	this.priority = Math.min(1,prio);
};
