
// Copyright (c) 2004
package uk.co.patrickhaston.mars;

import java.awt.Point;

/**
 * A Class class.
 * <P>
 * @author Patrick Haston
 */
public class MarsConstruction extends MarsLocatedObject
{
  private int constructionType;
  // Type type of construction
  public int status;
  public static int NOT_CREATED = 0; // 0 = not created
  public static int NOT_IN_USE = 1; // 1 = not in use
  public static int IN_USE = 2; // 2 = in active use
  public static int BROKEN = 3; // 3 = broken
  public static int UNDER_CONSTRUCTION = 4;
  
  private int storedResources;
  // the amount of resources stored in this construction
  private int usedInActivity;
  // the activity that this construction is allocated to
  private LatLong location;
  // the location of this construction
  private int settlement;
  // the settlement that owns this construction
  private int efficiency;
  // 0 - 100 = % operating efficiency (inverse chance of breaking)
  private int workers;
  // number of workers currently operating this
  private int owner;
  // the owner if privately owned
  private String description;
  // a description of this construction
  private int waypoint;
  // set to 0 if not at a waypoint, otherwise the waypoint id
  private int localX;
  // the location relative to the waypoint centrepoint (if at a waypoint)
  private int localY;
  // the location relative to the waypoint centrepoint (if at a waypoint)
  private int orientation;
  // the angle at which this construction is fixed (0 = north, 1=east, 2=south, 3=west)
  private int[] linkedTo = new int[4]; // (0 = north, 1=east, 2=south, 3=west)
  // the constructions to which this building is linked

  /**
   * Constructor
   */
  public MarsConstruction(int id)
  {
    super(id);
    constructionType = 0;
    status = 0;
    storedResources = 0;
    usedInActivity = 0;
    LatLong location = new LatLong();
    settlement = 0;
    efficiency = 0;
    workers = 0;
    description = "";
    waypoint = 0;
    localX = 0;
    localY = 0;
    orientation = 0;
    for (int i=0; i<4; i++)
    {
      linkedTo[i] = 0;
    }
  }

  /**
   * Copy Constructor
   */
  public MarsConstruction(MarsConstruction c)
  {
    super(c);
    if ( c==null )
    {
      constructionType = 0;
      status = 0;
      storedResources = 0;
      usedInActivity = 0;
      LatLong location = new LatLong();
      settlement = 0;
      efficiency = 0;
      workers = 0;
      description = "";
      waypoint = 0;
      localX = 0;
      localY = 0;
      orientation = 0;
      for (int i=0; i<4; i++)
      {
        linkedTo[i] = 0;
      }
    }
    else
    {
      constructionType = c.constructionType;
      status = c.status;
      storedResources = c.storedResources;
      usedInActivity = c.usedInActivity;
      LatLong location = new LatLong(c.location);
      settlement = c.settlement;
      efficiency = c.efficiency;
      workers = c.workers;
      description = c.description;
      waypoint = c.waypoint;
      localX = c.localX;
      localY = c.localY;
      orientation = c.orientation;
      for (int i=0; i<4; i++)
      {
        linkedTo[i] = c.linkedTo[i];
      }
    }
  }
  
  /**
   * Construct from data string
   */
  public MarsConstruction(String line)
  {
    super(line); // reads name, lat, long values

    constructionType = readInteger(line, 3);
    status = readInteger(line, 4);
    storedResources = readInteger(line, 5);
    usedInActivity = readInteger(line, 6);
    settlement = readInteger(line, 7);
    efficiency = readInteger(line, 8);
    workers = readInteger(line, 9);
    owner = readInteger(line, 10);
    description = readString(line, 11);
    waypoint = readInteger(line,12);
    localX = readInteger(line,13);
    localY = readInteger(line,14);
    orientation = readInteger(line, 15);
    for (int i=0; i<4; i++)
    {
      linkedTo[i] = readInteger(line, 16+i);
    }
    
  }
  
  
  /**
   * 
   * @param id = the id of this construction
   * @param cType = the type of this construction
   * @param loc = the Lat Long location of this construction
   * @param settle = the settlement to which this construction belongs
   * @param own = the owner of this construction
   * @param model = a pointer to the mars model
   */
  MarsConstruction( int id, int cType, // construction type
    LatLong loc, // Location
    int settle, // settlement
    int own,
    MarsModel model) // owner
  {
    // create a new construction with limited information  
    super(id);
    constructionType = cType;
    status = NOT_IN_USE;
    storedResources = 0;
    usedInActivity = 0;
    location = new LatLong(loc);
    settlement = settle;
    efficiency = 100;
    workers = 0;
    owner = own;
    description = ((MarsConstructionType) 
      model.ConstructionTypes.elementAt(cType)).getName()
      + " owned by ";
    if(settle > 0) description = description + ((MarsSettlement)
      model.Settlements.elementAt(settle)).getName();
    else description = description + ((MarsPerson)
      model.People.elementAt(own)).getName();
    waypoint = model.Settlements.getWaypoint(settlement);
/*    int result[] = model.Settlements.getLocationForNewConstruction(settlement, constructionType);
    localX = result[0];
    localY = result[1];
    orientation = result[2];
*/
  }
  
