/* CVS: $Id: CoalMine.java,v 1.15 2001/03/19 04:20:13 gvijf Exp $ */

package evolution.constructions;

import java.util.*;
import evolution.lands.SquareOfLand;
import evolution.GameBoard;
import evolution.World;
import java.lang.*;
import evolution.lands.*;


/**
 * Class of coal mines.
 */
public class CoalMine extends Construction {

    /**
     * Constructor of a Coalmine
     */
	 public CoalMine(List squares) {
         super(squares);
         Iterator it = squares.iterator();
         List result = new ArrayList();
         while (it.hasNext()) {
            SquareOfLand sl = (SquareOfLand) it.next();
            if ((!result.contains(sl)) && (sl.getLandResource("Coals").getValue()>0)) {
                result.add(sl);
            }
        	GameBoard gameboard = World.getInst().getGameBoard();
	        List list= getCoalContainingSquares(gameboard.getSurroundingSquares(sl, 1, false));
    	    while (!list.isEmpty()){
        		SquareOfLand sq= (SquareOfLand)list.get(0);
            	list.remove(sq);
	            if (!result.contains(sq)) {
    	            result.add(sq);
        	        list.addAll(getCoalContainingSquares(gameboard.getSurroundingSquares(sq, 1, false)));
            	}
	         }
	     }
         listOfSquares = result;
     }

    /**
     * Calculate the total land amount of land resources that are in the ground.
     */
    public double getTotalLandResource2(String name) {
        Iterator it = listOfSquares.iterator();
        double res = 0;
        while (it.hasNext()) {
            LandResource lr = ((LandResource) ((SquareOfLand) it.next()).getLandResource(name));
            res += lr.getValue();
        }
        return res;
    }

    /**
     * Checks whether this construction had enough landresources to be performed.
     */
    public boolean hasEnoughLandResources(double percentage) {
        Set usedLandResources = getUsesLandResources().keySet();
        Iterator it = usedLandResources.iterator();
        while (it.hasNext()) {
            String landResourceName = (String) it.next();
            if (((CoalMine) this).getCapacity() < percentage * ((Double)ConstructionKnowledgeCatalog.getInst().usesLandResources(getName()).get(landResourceName)).doubleValue()) {
                setWorking(false);
                return false;
            }
        }
        setWorking(true);
        return true;
    }

     /**
      * method that takes as input a list of squaresofland and
      * returns a list of those squaresofland containing coals
      */
     protected List getCoalContainingSquares(List list) {
        List result = new ArrayList();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            SquareOfLand sl = (SquareOfLand) it.next();
            //FIXME badly!!!
            //if(sl.getCoals()!= 0) {
                result.add(sl);
            //}
        }
        return result;
     }

    /**
     * Returns the capacity of this mine.
     */
     public double getCapacity() {
        double result = 0;
        Map producesResources = ConstructionKnowledgeCatalog.getInst().producesResources(getName());
        double coalsValue = ((Double)producesResources.get("Coals")).doubleValue();
        Iterator it = listOfSquares.iterator();
        while (it.hasNext()) {
            SquareOfLand sl = (SquareOfLand) it.next();
            result = result + sl.getLandResource("Coals").getValue();
        }
        return (Math.min(result, coalsValue));
     }

    /**
     *  Returns the list of squares containing coals associated with
     *  the coalmine.
     */
    public List getCoalContainingSquares() {
        return this.listOfSquares;
    }

    protected void modifyLandResources(List squares, Map resources, double mul) throws NotEnoughLandResourcesException {
        Iterator it1 = squares.iterator();
        double tot = getTotalLandResource2("Coals");
        System.err.println(getName() + ":: " + mul + " * " + resources + ", tot = " + tot);
        int i = 0;
        while (it1.hasNext()) {
            System.err.println("sq " + (++i));
            SquareOfLand square = (SquareOfLand) it1.next();
            double ratio = square.getLandResource("Coals").getValue()/tot;
            System.err.println("ratio " + ratio);
        	Iterator it = resources.keySet().iterator();
	        while(it.hasNext()) {
    	        String resName = (String) it.next();
        	    square.modResource(resName, mul * ratio * ((Double) resources.get(resName)).doubleValue());
	        }
        }
    }

    /**
     * The template evolve method.
     */
	protected void _evolve(double value){
        // FIXME: hier moeten de landresources van de squares aangepast worden
    }

    /**
     * A list of all the SquaresOfLand associated with the coalmine
     * that contains coals.
     */
    private List listOfSquares;

}
