import ctypes
from .enums import NavDataInvalidField, CrsNormalSlaveSelector, CrsSnowploughCommand
import Grifo_E_1553lib.data_types.enum_map as em
em.ENUM_MAP.update({
    "cursor_normal_slave_selector" : CrsNormalSlaveSelector,
    "cursor_snowplough_command" : CrsSnowploughCommand,
    
})
class _A4ValidityAndSlewStr(ctypes.LittleEndianStructure):
    _pack_ = 1
    _fields_ = [
        ("spare", ctypes.c_uint16, 1),              
        ("cursor_snowplough_command", ctypes.c_uint16, 1),              
        ("cursor_zero_command", ctypes.c_uint16, 1),              
        ("cursor_normal_slave_selector", ctypes.c_uint16, 1),              
        ("cursor_rates_invalid", ctypes.c_uint16, 1),              
        ("antenna_slew", ctypes.c_uint16, 1),              
        ("present_position_data_invalid", ctypes.c_uint16, 1),              
        ("cas_data_invalid", ctypes.c_uint16, 1),              
        ("tas_data_invalid_dadc", ctypes.c_uint16, 1),              
        ("spoi_position_data_invalid", ctypes.c_uint16, 1),              
        ("spoi_altitude_data_invalid", ctypes.c_uint16, 1),              
        ("radio_altimeter_altitude_data_invalid", ctypes.c_uint16, 1),              
        ("corrected_barometric_altitude_data_invalid", ctypes.c_uint16, 1),              
        ("baro_inertial_altitude_data_invalid", ctypes.c_uint16, 1),              
        ("altitude_data_invalid", ctypes.c_uint16, 1),              
        ("navigation_data_invalid", ctypes.c_uint16, 1),              
    ]


class A4ValidityAndSlew(ctypes.Union):
    _pack_ = 1
    _fields_ = [
        ("raw", ctypes.c_uint16),
        ("str", _A4ValidityAndSlewStr)
    ]

    def get_navigation_invalid(self):
        return (self.raw >> 15) & 0x01

    def set_navigation_invalid(self, value):
        self.raw &= ~(0x01 << 15)
        self.raw |= (value << 15)

    def get_attitude_invalid(self):
        return (self.raw >> 14) & 0x01

    def set_attitude_invalid(self, value):
        self.raw &= ~(0x01 << 14)
        self.raw |= (value << 14)

    def get_baro_intertial_altitude_invalid(self):
        return (self.raw >> 13) & 0x01

    def set_baro_intertial_altitude_invalid(self, value):
        self.raw &= ~(0x01 << 13)
        self.raw |= (value << 13)

    def get_correct_baro_altitude_invalid(self):
        return (self.raw >> 12) & 0x01

    def set_correct_baro_altitude_invalid(self, value):
        self.raw &= ~(0x01 << 12)
        self.raw |= (value << 12)

    def get_radio_altimeter_invalid(self):
        return (self.raw >> 11) & 0x01

    def set_radio_altimeter_invalid(self, value):
        self.raw &= ~(0x01 << 11)
        self.raw |= (value << 11)

    def get_spoi_altitude_invalid(self):
        return (self.raw >> 10) & 0x01

    def set_spoi_altitude_invalid(self, value):
        self.raw &= ~(0x01 << 10)
        self.raw |= (value << 10)

    def get_spoi_position_invalid(self):
        return (self.raw >> 9) & 0x01

    def set_spoi_position_invalid(self, value):
        self.raw &= ~(0x01 << 9)
        self.raw |= (value << 9)

    def get_tas_invalid(self):
        return (self.raw >> 8) & 0x01

    def set_tas_invalid(self, value):
        self.raw &= ~(0x01 << 8)
        self.raw |= (value << 8)

    def get_cas_invalid(self):
        return (self.raw >> 7) & 0x01

    def set_cas_invalid(self, value):
        self.raw &= ~(0x01 << 7)
        self.raw |= (value << 7)

    def get_present_position_invalid(self):
        return (self.raw >> 6) & 0x01

    def set_present_position_invalid(self, value):
        self.raw &= ~(0x01 << 6)
        self.raw |= (value << 6)

    def get_ant_slew_valid(self):
        return (self.raw >> 5) & 0x01

    def set_ant_slew_valid(self, value):
        self.raw &= ~(0x01 << 5)
        self.raw |= (value << 5)

    def get_crs_rate_invalid(self):
        return (self.raw >> 4) & 0x01

    def set_crs_rate_invalid(self, value):
        self.raw &= ~(0x01 << 4)
        self.raw |= (value << 4)

    def get_crs_normal_slave(self):
        return (self.raw >> 3) & 0x01

    def set_crs_normal_slave(self, value):
        self.raw &= ~(0x01 << 3)
        self.raw |= (value << 3)

    def get_crs_zero_valid_field(self):
        return (self.raw >> 2) & 0x01

    def set_crs_zero_valid_field(self, value):
        self.raw &= ~(0x01 << 2)
        self.raw |= (value << 2)

    def get_crs_snowplough_valid_field(self):
        return (self.raw >> 1) & 0x01

    def set_crs_snowplough_valid_field(self, value):
        self.raw &= ~(0x01 << 1)
        self.raw |= (value << 1)