import ctypes
from .enums import RdrModes, DesControl, IbitRequest, StbySelection, SarExecSelection,SpotSelection, SilenceSelection, SarExecSelection,TransferFeatureBug
import Grifo_E_1553lib.data_types.enum_map as em

em.ENUM_MAP.update({
    "master_mode": RdrModes,
    "des_ctrl": DesControl,
    "ibit": IbitRequest,
    "sar_type": SarExecSelection,
    "stby" :StbySelection,
    "stop_powerup" : SpotSelection,
    "silence": SilenceSelection,
    "sar_type" : SarExecSelection,
    "transfer_feature_bug" : TransferFeatureBug
})


class _RdrModeCommandWordStr(ctypes.LittleEndianStructure):
    _pack_ = 1
    _fields_ = [
        ("spare", ctypes.c_uint16, 1),              
        ("transfer_feature_bug", ctypes.c_uint16, 1),    
        ("sar_type", ctypes.c_uint16, 1),  
        ("silence", ctypes.c_uint16, 1),       
        ("reserved11", ctypes.c_uint16, 1),         
        ("stop_powerup", ctypes.c_uint16, 1),       
        ("freeze", ctypes.c_uint16, 1),             
        ("stby", ctypes.c_uint16, 1),               
        ("ibit", ctypes.c_uint16, 1),               
        ("des_ctrl", ctypes.c_uint16, 3),           
        ("master_mode", ctypes.c_uint16, 4),        
        # ("sar_type", ctypes.c_uint16, 2),         
    ]

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

    # 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 DesControl((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 SarExecSelection((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)