package worms.util;

import be.kuleuven.cs.som.annotate.Basic;
import be.kuleuven.cs.som.annotate.Immutable;
import be.kuleuven.cs.som.annotate.Value;

@Value
public class Coordinate extends Tuple<Double, Double> {
    /**
     * Initialize this new Tuple as a Tuple with the given items.
     *
     * @param coordinateX The x-coordinate for this Coordinate
     * @param coordinateY The y-coordinate for this Coordinate
     */
    public Coordinate(double coordinateX, double coordinateY) throws IllegalArgumentException {
        super(coordinateX, coordinateY);
    }

    /**
     *
     * @param   coordinateX
     *          The value for the x-coordinate.
     * @param   coordinateY
     *          The value for the y-coordinate.
     * @return
     *          The coordinate created from coordinateX and coordinateY.
     *          | result == new Coordinate(coordinateX, coordinateY);
     */
    @Immutable
    public static Coordinate create(double coordinateX, double coordinateY){
        return new Coordinate(coordinateX, coordinateY);
    }

    /**
     *  Return a new Coordinate created from the elements of the array.
     *
     * @param   arr
     *          The Array to create the Coordinate of.
     * @return  The Tuple created from the elements of the given array.
     *          | result == new Coordinate(arr[0], arr[1])
     * @throws IllegalArgumentException
     *          The given array is null or the length of the given array
     *          is not 2.
     *          | arr == null || arr.length != 2
     */
    @Immutable
    public static Coordinate create(double[] arr) throws IllegalArgumentException {
        if (arr == null || arr.length != 2)
            throw new IllegalArgumentException("Invalid parameter arr");
        return new Coordinate(arr[0], arr[1]);
    }

    public double[] toArray() {

        return new double[]{getX(), getY()};
    }

    @Basic @Immutable
    public double getX() {
        return this.item1;
    }
    @Basic @Immutable
    public double getY() {
        return this.item2;
    }
}
