package com.base.track.model.entity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Position extends LatLon{
    public static final Position ZERO;
    public final double elevation;

    static {
        ZERO = new Position(Angle.ZERO, Angle.ZERO, 0.0D);
    }

    public static Position fromRadians(double latitude, double longitude, double elevation) {
        return new Position(Angle.fromRadians(latitude), Angle.fromRadians(longitude), elevation);
    }

    public static Position fromDegrees(double latitude, double longitude, double elevation) {
        return new Position(Angle.fromDegrees(latitude), Angle.fromDegrees(longitude), elevation);
    }

    public static Position fromDegrees(double latitude, double longitude) {
        return new Position(Angle.fromDegrees(latitude), Angle.fromDegrees(longitude), 0.0D);
    }

    public Position(Angle latitude, Angle longitude, double elevation) {
        super(latitude, longitude);
        this.elevation = elevation;
    }

    public Position(LatLon latLon, double elevation) {
        super(latLon);
        this.elevation = elevation;
    }

    public double getElevation() {
        return this.elevation;
    }

    public double getAltitude() {
        return this.elevation;
    }

    public LatLon getLatLon() {
        return new LatLon(this.latitude, this.longitude);
    }

    public Position add(Position that) {
        Angle lat = Angle.normalizedLatitude(this.latitude.add(that.latitude));
        Angle lon = Angle.normalizedLongitude(this.longitude.add(that.longitude));
        return new Position(lat, lon, this.elevation + that.elevation);
    }

    public Position subtract(Position that) {
        Angle lat = Angle.normalizedLatitude(this.latitude.subtract(that.latitude));
        Angle lon = Angle.normalizedLongitude(this.longitude.subtract(that.longitude));
        return new Position(lat, lon, this.elevation - that.elevation);
    }

    public static boolean positionsCrossDateLine(Iterable<? extends Position> positions) {
        if (positions == null) {
            throw new IllegalArgumentException("nullValue.PositionsListIsNull");
        } else {
            Position pos = null;

            Position posNext;
            for(Iterator var3 = positions.iterator(); var3.hasNext(); pos = posNext) {
                posNext = (Position)var3.next();
                if (pos != null && Math.signum(pos.getLongitude().degrees) != Math.signum(posNext.getLongitude().degrees)) {
                    double delta = Math.abs(pos.getLongitude().degrees - posNext.getLongitude().degrees);
                    if (delta > 180.0D && delta < 360.0D) {
                        return true;
                    }
                }
            }

            return false;
        }
    }

    public static List<Position> computeShiftedPositions(Position oldPosition, Position newPosition, Iterable<? extends Position> positions) {
        if (oldPosition != null && newPosition != null) {
            if (positions == null) {
                throw new IllegalArgumentException("nullValue.PositionsListIsNull");
            } else {
                ArrayList<Position> newPositions = new ArrayList();
                double elevDelta = newPosition.getElevation() - oldPosition.getElevation();
                Iterator var7 = positions.iterator();

                while(var7.hasNext()) {
                    Position pos = (Position)var7.next();
                    Angle distance = LatLon.greatCircleDistance(oldPosition, pos);
                    Angle azimuth = LatLon.greatCircleAzimuth(oldPosition, pos);
                    LatLon newLocation = LatLon.greatCircleEndPosition(newPosition, azimuth, distance);
                    double newElev = pos.getElevation() + elevDelta;
                    newPositions.add(new Position(newLocation, newElev));
                }

                return newPositions;
            }
        } else {
            throw new IllegalArgumentException("nullValue.PositionIsNull");
        }
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        } else if (o != null && this.getClass() == o.getClass()) {
            if (!super.equals(o)) {
                return false;
            } else {
                Position position = (Position)o;
                return Double.compare(position.elevation, this.elevation) == 0;
            }
        } else {
            return false;
        }
    }

    public int hashCode() {
        int result = super.hashCode();
        long temp = this.elevation != 0.0D ? Double.doubleToLongBits(this.elevation) : 0L;
        result = 31 * result + (int)(temp ^ temp >>> 32);
        return result;
    }

    public String toString() {
        return "(" + this.latitude.toString() + ", " + this.longitude.toString() + ", " + this.elevation + ")";
    }

    public static class PositionList {
        public List<? extends Position> list;

        public PositionList(List<? extends Position> list) {
            this.list = list;
        }
    }
}
