package seng302.Model;

import org.geotools.referencing.GeodeticCalculator;
import seng302.GPSCoordinate;

/**
 * Created by cbt24 on 6/03/17.
 */
public class Leg {
    private final String name; //nautical miles
    private double distance;
    private Marker startMarker;
    private Marker endMarker;
    private final int legNumber;
    // 1 nautical mile = 1852 meters
    public static final int NM_TO_METERS = 1852;

    /**
     * Leg Initializer
     *
     * @param name   Name of the Leg
     * @param start  marker
     * @param end    marker
     * @param number Leg's position in race
     */
    public Leg(String name, Marker start, Marker end, int number) {
        this.name = name;
        this.startMarker = start;
        this.endMarker = end;
        this.legNumber = number;
        calculateDistance();
    }

    /**
     * Construction Method
     *
     * @param name   Name of the Leg
     * @param number leg number
     */
    public Leg(String name, int number) {
        this.name = name;
        this.legNumber = number;
    }

    /**
     * Returns the name of the Leg
     *
     * @return Returns the name of the Leg
     */
    public String getName() {
        return name;
    }

    /**
     * Get the distance in nautical miles
     *
     * @return Returns the total distance of the leg.
     */
    public double getDistance() {
        return distance;
    }

    /**
     * Returns the leg number that the leg exists in the Race
     *
     * @return Returns the Leg
     */
    public int getLegNumber() {
        return legNumber;
    }


    public Marker getStartMarker() {
        return startMarker;
    }

    public void setStartMarker(Marker startMarker) {
        this.startMarker = startMarker;
    }

    public Marker getEndMarker() {
        return endMarker;
    }

    public void setEndMarker(Marker endMarker) {
        this.endMarker = endMarker;
    }

    /**
     * Calculates the distance that the legs are in nautical miles (1.852 km).
     */
    private void calculateDistance() {

        GeodeticCalculator calc = new GeodeticCalculator();
        //Load start and end of leg
        GPSCoordinate startMarker = this.startMarker.getAverageGPSCoordinate();
        GPSCoordinate endMarker = this.endMarker.getAverageGPSCoordinate();
        calc.setStartingGeographicPoint(startMarker.getLongitude(), startMarker.getLatitude());
        calc.setDestinationGeographicPoint(endMarker.getLongitude(), endMarker.getLatitude());
        this.distance = calc.getOrthodromicDistance() / NM_TO_METERS;
    }


}
