import ctypes
from .enums import CrsNormalSlaveSelector, CrsSnowploughCommand
import Grifo_E_1553lib.data_types.enum_map as em

em.ENUM_MAP.update({
    "cursor_normal_slave_selector_tellback" :CrsNormalSlaveSelector,
    "cursor_snowplough_command_tellback" :CrsSnowploughCommand
})
class CursorXDisplayCoordAndQual(ctypes.LittleEndianStructure):
    _pack_ = 1
    _fields_ = [
        ("spare", ctypes.c_uint16, 3),
        ("reserved", ctypes.c_uint16, 1),
        ("cursor_snowplough_command_tellback", ctypes.c_uint16, 1),
        ("cursor_zero_tellback", ctypes.c_uint16, 1),
        ("cursor_normal_slave_selector_tellback", ctypes.c_uint16, 1),
        ("current_x_display_coord", ctypes.c_uint16, 9)
    ]
    
    def get_current_x_display_coord(self):
        """Extract the 9-bit X display coordinate"""
        return self.current_x_display_coord
    
    def get_cursor_snowplough_tellback(self):
        """Extract cursor snowplough command tellback bit"""
        return self.cursor_snowplough_command_tellback
    
    def get_cursor_zero_tellback(self):
        """Extract cursor zero tellback bit"""
        return self.cursor_zero_tellback
    
    def get_cursor_normal_slave_tellback(self):
        """Extract cursor normal/slave selector tellback bit"""
        return self.cursor_normal_slave_selector_tellback
    
    
class CursorYDisplayCoord(ctypes.LittleEndianStructure):
    _pack_ = 1
    _fields_ = [
        ("spare", ctypes.c_uint16, 7),
        ("current_y_display_coord", ctypes.c_uint16, 9)
    ]
    
    def get_current_y_display_coord(self):
        """Extract the 9-bit Y display coordinate"""
        return self.current_y_display_coord
    
class _CursorPositionLatitudeStr(ctypes.LittleEndianStructure):
    _pack_ = 1
    _fields_ = [
        ("latitude_lsw", ctypes.c_uint16),
        ("latitude_msw", ctypes.c_int16)
    ]
    
class CursorPositionLatitude(ctypes.Union):
    _pack_ = 1
    _fields_ = [
        ("crs_latitude", ctypes.c_uint16),
        ("str", _CursorPositionLatitudeStr)
    ]
    
    def get_value(self):
        """Return the 32-bit latitude value from LSW and MSW"""
        return (self.str.latitude_msw << 16) | self.str.latitude_lsw

class _CursorPositionLongitudeStr(ctypes.LittleEndianStructure):
    _pack_ = 1
    _fields_ = [
        ("longitude_lsw", ctypes.c_uint16),
        ("longitude_msw", ctypes.c_int16)
    ]
    
class CursorPositionLongitude(ctypes.Union):
    _pack_ = 1
    _fields_ = [
        ("crs_longitude", ctypes.c_uint16),
        ("str", _CursorPositionLongitudeStr)
    ]
    
    def get_value(self):
        """Return the 32-bit longitude value from LSW and MSW"""
        return (self.str.longitude_msw << 16) | self.str.longitude_lsw