import ctypes
import Grifo_E_1553lib.data_types.enum_map as em
# Constants for waypoint code range
ICD1553_WAYPOINT_SYMBOL_MIN = 0
ICD1553_WAYPOINT_SYMBOL_MAX = 99

class WaypointLonCode(ctypes.LittleEndianStructure):
    _pack_ = 1
    _fields_ = [
        ("waypoint_code", ctypes.c_uint16, 7),
        ("waypoint_1_longitude_lsw", ctypes.c_uint16, 9),        
        ("waypoint_1_longitude_msw", ctypes.c_uint16, 16),        
    ]

# class WaypointLonCode(ctypes.Union):
#     _pack_ = 1
#     _fields_ = [
#         ("raw", ctypes.c_uint32),
#         ("str",_WaypointLonCodeStr )
#     ]

    # Bitfield accessors for waypoint_code
    def get_waypoint_code(self):
        return (self.raw[1] >> 9) & 0x7F

    def set_waypoint_code(self, value):
        if not ICD1553_WAYPOINT_SYMBOL_MIN <= value <= ICD1553_WAYPOINT_SYMBOL_MAX:
            raise ValueError(f"Waypoint code value must be between {ICD1553_WAYPOINT_SYMBOL_MIN} and {ICD1553_WAYPOINT_SYMBOL_MAX}")
        self.raw[1] &= ~(0x7F << 9)
        self.raw[1] |= (value << 9)