/* CVS: $Id: Constructing.java,v 1.11 2001/03/18 19:16:33 gvijf Exp $ */

package evolution.actions;

import java.util.*;

import evolution.*;
import evolution.lands.*;
import evolution.resources.*;
import evolution.constructions.*;

/**
 * Construction of a building.
 */
public class Constructing extends Action {

    /**
     * Constructor for the constructing-action.
     */
    public Constructing() {
        setConstruction(null);
    }

    /**
     * Checks if this construction-action can be performed by the human on the selected square of land.
     * If not, some exception will be thrown.
     */
    protected void doChecks(Human human) throws NotEnoughResourcesException, IllegalLandTypeException, NotEnoughLandResourcesException {
        setConstruction(human.getSquareOfLand().getConstruction());
        if(getConstruction() == null)
            throw new IllegalLandTypeException("I can't construct something out of thin air!");
        if(getConstruction().isProduction())
            throw new IllegalLandTypeException("The construction is finished, god damn...");
        super.doChecks(human);
    }

    /**
     * Extra things which should be done when this action's perform
     * method is called.
     * Template method.
     */
    protected void _perform(Human human) throws NotEnoughResourcesException, IllegalLandTypeException, NotEnoughLandResourcesException {
        getConstruction().construct(ConstructionKnowledgeCatalog.getInst().
            getConstructionSpeed(getConstruction().getName()));
    }

    /**
     * Returns a map of the names of resources that are produced
     * by this constructing-action.
     */
    protected Map getProducesResources() {
        if(getConstruction() == null) return new HashMap();
        return ConstructionKnowledgeCatalog.getInst().producesResourcesConstructing(getConstruction().getName());
    }

    /**
     * Returns a map of the names of resources that are used
     * by this constructing-action.
     */
    protected Map getUsesResources() {
        if(getConstruction() == null) return new HashMap();
        return ConstructionKnowledgeCatalog.getInst().usesResourcesConstructing(getConstruction().getName());
    }

    /**
     * Returns a map of the names of landresources that are produced
     * by this constructing-action.
     */
    protected Map getProducesLandResources() {
        if(getConstruction() == null) return new HashMap();
        return ConstructionKnowledgeCatalog.getInst().producesLandResourcesConstructing(getConstruction().getName());
    }

    /**
     * Returns a map of the names of landresources that are used
     * by this constructing-action.
     */     
    protected Map getUsesLandResources() {
        if(getConstruction() == null) return new HashMap();
        return ConstructionKnowledgeCatalog.getInst().usesLandResourcesConstructing(getConstruction().getName());
    }

    /**
     * Checks whether this constructing-action can be performed on the given square of land.
     */
    protected boolean canBePerformedOn(SquareOfLand square) {
        if((square.getConstruction() != null) && square.getConstruction().isBeingBuild()) return true;
        return false;
    }

    /**
     * Set the construction of this constructing-action.
     */
    protected void setConstruction(Construction construction) {
        this.construction = construction;
    }

    /**
     * Return the construction of this constructing-action.
     */
    protected Construction getConstruction() {
        return construction;
    }

    /**
     * The construction of this constructing-action.
     * @supplierCardinality 1 
     */
    private Construction construction;
}