import ctypes
from .enums import CrsGhostSelector, CrsMttSelector, CrsSarSelector
import Grifo_E_1553lib.data_types.enum_map as em
em.ENUM_MAP.update({
    "sar_enabled_selector" : CrsSarSelector,
    "mtt_enabled_selector" : CrsMttSelector,
    "normal_ghost_selector" : CrsGhostSelector,
    
})


class _CrsMotionXStr(ctypes.LittleEndianStructure):
    _pack_ = 1
    _fields_ = [
        ("spare", ctypes.c_uint16,4),
        ("sar_enabled_selector", ctypes.c_uint16,1),
        ("mtt_enabled_selector", ctypes.c_uint16,1),
        ("normal_ghost_selector", ctypes.c_uint16,1),
        ("rate_of_motion_x_display", ctypes.c_int16,9),
    ]

class CrsMotionX(ctypes.Union):
    _pack_ = 1
    _fields_ = [
        ("raw", ctypes.c_uint16),
        ("str", _CrsMotionXStr)
    ]

    def get_rate(self):
        return (self.raw >> 7) & 0x1FF

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

    def get_ghost(self):
        return CrsGhostSelector((self.raw >> 6) & 0x01)

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

    def get_mtt(self):
        return CrsMttSelector((self.raw >> 5) & 0x01)

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

    def get_sar(self):
        return CrsSarSelector((self.raw >> 4) & 0x01)

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

    def get_abs_pos(self):
        return (self.raw >> 0) & 0x01

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

    def get_abs_pos_rate(self):
        return (self.raw >> 7) & 0x1FF

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