use crate::message::FromBody;
use crate::NexradParseError;

pub const MSG_RDA_STATUS_DATA: u8 = 2;

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Msg02RDAStatusData {
    pub rda_status: u16,
    pub operability_status: u16,
    pub control_status: u16,
    pub aux_power_state: u16,
    pub average_transmitter_power_watts: u16,
    pub horiz_ref_calib_corr: u16,
    pub data_tx_enabled: u16,
    pub vcp: i16,
    pub rdaca: u16,
    pub rda_build_no: u16,
    pub opmode: u16,
    pub super_resolution_status: u16,
    pub clutter_mitigation_status: u16,
    pub rda_scan_data_flags: u16,
    pub rda_alarm_summary: u16,
    pub command_ack: u16,
    pub channel_con_stat: u16,
    pub spot_blank_stat: u16,
    pub bypass_map_gen_date: u16,
    pub bypass_map_gen_time: u16,
    pub clutter_map_gen_date: u16,
    pub clutter_map_gen_time: u16,
    pub vert_ref_calib_corr: u16,
    pub tps_status: u16,
    pub rms_control_status: u16,
    pub perf_check_status: u16,
    pub alarm: u16,
    pub extra: [u8; 8],
}

impl FromBody<68> for Msg02RDAStatusData {
    fn from_body(body: [u8; 68]) -> Result<Self, NexradParseError> {
        Ok(Self {
            rda_status: u16::from_be_bytes(body[0..2].try_into().unwrap()),
            operability_status: u16::from_be_bytes(body[2..4].try_into().unwrap()),
            control_status: u16::from_be_bytes(body[4..6].try_into().unwrap()),
            aux_power_state: u16::from_be_bytes(body[6..8].try_into().unwrap()),
            average_transmitter_power_watts: u16::from_be_bytes(body[8..10].try_into().unwrap()),
            horiz_ref_calib_corr: u16::from_be_bytes(body[10..12].try_into().unwrap()),
            data_tx_enabled: u16::from_be_bytes(body[12..14].try_into().unwrap()),
            vcp: i16::from_be_bytes(body[14..16].try_into().unwrap()),
            rdaca: u16::from_be_bytes(body[16..18].try_into().unwrap()),
            rda_build_no: u16::from_be_bytes(body[20..22].try_into().unwrap()),
            opmode: u16::from_be_bytes(body[22..24].try_into().unwrap()),
            super_resolution_status: u16::from_be_bytes(body[24..26].try_into().unwrap()),
            clutter_mitigation_status: u16::from_be_bytes(body[26..28].try_into().unwrap()),
            rda_scan_data_flags: u16::from_be_bytes(body[30..32].try_into().unwrap()),
            rda_alarm_summary: u16::from_be_bytes(body[32..34].try_into().unwrap()),
            command_ack: u16::from_be_bytes(body[34..36].try_into().unwrap()),
            channel_con_stat: u16::from_be_bytes(body[36..38].try_into().unwrap()),
            spot_blank_stat: u16::from_be_bytes(body[38..40].try_into().unwrap()),
            bypass_map_gen_date: u16::from_be_bytes(body[40..42].try_into().unwrap()),
            bypass_map_gen_time: u16::from_be_bytes(body[42..44].try_into().unwrap()),
            clutter_map_gen_date: u16::from_be_bytes(body[44..46].try_into().unwrap()),
            clutter_map_gen_time: u16::from_be_bytes(body[46..48].try_into().unwrap()),
            vert_ref_calib_corr: u16::from_be_bytes(body[50..52].try_into().unwrap()),
            tps_status: u16::from_be_bytes(body[52..54].try_into().unwrap()),
            rms_control_status: u16::from_be_bytes(body[54..56].try_into().unwrap()),
            perf_check_status: u16::from_be_bytes(body[56..58].try_into().unwrap()),
            alarm: u16::from_be_bytes(body[58..60].try_into().unwrap()),
            extra: body[60..].try_into().unwrap(),
        })
    }
}