
// Copyright (c) 2004
package uk.co.patrickhaston.mars;

import uk.co.patrickhaston.pdhdata.PdhError;
import java.awt.Point;

/**
 * A Class class.
 * <P>
 * @author Patrick Haston
 */
public class MarsSettlements extends MarsTypes
{

  /**
   * Constructor
   */
  public MarsSettlements(MarsModel model)
  {
    super(model);
    CollectionName = "[Settlements]";
  }

  public void readData(String line)
  {
    int idx = readIndex(line);
    if (idx>0)
    {
      MarsSettlement s = new MarsSettlement(line);
/*
      s.name = new String(readString(line, 0));
      double latitude = readDouble(line, 1);
      double longitude = readDouble(line, 2);
      s.location = new LatLong(latitude, longitude);
      s.population = readLong(line, 3);
      s.storedResources = readInteger(line, 4);
*/
      this.setElementAt(s, idx);
    }
  }
  
  public void setSettlementLocation(int s, int wp, MarsWaypoint w)
  {
    if (s > this.size()) return;
    ((MarsSettlement) elementAt(s)).setWaypoint(wp, w);
  }
  
  public void setReferences(MarsModel model)
  {
    MarsSettlement s = null;
    MarsWaypoint w = null;
    for (int i=1; i<this.size(); i++)
    {
      s = (MarsSettlement) this.elementAt(i);
      int wp = s.getWaypoint();
      w = (MarsWaypoint) model.Waypoints.elementAt(wp);
      s.setWaypoint(wp, w);
    }
  }
  
  public int getStoredResources(int s)
  {
    if (s > this.size()) return 0;
    return ((MarsSettlement) elementAt(s)).getStoredResources();
  }
  
  public LatLong getLocation(int s)
  {
    return theModel.Waypoints.getLocation(getWaypoint(s));
  }
  
  public int getWaypoint(int s)
  {
    if (s < 1) return 0;
    if (s > this.size()) return 0;
    return ((MarsSettlement) elementAt(s)).getWaypoint();
  }
  
  public int getSettlementAt(int waypointId)
  {
    if(this.size() < 2) return 0;
    int s = 0;
    for(int i=1; i<size(); i++)
    {
      if (waypointId == getWaypoint(i)) s = i;
    }
    return s;
  }

  /**
   * @param s - the settlement id
   * @param activityId - the activity which will build this construction
   * @param constructionType - the type of construction
   * @return returns an x,y coordinate within the settlement
   */
  public int[] getLocationForNewConstruction(int s, int activityId, int constructionType)
  {
    int retval[] = new int[3];
    retval[0] = 0;
    retval[1] = 0;
    retval[2] = 0;
    if (s < 1) return retval;
    if (s > this.size()) return retval;
    // the code for working out the location goes here
    return retval;
  }
  
  /**
   * 
   * @param settlementId = the settlement where the construction will be located
   * @param activityId = the activity which will be building the new construction
   * @param constructionId = the construction to be built
   * @return true = success
   */
  public boolean setTileForNewBuilding(int settlementId, int activityId, int constructionId)
  {
    if (settlementId < 1) return false;
    if (settlementId > this.size()) return false;
    return ((MarsSettlement) elementAt(settlementId)).setTileForNewBuilding(theModel, activityId, constructionId);
  }
  
  public int getConstructionAt(int settlementId, Point tile)
  {
    if (settlementId < 1) return PdhError.OUT_OF_RANGE;
    if (settlementId > this.size()) return PdhError.OUT_OF_RANGE;
    return ((MarsSettlement) elementAt(settlementId)).getConstructionAt(tile);
  }
}


