package seng302.Model;

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

import java.awt.geom.Point2D;

import static junit.framework.TestCase.assertEquals;
import static seng302.Model.Leg.NM_TO_METERS;

/**
 * Created by esa46 on 22/03/17.
 */
public class LegTest {

    private final Marker ORIGIN_Marker = new Marker(new GPSCoordinate(0, 0));

    @Test
    public void calculateDistanceHandles5nmNorth() {
        GeodeticCalculator calc = new GeodeticCalculator();
        calc.setStartingGeographicPoint(0, 0);
        calc.setDirection(0, 5 * NM_TO_METERS);

        Marker endMarker = getEndMarker(calc.getDestinationGeographicPoint());
        Leg test = new Leg("Test", ORIGIN_Marker, endMarker, 0);
        assertEquals(test.getDistance(), 5, 1e-8);
    }

    @Test
    public void calculateDistanceHandles12nmEast() {
        GeodeticCalculator calc = new GeodeticCalculator();
        calc.setStartingGeographicPoint(0, 0);
        calc.setDirection(90, 12 * NM_TO_METERS);

        Marker endMarker = getEndMarker(calc.getDestinationGeographicPoint());
        Leg test = new Leg("Test", ORIGIN_Marker, endMarker, 0);
        assertEquals(test.getDistance(), 12, 1e-8);
    }

    @Test
    public void calculateDistanceHandlesHalfnmSouth() {
        GeodeticCalculator calc = new GeodeticCalculator();
        calc.setStartingGeographicPoint(0, 0);
        calc.setDirection(180, 0.5 * NM_TO_METERS);

        Marker endMarker = getEndMarker(calc.getDestinationGeographicPoint());
        Leg test = new Leg("Test", ORIGIN_Marker, endMarker, 0);
        assertEquals(test.getDistance(), 0.5, 1e-8);
    }

    @Test
    public void calculateDistanceHandlesPoint1nmWest() {
        GeodeticCalculator calc = new GeodeticCalculator();
        calc.setStartingGeographicPoint(0, 0);
        calc.setDirection(-90, 0.1 * NM_TO_METERS);

        Marker endMarker = getEndMarker(calc.getDestinationGeographicPoint());
        Leg test = new Leg("Test", ORIGIN_Marker, endMarker, 0);
        assertEquals(test.getDistance(), 0.1, 1e-8);
    }

    @Test
    public void calculateDistanceHandlesZeroDifference() {

        Leg test = new Leg("Test", ORIGIN_Marker, ORIGIN_Marker, 0);
        assertEquals(test.getDistance(), 0, 1e-8);
    }

    private Marker getEndMarker(Point2D point) {

        GPSCoordinate coords = new GPSCoordinate(point.getY(), point.getX());

        return new Marker(coords);

    }
}
