import ctypes
from .enums import RdrModes, DesignationStatus, IbitRequest, StbySelection, LastAcquisitionResult, TransitionStatus, RFRadiationStatus, DegradedPerformanceStatus
import Grifo_E_1553lib.data_types.enum_map as em

em.ENUM_MAP.update({
    "master_mode_tellback": RdrModes,
    "radar_designation_status": DesignationStatus,
    "int_bit_status": IbitRequest,
    "degraded_performance_status": DegradedPerformanceStatus,
    "stby_tellback" :StbySelection,
    "rf_radiation_status": RFRadiationStatus,
    "transition_status" : TransitionStatus,
    "last_acquisition_result" : LastAcquisitionResult
})


class _RdrStatusTellbackStr(ctypes.LittleEndianStructure):
    _pack_ = 1
    _fields_ = [
 		("reserved15", ctypes.c_uint16, 1),
        ("last_acquisition_result", ctypes.c_uint16, 1),
        ("transition_status", ctypes.c_uint16, 1),
        ("rf_radiation_status", ctypes.c_uint16, 1),
        ("reserved11", ctypes.c_uint16, 1),
        ("degraded_performance_status", ctypes.c_uint16, 1),
        ("reserved9", ctypes.c_uint16, 1),
        ("stby_tellback", ctypes.c_uint16, 1),
        ("int_bit_status", ctypes.c_uint16, 1),
        ("radar_designation_status", ctypes.c_uint16, 3),
        ("master_mode_tellback", ctypes.c_uint16, 4)       
    ]

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

    # Bitfield accessors for master_mode
    def get_master_mode(self):
        return RdrModes((self.raw >> (15-3)) & 0x0F)

    def set_master_mode(self, value):
        self.raw &= ~(0x0F << (15-3))
        self.raw |= (value << (15-3))

    # Bitfield accessors for des_ctrl
    def get_des_ctrl(self):
        return DesignationStatus((self.raw >> (15-6)) & 0x07)

    def set_des_ctrl(self, value):
        self.raw &= ~(0x07 << (15-6))
        self.raw |= (value << (15-6))

    # Bitfield accessors for ibit
    def get_ibit(self):
        return IbitRequest((self.raw >> (15-7)) & 0x01)

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

    # Bitfield accessors for stby
    def get_stby(self):
        return (self.raw >> (15-8)) & 0x01

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

    # Bitfield accessors for freeze
    def get_freeze(self):
        return (self.raw >> (15-9)) & 0x01

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

    # Bitfield accessors for stop_powerup
    def get_stop_powerup(self):
        return (self.raw >> (15-10)) & 0x01

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

    # Bitfield accessors for reserved11
    def get_reserved11(self):
        return (self.raw >> (15-11)) & 0x01

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

    # Bitfield accessors for rf_radiation
    def get_rf_radiation(self):
        return (self.raw >> (15-12)) & 0x01

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

    # Bitfield accessors for transition_status
    def get_transition_status(self):
        return (self.raw >> (15-13)) & 0x01

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

    # Bitfield accessors for last_acq_result
    def get_last_acq_result(self):
        return (self.raw >> (15-14)) & 0x01

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

    # Bitfield accessors for sar_type
    def get_sar_type(self):
        return TransitionStatus((self.raw >> 0) & 0x03)

    def set_sar_type(self, value):
        self.raw &= ~(0x03 << 0)
        self.raw |= (value << 0)

    # Bitfield accessors for spare
    def get_spare(self):
        return (self.raw >> 14) & 0x03

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