import ctypes
from .enums import Zoom, SarMapOrientation


import Grifo_E_1553lib.data_types.enum_map as em

em.ENUM_MAP.update({
    "sar_map_orientation" : SarMapOrientation,
    "zoom_command" : Zoom
})

class _RdrFunAndParam2Str(ctypes.LittleEndianStructure):
    _pack_ = 1
    _fields_ = [
        ("spare", ctypes.c_uint16,8),
        ("sar_map_orientation", ctypes.c_uint16,2),
        ("zoom_command", ctypes.c_uint16,2),
        ("spare_2", ctypes.c_uint16,4),
        
    ]
    
    
class RdrFunAndParam2(ctypes.Union):
    _fields_ = [
        ("raw", ctypes.c_uint16),
        ("str", _RdrFunAndParam2Str)
    ]

    # Bitfield accessors for spare_0_4
    def get_spare_0_4(self):
        return (self.raw >> 12) & 0x0F

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

    # Bitfield accessors for zoom
    def get_zoom(self):
        return Zoom((self.raw >> 9) & 0x03)

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

    # Bitfield accessors for sar_map_orientation
    def get_sar_map_orientation(self):
        return SarMapOrientation((self.raw >> 7) & 0x03)

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

    # Bitfield accessors for spare_8_15
    def get_spare_8_15(self):
        return (self.raw >> 0) & 0xFF

    def set_spare_8_15(self, value):
        self.raw &= ~(0xFF << 0)
        self.raw |= (value << 0)