import ctypes
from Grifo_E_1553lib.data_types.enums import WaypointFytSelection, WaypointQualifierValue
import Grifo_E_1553lib.data_types.enum_map as em

em.ENUM_MAP.update({
    # "bulls_eye_identifier" : BullsEyeIdentifier
    "waypoint_qualifier_value" : WaypointQualifierValue,
    "waypoint_fyt_selector" : WaypointFytSelection
})

class _LatLonUnionStr(ctypes.LittleEndianStructure):
    _pack_ = 1
    _fields_= [
        ("latlonlow", ctypes.c_uint16, 9),
        ("latlonhigh", ctypes.c_uint16, 16),
    ]

class LatLonUnion(ctypes.Union):
    _pack_ = 1
    _fields_ = [
        ("latitude", ctypes.c_uint32,25),
        ("longitude", ctypes.c_uint32,25),
        ("str", _LatLonUnionStr)
    ]
    
print("SIZEOFFFFFFFF")
print(ctypes.sizeof(LatLonUnion))

class WaypointLatSelQual(ctypes.LittleEndianStructure):
    _pack_ = 1
    _fields_ = [
        ("spare", ctypes.c_uint16, 3),              
        ("waypoint_qualifier_value", ctypes.c_uint16, 2),    
        ("waypoint_qualifier_validity", ctypes.c_uint16, 1),  
        ("waypoint_fyt_selector", ctypes.c_uint16, 1),       
        ("waypoint_1_latitude_lsw", ctypes.c_uint16, 9),        
        ("waypoint_1_latitude_msw", ctypes.c_int16, 16)  
        # ("waypoint_1_latitude", LatLonUnion)  
        # ("waypoint_1_latitude", ctypes.c_uint32, 25)  
              
    ]

# class WaypointLatSelQual(ctypes.Union):
#     _pack_ = 1
#     _fields_ = [
#         ("raw", ctypes.c_uint32),  # Rappresenta i due icd_raw_word_t
#         ("str",_WaypointLatSelQualStr)
#     ]

    # Bitfield accessors for waypoint_fyt_selection_field
    def get_waypoint_fyt_selection_field(self):
        return WaypointFytSelection((self.raw[0] >> 7) & 0x01)

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

    # Bitfield accessors for waypoint_qualifier_validity
    def get_waypoint_qualifier_validity(self):
        return (self.raw[0] >> 6) & 0x01

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

    # Bitfield accessors for waypoint_qualifier_value_field
    def get_waypoint_qualifier_value_field(self):
        return WaypointQualifierValue((self.raw[0] >> 3) & 0x03)

    def set_waypoint_qualifier_value_field(self, value):
        self.raw[0] &= ~(0x03 << 3)
        self.raw[0] |= (value << 3)

    # Bitfield accessors for spare
    def get_spare(self):
        return (self.raw[1] >> 0) & 0x07

    def set_spare(self, value):
        self.raw[1] &= ~(0x07 << 0)
        self.raw[1] |= (value << 0)