import ctypes
from .enums import FrequencyGrouping
import Grifo_E_1553lib.data_types.enum_map as em

em.ENUM_MAP.update({
    "frequency_grouping_option_selection" : FrequencyGrouping
})

class _RFChannelsGroupingOptionsStr(ctypes.LittleEndianStructure):
    _pack_ = 1
    _fields_ = [
        ("spare", ctypes.c_uint16,1),
        ("frequency_grouping_option_selection", ctypes.c_uint16,1),
        ("reserved_mti_threshold", ctypes.c_uint16,7),
        ("reserved_if_gain", ctypes.c_uint16,7)
    ]
    
class RFChannelsGroupingOptions(ctypes.Union):
    _pack_ = 1
    _fields_ = [
        ("raw", ctypes.c_uint16),
        ("str", _RFChannelsGroupingOptionsStr)
    ]

    # Bitfield accessors for if_gain
    def get_if_gain(self):
        return (self.raw >> 9) & 0x7F

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

    # Bitfield accessors for moving_target_gain
    def get_moving_target_gain(self):
        return (self.raw >> 2) & 0x7F

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

    # Bitfield accessors for frequency_grouping_field
    def get_frequency_grouping_field(self):
        return FrequencyGrouping((self.raw >> 1) & 0x01)

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

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

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