  public String toString()
  {
    String s = super.toString();
    
    s = s + new Integer(constructionType).toString() + ",";
    s = s + new Integer(status).toString() + ",";
    s = s + new Integer(storedResources).toString() + ",";
    s = s + new Integer(usedInActivity).toString() + ",";
    s = s + new Integer(settlement).toString() + ",";
    s = s + new Integer(efficiency).toString() + ",";
    s = s + new Long(workers).toString() + ",";
    s = s + description;
    s = s + " Waypoint:" + waypoint;
    s = s + " Local X,Y:" + localX + "," + localY;
    s = s + " Orientation: " + MarsView.getOrientationName(orientation);
    s = s + " to north: " + linkedTo[0];
    s = s + " to east: " + linkedTo[1];
    s = s + " to south: " + linkedTo[2];
    s = s + " to west: " + linkedTo[3];
    
    return s;
  }

  public String toFile()
  {
    String s = super.toFile();
    
    s = s + constructionType + ",";
    s = s + status + ",";
    s = s + storedResources + ",";
    s = s + usedInActivity + ",";
    s = s + settlement + ",";
    s = s + efficiency + ",";
    s = s + workers + ",";
    s = s + description + ",";
    s = s + waypoint + ",";
    s = s + localX + ",";
    s = s + localY + ",";
    s = s + orientation;
    for (int i=0; i<4; i++)
    {
      s = s + "," + linkedTo[i];
    }

    return s;
  }

  public void update(MarsModel model)
  {
    if(efficiency > 0 && status != UNDER_CONSTRUCTION)
    {
      efficiency--;
      if(efficiency < 10 && status != BROKEN)
      {
        status = BROKEN;
        model.systemMessage("Construction " + this.getName() 
           + " with id: "+ this.getId() + " is broken.");
      }
    }
  }

  public void activityStopped(int activityId)
  {
    if (usedInActivity == activityId) usedInActivity = 0;
/*
     for (int i=0; i<7; i++)
    {
      if (activity[i] == activityId) activity[i] = 0;
    }
*/
  }
  
  public int getWaypoint()
  {
    return waypoint;
  }
  public int getOrientation()
  {
    return orientation;
  }
  public void setOrientation(int direction)
  {
    orientation = direction;
  }
  public int getStatus()
  {
    return status;
  }
  public void setStatus(int s)
  {
    status = s;
  }
  public int getLocalX()
  {
    return localX;
  }
  public int getLocalY()
  {
    return localY;
  }
  public void setTile(Point tile)
  {
    localX = tile.x;
    localY = tile.y;
  }
  public boolean isAt(int x, int y)
  {
    return ((localX == x) && (localY == y));
  }
  public int getType()
  {
    return constructionType;
  }
  
  public int getLinkedNorth()
  {
    return linkedTo[0];
  }
  public int getLinkedEast()
  {
    return linkedTo[1];
  }
  public int getLinkedSouth()
  {
    return linkedTo[2];
  }
  public int getLinkedWest()
  {
    return linkedTo[3];
  }
  public int getLinked(int direction)
  {
    return linkedTo[direction];
  }
  public void linkTo(int building, int direction)
  {
    linkedTo[direction] = building;
  }
  
  public void resetWaypoint(MarsModel model)
  {
    if(waypoint < 0)
    {
      // the waypoint is stored as a settlement id
      waypoint = model.Settlements.getWaypoint(-waypoint);
      // convert it to a waypoint...
    }
  }
  
  public String getStatusDescription()
  {
    if (status == NOT_CREATED) return "not created";
    if (status == NOT_IN_USE) return "not in use";
    if (status == IN_USE) return "in use";
    if (status == BROKEN) return "broken";
    if (status == UNDER_CONSTRUCTION) return "under construction";
    return "Not Known";
  }
  
  public String getMaintenance()
  {
    if (status == UNDER_CONSTRUCTION) return "n/a";
    return ("" + efficiency + "%" ); 
  }
}


