package simogl.resource;

import simogl.gameobjects.AbstractGameObject;
import simogl.gameobjects.building.AbstractBuilding;
import simogl.gameobjects.building.CityHall;
import simogl.gui.BalancePanel;
import simogl.gui.ResourcePanel;
import simogl.level.AbstractLevel;

/**
 * Represents the resource "gold"
 * @author Frank Bruns
 *
 */
public class Gold extends AbstractResource
{
	/**
	 * Creates a object of the resource gold with the specified
	 * initial amount
	 * @param amount initial amount of gold
	 * @param intervall intervall of time in miliseconds in which
	 * calculations on resources will be performed, effecting the
	 * amount of the player's resources
	 */
	public Gold(int amount, long intervall)
	{
		super(amount,intervall);
	}

	@Override
	public void update(long elapsedTime)
	{
		this.cumulatedTime += elapsedTime;
		
		// only calculate the new gold resources, if the defined time
		// intervall was exceeded
		if(this.cumulatedTime > this.getUpdateIntervall())
		{
			// reset cumulated time first
			this.cumulatedTime = 0L;	
			
			// calculate the total tax income and furthermore
			// calculate the regular costs for 
			// running certain buildings
			int totalIncome = 0;
			int totalCosts = 0;
			AbstractBuilding building = null;
			for(AbstractGameObject b : AbstractLevel.buildings)
			{
				 building = (AbstractBuilding)b;
				 
				 totalIncome += building.getTaxes();
				 totalCosts += building.getRegularCosts();				 				 				 			
			}
			
			// check that only possible taxes get collected
			if(totalIncome > CityHall.getMaxCollectableTaxes())
			{
				totalIncome = CityHall.getMaxCollectableTaxes();				 
			}
			 
			// add the difference of income and costs to the gold resources
			this.increase(totalIncome-totalCosts);
			
			// set the balance and resource panels to the new values
			BalancePanel.setLabelIncome(Integer.toString(totalIncome) + 
					" (max. "+CityHall.getMaxCollectableTaxes()+")");
			BalancePanel.setLabelCosts(Integer.toString(totalCosts));
			BalancePanel.setLabelBalance(Integer.toString(totalIncome - totalCosts));
			ResourcePanel.setLabelGold(Integer.toString(this.getCurrentAmount()));			
		}
	}
}
