use std::ffi::{CString, NulError};
use std::mem::{self, MaybeUninit};
use xplane_sys::{
XPLMFixedString150_t, XPLMGetMETARForAirport, XPLMGetWeatherAtLocation, XPLMWeatherInfo_t,
};
use crate::NoSendSync;
pub struct WeatherApi {
pub(crate) _phantom: NoSendSync,
}
impl WeatherApi {
#[allow(clippy::cast_sign_loss)]
pub fn get_aerodrome_metar<S: Into<Vec<u8>>>(ad: S) -> Result<String, NulError> {
let ad = CString::new(ad)?;
let mut get_metar_out = XPLMFixedString150_t { buffer: [0i8; 150] };
unsafe {
XPLMGetMETARForAirport(ad.as_ptr(), &mut get_metar_out);
};
let buffer =
Vec::from(unsafe { mem::transmute::<[i8; 150], [u8; 150]>(get_metar_out.buffer) });
Ok(String::from_utf8(buffer).unwrap())
}
#[must_use]
pub fn get_weather_at_location(lat: f64, lon: f64, alt_m: f64) -> Option<XPLMWeatherInfo_t> {
let mut weather: MaybeUninit<XPLMWeatherInfo_t> = MaybeUninit::zeroed();
if unsafe { XPLMGetWeatherAtLocation(lat, lon, alt_m, weather.as_mut_ptr()) } == 1 {
unsafe { Some(weather.assume_init()) }
} else {
None
}
}
}