
import java.util.ArrayList;
import java.util.List;

import org.jgrapht.graph.*;
import org.jgrapht.*;

import processing.core.PApplet;

public class RoadEdge<V> extends DefaultEdge {
  private PApplet p;
  private static final int DENSITY = 1;
  @Override
    public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((hash == null) ? 0 : hash.hashCode());
    return result;
  }

  private RoadVertex from, to;
  private double length;
  private List<float[]> pathPositions;
  private List<Car>  cars;
  private V v;
  private String hash;
  private static int nrID = 0;
  private int ID;

  RoadEdge (PApplet parent, RoadVertex from, RoadVertex to) {
    p = parent;
    this.from = from;
    this.to = to;
    pathPositions = new ArrayList <float[]>(); 
    cars = new ArrayList();

    length = Math.sqrt ((to.getX() - from.getX()) * (to.getX() - from.getX()) + (to.getY() - from.getY()) * (to.getY() - from.getY()));
    int nrPositions = (int) length/DENSITY;
    float dx = (to.getX() - from.getX()) / nrPositions;
    float dy = (to.getY() - from.getY()) / nrPositions;		
    for (int i = 0; i < nrPositions; i++) {
      float pointX = from.getX() + i * dx;
      float pointY = from.getY() + i * dy;
      float [] coords = new float [2];
      coords[0] =  pointX;
      coords[1] =  pointY;
      pathPositions.add(coords);
      //p.println (coords);
    }
    hash = from.getX() + "+" + from.getY() + "+" + to.getX() + "+" + to.getY();
    ID = ++nrID;
  }




  public int getID() {
    return ID;
  }


  public String getHash() {
    return hash;
  }

  public static int getDensity() {
    return DENSITY;
  }

  public List<float[]> getPathPositions() {
    return pathPositions;
  }



  public List<Car> getCars() {
    return cars;
  }

  public void setCars(List<Car> cars) {
    this.cars = cars;
  }

  void addCar (Car car) {
    cars.add(car);
  }

  public void draw () {
    p.line(from.getX(), from.getY(), to.getX(), to.getY());
  }

  public void drawDebug () {
    p.stroke(0, 255, 255);
    p.strokeWeight(4);
    p.line(from.getX(), from.getY(), to.getX(), to.getY());
    //p.text(this.getID(), to.getX(), to.getY());
    p.stroke(255);
    //p.println (from.getId() + " " + to.getId());
    p.strokeWeight(1);
  }

  public RoadVertex getFrom() {
    return from;
  }

  public void setFrom(RoadVertex from) {
    this.from = from;
  }

  public RoadVertex getTo() {
    return to;
  }

  public void setTo(RoadVertex to) {
    this.to = to;
  }

  public double getLength() {
    return length;
  }

  public void setLength(double length) {
    this.length = length;
  }
}

