package io.github.js0ny.ilp_coursework.data.common;

/**
 * Represents the data transfer object for a point or coordinate that defines by a longitude and
 * latitude
 *
 * @param lng longitude of the coordinate/point
 * @param lat latitude of the coordinate/point
 */
public record LngLat(double lng, double lat) {
    private static final double EPSILON = 1e-9;

    public LngLat {
        if (lat < -90 || lat > 90) {
            throw new IllegalArgumentException(
                    "Latitude must be between -90 and +90 degrees. Got: " + lat);
        }

        if (lng < -180 || lng > 180) {
            throw new IllegalArgumentException(
                    "Longitude must be between -180 and +180 degrees. Got: " + lng);
        }
    }

    public LngLat(LngLatAlt coord) {
        this(coord.lng(), coord.lat());
    }

    public boolean isSamePoint(LngLat other) {
        if (other == null) {
            return false;
        }
        return (Math.abs(lng - other.lng()) < EPSILON && Math.abs(lat - other.lat()) < EPSILON);
    }
}
