import ctypes
from .enums import (
    TargetHistory, GroundTargetRejectVel, MinDetectGroundVel, ALEBlanking,
    AltitudeBlock, PRFLookup
)
import Grifo_E_1553lib.data_types.enum_map as em

em.ENUM_MAP.update({
    "lprf_mprf_lookup_selection" : PRFLookup,
    "altitude_block_selection" : AltitudeBlock,
    "ale_blanking_enable_disable" : ALEBlanking,
    "min_detectable_ground_target_radial_velocity" : MinDetectGroundVel,
    "ground_target_reject_radial_velocity" : GroundTargetRejectVel,
    "target_history" : TargetHistory
})

class _RDROperationalSettingsStr(ctypes.LittleEndianStructure):
    _pack_ = 1
    _fields_ = [
        ("spare", ctypes.c_uint16, 1),              
        ("lprf_mprf_lookup_selection", ctypes.c_uint16, 1),         
        ("altitude_block_selection", ctypes.c_uint16, 2),       
        ("ale_blanking_enable_disable", ctypes.c_uint16, 1),             
        ("min_detectable_ground_target_radial_velocity", ctypes.c_uint16, 1),               
        ("ground_target_reject_radial_velocity", ctypes.c_uint16, 1),               
        ("rdr_symbology_intensity", ctypes.c_uint16, 7),           
        ("target_history", ctypes.c_uint16, 2),        
    ]

class RDROperationalSettings(ctypes.Union):
    _fields_ = [
        ("raw", ctypes.c_uint16),
        ("str", _RDROperationalSettingsStr)
    ]

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

    def set_history_level(self, value):
        self.raw &= ~(0x03 << 14)  # Clear existing bits
        self.raw |= (value << 14)   # Set new bits

    # Bitfield accessors for sym_intensity
    def get_sym_intensity(self):
        return (self.raw >> 8) & 0x7F

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

    # Bitfield accessors for ground_reject_vel_high
    def get_ground_reject_vel_high(self):
        return (self.raw >> 7) & 0x01

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

    # Bitfield accessors for min_detect_ground_radial_vel_high
    def get_min_detect_ground_radial_vel_high(self):
        return (self.raw >> 6) & 0x01

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

    # Bitfield accessors for ale_blanking_disable
    def get_ale_blanking_disable(self):
        return (self.raw >> 5) & 0x01

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

    # Bitfield accessors for altitude_block
    def get_altitude_block(self):
        return (self.raw >> 2) & 0x03

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

    # Bitfield accessors for prf_lookup_selection
    def get_prf_lookup_selection(self):
        return (self.raw >> 1) & 0x01

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

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

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