package de.thdeg.greiner.superpangworld.graphics.base;

import java.util.Objects;

/**
 * The position of an object on the game window represented by 2 dimensional coordinates.
 */
public class Position implements Cloneable, Comparable<Position>{

    /** The x-coordinate */
    public double x;
    /** The y-coordinate */
    public double y;

    /**
     * Create a position.
     *
     * @param x the x-coordinate of the position
     * @param y the y-coordinate of the position
     */
    public Position(double x, double y) {
        this.x = x;
        this.y = y;
    }

    /**
     * Move the position to the left.
     *
     * @param pixel the amount of pixels to move
     */
    public void left(double pixel){
        x -= pixel;
    }

    /**
     * Move the position to the right.
     *
     * @param pixel the amount of pixels to move
     */
    public void right(double pixel){
        x += pixel;
    }

    /**
     * Move the position upwards.
     *
     * @param pixel the amount of pixels to move
     */
    public void up(double pixel){
        y -= pixel;
    }

    /**
     * Move the position downwards.
     *
     * @param pixel the amount of pixels to move
     */
    public void down(double pixel){
        y += pixel;
    }

    /**
     * Calculates the distance to any other position.
     *
     * @param other Position to calculate the distance to.
     * @return The distance.
     */
    public double distance(Position other) {
        return Math.sqrt(Math.pow((x - other.x), 2) + Math.pow((y - other.y), 2));
    }

    @Override
    public String toString() {
        return "Position(" + (int) Math.round(x) + ", " + (int) Math.round(y) + ')';
    }

    @Override
    public Position clone() {
        Position other = null;
        try{
            other = (Position) super.clone();
        }catch(CloneNotSupportedException ignored){
        }
        return other;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Position position = (Position) o;
        return Double.compare(position.x, x) == 0 && Double.compare(position.y, y) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }

    @Override
    public int compareTo(Position o) {
        Position p0 = new Position(0,0);
        return (int)Math.signum(this.distance(p0)-o.distance(p0));
    }
}
