package uk.co.patrickhaston.mars;

public class MarsWaypoint extends MarsData 
{
  private LatLong location;
  // where it is!
  private int surveyedBy; // 0 = not surveyed, -ve = individual, +ve = settlement
  // this indicates that the area around the way point has been surveyed for resources
  private int resourcesId;
  // a pointer to the gameResources that are available at this waypoint for mining
  private int wayMarker; // 0 = no way marker, -ve = individual, +ve = settlement
  // a waymarker is a radio transmitter and is required for marking a trail
  private int links[] = null;
  // an array of links linking this waypoint to its nearest neighbours
  
  private String name = null;
  // by default, waypoints are named after the nearest feature
  // if a waypoint already has this name, then the second one to be
  // found is called B, then C and so on.  They are only named
  // when they are surveyed.
  
  public MarsWaypoint(int id)
  {
    super(id);
    location = null;
    surveyedBy = 0;
    resourcesId = 0;
    wayMarker = 0;
    name = "";
  }
  
  public MarsWaypoint(int id, LatLong loc)
  {
    super(id);
    location = new LatLong(loc);
    surveyedBy = 0;
    resourcesId = 0;
    wayMarker = 0;
    name = "";
  }
  
  public MarsWaypoint(int id, MarsModel model, double latitude, double longitude)
  {
    super(id);
    // This is used when generating a new waypoint
    location = new LatLong(latitude, longitude);
    surveyedBy = 0;
    resourcesId = model.gameResources.addNewMineralResource();
    wayMarker = 0;
    name = "";
  }
  
  public MarsWaypoint(String line)
  {
    super(line);
    double latitude = readDouble(line, 0);
    double longitude = readDouble(line, 1);
    location = new LatLong(latitude, longitude);
    surveyedBy = readInteger(line, 2);
    resourcesId = readInteger(line, 3);
    wayMarker = readInteger(line, 4);
    links = readIntegerArray(line, 5);
    name = readString(line, 6);
  }
  
  public LatLong getLocation()
  {
    return location;
  }
  
  public void setLocation(LatLong loc)
  {
    location = new LatLong(loc);
  }
  
  public String toString()
  {
    String s = new String(); //super.toString();
    if (name == "")
    {
      s = "Location";
    }
    else
    {
      s = name;
    }
    s = s + " (" + location.east + "," + location.north + ")";
    return s;
  }

  public String toFile()
  {
    String s = new String(); //super.toFile();
    s = location.east + "," + location.north + ",";
    if(links != null)
    {
      for (int i=0; i<links.length; i++)
      {
        s = s + links[i] + ";";
      }
    }
    
    s = s.substring(0, s.length() - 1); // remove the trailing semi-colon
    
    s = s + "," + name;
    
    return s;
  }
  
  public double distanceTo(LatLong loc)
  {
    if(location == null) return 0;
    return location.distanceTo(loc); // this needs to be multiplied by the radius to get a true measure
  }
  
  public double distanceTo(MarsWaypoint w)
  {
    return distanceTo(w.location);
  }
  
  public boolean isAt(LatLong loc)
  {
    if(location == null) return false;
    return location.isAt(loc);
  }
  
  public void addLink(int link)
  {
    int [] links2 = null;
    if (links == null)
    {
      links = new int[1];
      links[0] = link;
    }
    else
    {
      links2 = new int[links.length + 1];
      for(int i=0; i<links.length; i++)
      {
        links2[i] = links[i];
      }
      links2[links.length] = link;
      links = links2;
    }
  }
  
  public boolean isLinked()
  {
    return (links != null);
  }
  
  public int getStoredResources()
  {
    return resourcesId;
  }
  
  public boolean isSurveyed()
  {
    return !(surveyedBy == 0);
  }
  
  public int[] getLinks()
  {
    return links;
  }
  
  public void setSurveyed(int surveyor)
  {
    surveyedBy = surveyor;
  }
  
  public String getName()
  {
    return toString();
  }
}
