package leigh.astro.unit;

import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import leigh.mecha.util.NumberUtil;

import java.util.Objects;

/**
 * Holds a spherical coordinate, with both units internally represented as degrees.
 *
 * @author Alex Leigh
 */
public class SphericalCoordinate {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(SphericalCoordinate.class);

    private final double x;
    private final double y;

    public SphericalCoordinate(double x, double dec) {
        this.x = x;
        this.y = dec;
    }

    /**
     * Return X in degrees.
     */
    public double getX() {
        return x;
    }

    /**
     * Return Y in degrees.
     */
    public double getY() {
        return y;
    }

    /**
     * Return X in radians.
     */
    public double getXrad() {
        return Math.toRadians(x);
    }

    /**
     * Return Y in radians.
     */
    public double getYrad() {
        return Math.toRadians(y);
    }

    /**
     * Return X in hours.
     */
    public double getXhours() {
        return x / 15;
    }


    public HoursMinutesSeconds getHms() {
        return new HoursMinutesSeconds(x);
    }

    /**
     * This method is deprecated on account of being stupid.
     */
    @Deprecated
    public boolean isNear(SphericalCoordinate c) {
        logger.debug("comparing: {} {}", this, c);
        double slop = 1;

        double xt = NumberUtil.precision(x, 1);
        double yt = NumberUtil.precision(y, 1);
        double cx = NumberUtil.precision(c.getX(), 1);
        double cy = NumberUtil.precision(c.getY(), 1);

        logger.debug("Comparing: {} {} {} {}", xt, cx, yt, cy);
        if (cx < xt + slop && cx > xt - slop && cy < yt + slop && cy > yt - slop) return true;
        return false;
    }

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

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

    @Override
    public String toString() {
        return "SphericalCoordinate{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }
}
