package seng302.Model;


/**
 * Represents a compound mark -  that is, either one or two individual marks which form a single compound mark.
 */
public class CompoundMark {

    /**
     * The first mark in the compound mark.
     */
    private Mark mark1;

    /**
     * The second mark in the compound mark.
     */
    private Mark mark2;

    /**
     * The average coordinate of the compound mark.
     */
    private GPSCoordinate averageGPSCoordinate;


    /**
     * Constructs a compound mark from a single mark.
     * @param mark1 The individual mark that comprises this compound mark.
     */
    public CompoundMark(Mark mark1) {
        this.mark1 = mark1;
        this.averageGPSCoordinate = calculateAverage();

    }


    /**
     * Constructs a compound mark from a pair of marks.
     * @param mark1 The first individual mark that comprises this compound mark.
     * @param mark2 The second individual mark that comprises this compound mark.
     */
    public CompoundMark(Mark mark1, Mark mark2) {
        this.mark1 = mark1;
        this.mark2 = mark2;
        this.averageGPSCoordinate = calculateAverage();

    }


    /**
     * Returns the first mark of the compound mark.
     * @return The first mark of the compound mark.
     */
    public Mark getMark1() {
        return mark1;
    }

    /**
     * Returns the second mark of the compound mark.
     * @return The second mark of the compound mark.
     */
    public Mark getMark2() {
        return mark2;
    }


    /**
     * Returns the position of the first mark in the compound mark.
     * @return The position of the first mark in the compound mark.
     */
    public GPSCoordinate getMark1Position() {
        return mark1.getPosition();
    }

    /**
     * Returns the position of the second mark in the compound mark.
     * @return The position of the second mark in the compound mark.
     */
    public GPSCoordinate getMark2Position() {
        return mark2.getPosition();
    }


    /**
     * Returns the average coordinate of the compound mark.
     * @return The average coordinate of the compound mark.
     */
    public GPSCoordinate getAverageGPSCoordinate() {
        return averageGPSCoordinate;
    }


    /**
     * Calculates the average coordinate of the compound mark.
     * @return The average coordinate of the compound mark.
     */
    private GPSCoordinate calculateAverage() {

        //If the compound mark only contains one mark, the average is simply the first mark's position.
        if (this.mark2 == null) {
            return this.getMark1Position();
        }


        //Otherwise, calculate the average of both marks.
        GPSCoordinate averageCoordinate = GPSCoordinate.calculateAverageCoordinate(this.getMark1Position(), this.getMark2Position());

        return averageCoordinate;

    }
}
