use std::ffi::CString;

use simconnect_sdk::SimConnectObject;

#[allow(unused)]
#[derive(Debug, Clone, SimConnectObject)]
#[simconnect(period = "second", condition = "changed")]
pub(super) struct AtcID {
    #[simconnect(name = "ATC ID")]
    pub value: String,
}

pub(super) fn atc_id(value: &str) -> Result<AtcIDCPacked, anyhow::Error> {
    if value.len() > 10 {
        return Err(anyhow::anyhow!("ATC_ID exceeding 10 characters"));
    }
    let cs = CString::new(value.to_uppercase())?;
    let mut buffer = [0i8; 256];
    for (i, b) in cs.to_bytes_with_nul().iter().enumerate() {
        buffer[i] = *b as i8;
    }
    Ok(AtcIDCPacked { value: buffer })
}

#[derive(Debug, Clone, SimConnectObject)]
#[simconnect(period = "second", condition = "changed")]
pub(super) struct Airplane {
    #[simconnect(name = "ATC TYPE")]
    pub atc_type: String,
    #[simconnect(name = "ATC MODEL")]
    pub atc_model: String,
    #[simconnect(name = "TITLE")]
    pub title: String,
    #[simconnect(name = "CATEGORY")]
    pub category: String,
}

#[derive(Debug, Clone, SimConnectObject)]
#[simconnect(period = "second", condition = "changed")]
pub(super) struct Fuel {
    #[simconnect(name = "FUEL TANK CENTER QUANTITY", unit = "gallons")]
    pub center_quantity: f64,
    #[simconnect(name = "FUEL TANK CENTER CAPACITY", unit = "gallons")]
    pub center_capacity: f64,

    #[simconnect(name = "FUEL TANK CENTER2 QUANTITY", unit = "gallons")]
    pub center2_quantity: f64,
    #[simconnect(name = "FUEL TANK CENTER2 CAPACITY", unit = "gallons")]
    pub center2_capacity: f64,

    #[simconnect(name = "FUEL TANK CENTER3 QUANTITY", unit = "gallons")]
    pub center3_quantity: f64,
    #[simconnect(name = "FUEL TANK CENTER3 CAPACITY", unit = "gallons")]
    pub center3_capacity: f64,

    #[simconnect(name = "FUEL TANK LEFT MAIN QUANTITY", unit = "gallons")]
    pub left_main_quantity: f64,
    #[simconnect(name = "FUEL TANK LEFT MAIN CAPACITY", unit = "gallons")]
    pub left_main_capacity: f64,

    #[simconnect(name = "FUEL TANK LEFT AUX QUANTITY", unit = "gallons")]
    pub left_aux_quantity: f64,
    #[simconnect(name = "FUEL TANK LEFT AUX CAPACITY", unit = "gallons")]
    pub left_aux_capacity: f64,

    #[simconnect(name = "FUEL TANK LEFT TIP QUANTITY", unit = "gallons")]
    pub left_tip_quantity: f64,
    #[simconnect(name = "FUEL TANK LEFT TIP CAPACITY", unit = "gallons")]
    pub left_tip_capacity: f64,

    #[simconnect(name = "FUEL TANK RIGHT MAIN QUANTITY", unit = "gallons")]
    pub right_main_quantity: f64,
    #[simconnect(name = "FUEL TANK RIGHT MAIN CAPACITY", unit = "gallons")]
    pub right_main_capacity: f64,

    #[simconnect(name = "FUEL TANK RIGHT AUX QUANTITY", unit = "gallons")]
    pub right_aux_quantity: f64,
    #[simconnect(name = "FUEL TANK RIGHT AUX CAPACITY", unit = "gallons")]
    pub right_aux_capacity: f64,

    #[simconnect(name = "FUEL TANK RIGHT TIP QUANTITY", unit = "gallons")]
    pub right_tip_quantity: f64,
    #[simconnect(name = "FUEL TANK RIGHT TIP CAPACITY", unit = "gallons")]
    pub right_tip_capacity: f64,

    #[simconnect(name = "FUEL TANK EXTERNAL1 QUANTITY", unit = "gallons")]
    pub external1_quantity: f64,
    #[simconnect(name = "FUEL TANK EXTERNAL1 CAPACITY", unit = "gallons")]
    pub external1_capacity: f64,

    #[simconnect(name = "FUEL TANK EXTERNAL2 QUANTITY", unit = "gallons")]
    pub external2_quantity: f64,
    #[simconnect(name = "FUEL TANK EXTERNAL2 CAPACITY", unit = "gallons")]
    pub external2_capacity: f64,

    #[simconnect(name = "FUEL WEIGHT PER GALLON", unit = "pounds")]
    pub weight: f64,
}

#[derive(Debug, Clone, SimConnectObject)]
#[simconnect(period = "second", condition = "changed")]
pub(super) struct Gps {
    #[simconnect(name = "PLANE LATITUDE", unit = "degrees")]
    pub lat: f64,
    #[simconnect(name = "PLANE LONGITUDE", unit = "degrees")]
    pub lon: f64,
    #[simconnect(name = "PLANE ALTITUDE", unit = "feet")]
    pub alt: f64,
}

#[derive(Debug, Clone, SimConnectObject)]
#[simconnect(period = "second", condition = "changed")]
pub(super) struct OnGround {
    #[simconnect(name = "SIM ON GROUND")]
    pub sim_on_ground: bool,
}

#[derive(Debug, Clone, SimConnectObject)]
#[simconnect(period = "second", condition = "changed")]
pub(super) struct IsParked {
    #[simconnect(name = "PLANE IN PARKING STATE")]
    pub is_parked: bool,
}
