package uk.co.patrickhaston.mars;

public class MarsLink extends MarsData 
{
  private int wp[] = new int[2];
  private int linkType;
  private double distance;
 
  /**
   * Constructors  
   */
  public MarsLink(int id)
  {
    super(id);
    wp[0] = 0;
    wp[1] = 0;
    linkType = 1;
    distance = 0;
  }
  
  public MarsLink(String line)
  {
    super(line);
    
    wp[0] = readInteger(line, 0);
    wp[1] = readInteger(line, 1);
    linkType = readInteger(line, 2);
    distance = readDouble(line, 3);
  }
  
  public MarsLink(int id, int w1, int w2, double dist)
  {
    super(id);
    wp[0] = w1;
    wp[1] = w2;
    linkType = 1;
    distance = dist;
  }

  /**
   * read / write methods  
   */
  public String toString()
  {
    String s = new String(); //super.toString();
    
    s = "Link from " + Integer.toString(wp[0]) + " to "+ Integer.toString(wp[0]);
    s = s + " of type " + linkType + " of length " + distance;
    
    return s;
  }
  
  public String toFile()
  {
    String s = new String(); //super.toFile();
    
    s = Integer.toString(wp[0]) + ","+ Integer.toString(wp[0]);
    s = s + "," + linkType + "," + distance;
    
    return s;
  }

  /**
   * Accessors
   */
  public int getType()
  {
    return linkType;
  }
  
  public void setType(int t)
  {
    linkType = t;
  }
  
  public double getDistance()
  {
    return distance;
  }
  
  public void setDistance(double dist)
  {
    distance = dist;
  }
  
  public int[] getEnds()
  {
    return wp;
  }

  public int getOtherEnd(int waypoint)
  {
    if(wp == null) return 0;
    if(wp[0] == waypoint) return wp[1];
    if(wp[1] == waypoint) return wp[0];
    return 0;
  }
  
  /**
   * Other methods  
   */
  public boolean isLinked(int w)
  {
    if (wp[0] == w || wp[1] == w) return true;
    return false;
  }

  public boolean isLinked(int w1, int w2)
  {
    if (wp[0] == w1 && wp[1] == w2) return true;
    if (wp[1] == w1 && wp[0] == w2) return true;
    return false;
  }
}
