
// Copyright (c) 2004
package uk.co.patrickhaston.mars;


import java.awt.Point;

/**
 * A Class class.
 * <P>
 * @author Patrick Haston
 */
public class MarsConstructions extends MarsArray
{

  /**
   * Constructor
   */
  public MarsConstructions(MarsModel model)
  {
    super(model);
    this.CollectionName = "[Constructions]";
  }

  public void readData(String line)
  {
    int idx = readIndex(line);
    if (idx>0)
    {
      MarsConstruction c = new MarsConstruction(line);

      this.setElementAt(c, idx);
    }
  }

  /*
  public void update()
  {
    if(this.elementCount > 1)
    {
      for (int i=1; i<this.elementCount; i++)
      {
        ((MarsConstruction) this.elementAt(i)).update(theModel);
      }
    }
  }
  */
  public int addConstruction( int cType, // construction type
    LatLong loc, // Location
    int settle, 
    int own)
  {
    MarsConstruction c = new MarsConstruction(size(), cType, loc, settle, own, theModel);
    
    this.addElement(c);
    return (this.size() - 1 );
  }
  
  public void activityStopped(int constructionId, int activityId)
  {
    if (constructionId < 1) return;
    if (constructionId >= size()) return;
    ((MarsConstruction) elementAt(constructionId)).activityStopped(activityId);
  }
  
  public int[] getConstructionsAt(int waypoint)
  {
    int retval[] = null;
    // check to make sure there are some constructions
    if (size() < 2) return null;
    // first count the constructions
    int count = 0;
    int i;
    for (i=1; i<size(); i++)
    {
      if(((MarsConstruction) elementAt(i)).getWaypoint() == waypoint) count++;
    }
    retval = new int[count];
    count = 0;
    for (i=1; i<size(); i++)
    {
      if(((MarsConstruction) elementAt(i)).getWaypoint() == waypoint) 
      {
        retval[count] = i;
        count++;
      }
    }
    return retval;
  }
  
  public boolean isAt(int c, int x, int y)
  {
    if (size() < 2) return false;
    if (c >= size()) return false;
    if (elementAt(c) == null) return false;
    return ((MarsConstruction) elementAt(c)).isAt(x, y);
  }
  
  public int getConstructionType(int c)
  {
    if (size() < 2) return 0;
    if (c >= size()) return 0;
    if (elementAt(c) == null) return 0;
    return ((MarsConstruction) elementAt(c)).getType();
  }
  
  public int getOrientation(int c)
  {
    if (size() < 2) return 0;
    if (c >= size()) return 0;
    if (elementAt(c) == null) return 0;
    return ((MarsConstruction) elementAt(c)).getOrientation();
  }
  
  public int getStatus(int c)
  {
    if (size() < 2) return 0;
    if (c >= size()) return 0;
    if (elementAt(c) == null) return 0;
    return ((MarsConstruction) elementAt(c)).getStatus();
  }
  
  public String getStatusDescription(int c)
  {
    if (size() < 2) return "";
    if (c >= size()) return "";
    if (elementAt(c) == null) return "";
    return ((MarsConstruction) elementAt(c)).getStatusDescription();
  }
  
  public String getMaintenance(int c)
  {
    if (size() < 2) return "";
    if (c >= size()) return "";
    if (elementAt(c) == null) return "";
    return ((MarsConstruction) elementAt(c)).getMaintenance();
  }
  
  public void setOrientation(int c, int direction)
  {
    if (size() < 2) return;
    if (c >= size()) return;
    if (elementAt(c) == null) return;
    ((MarsConstruction) elementAt(c)).setOrientation(direction);
  }
  
  public void setTile(int c, Point tile)
  {
    if (size() < 2) return;
    if (c >= size()) return;
    if (elementAt(c) == null) return;
    ((MarsConstruction) elementAt(c)).setTile(tile);
  }
  
  public void resetWaypoints()
  {
    if (size() < 2 ) return;
    for (int i=1; i<size(); i++)
    {
      ((MarsConstruction) elementAt(i)).resetWaypoint(theModel);
    }
  }
  
  public void update()
  {
    if (size() < 2 ) return;
    for (int i=1; i<size(); i++)
    {
      ((MarsConstruction) elementAt(i)).update(theModel);
    }
  }
}



