use crate::DEIMOS_RADIUS_M;

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DeimosCoord {
    pub latitude_deg: f64,
    pub longitude_deg: f64,
    pub elevation_m: f64,
}

impl DeimosCoord {
    pub fn new(latitude_deg: f64, longitude_deg: f64, elevation_m: f64) -> Self {
        Self {
            latitude_deg,
            longitude_deg,
            elevation_m,
        }
    }
}

pub fn great_circle_distance_km(a: DeimosCoord, b: DeimosCoord) -> f64 {
    let lat1 = a.latitude_deg.to_radians();
    let lat2 = b.latitude_deg.to_radians();
    let dlat = (b.latitude_deg - a.latitude_deg).to_radians();
    let dlon = (b.longitude_deg - a.longitude_deg).to_radians();
    let hav = (dlat * 0.5).sin().powi(2) + lat1.cos() * lat2.cos() * (dlon * 0.5).sin().powi(2);
    let central_angle = 2.0 * hav.sqrt().asin();
    DEIMOS_RADIUS_M * central_angle / 1_000.0
}

pub fn antipode(coord: DeimosCoord) -> DeimosCoord {
    let longitude = if coord.longitude_deg >= 0.0 {
        coord.longitude_deg - 180.0
    } else {
        coord.longitude_deg + 180.0
    };
    DeimosCoord::new(-coord.latitude_deg, longitude, -coord.elevation_m)
}
