package mock.model.commandFactory;

import mock.model.MockBoat;
import mock.model.MockRace;
import shared.model.Bearing;

/**
 * Command class for tacking and gybing
 */
public class TackGybeCommand implements Command {
    private MockRace race;
    private MockBoat boat;

    /**
     * Constructor for class
     * @param race mock race
     * @param boat mock boat to update
     */
    public TackGybeCommand(MockRace race, MockBoat boat) {
        this.race = race;
        this.boat = boat;
    }

    @Override
    public void execute() {

        boat.setAutoVMG(false);

        double boatAngle = boat.getBearing().degrees();
        double windAngle =race.getWindDirection().degrees();
        double differenceAngle = calcDistance(boatAngle, windAngle);
        double angleA = windAngle + differenceAngle;
        double angleB = windAngle - differenceAngle;
        if(angleA % 360 == boatAngle){
            boat.setBearing(Bearing.fromDegrees(angleB));
        } else {
            boat.setBearing(Bearing.fromDegrees(angleA));
        }
    }

    /**
     * Method to calculate smallest angle between 2 angles
     * @param degreeA first angle degree
     * @param degreeB second angle degree
     * @return the calculated smallest angle
     */
    public double calcDistance(double degreeA, double degreeB){
        double phi = Math.abs(degreeB - degreeA) % 360;
        return phi > 180 ? 360 - phi : phi;
    }

}

