import ctypes
from .enums import TwsTargetValidity, TwsTargetWhich
import Grifo_E_1553lib.data_types.enum_map as em
em.ENUM_MAP.update({
    "track_01_validity" : TwsTargetValidity,
    "track_02_validity" : TwsTargetValidity,
    "track_03_validity" : TwsTargetValidity,
    "track_04_validity" : TwsTargetValidity,
    "track_05_validity" : TwsTargetValidity,
    "track_06_validity" : TwsTargetValidity,
    "track_07_validity" : TwsTargetValidity,
    "track_08_validity" : TwsTargetValidity,
    "track_08_validity" : TwsTargetValidity,
    "track_10_validity" : TwsTargetValidity,
    "hpt_selector" : TwsTargetWhich  
})

class _ValitidyWordAndHptIdentifierStr(ctypes.LittleEndianStructure):
    _pack_ = 1
    _fields_ = [
        ("spare", ctypes.c_uint16, 2),
        ("hpt_selector", ctypes.c_uint16, 4),
        ("track_10_validity", ctypes.c_uint16, 1),
        ("track_08_validity", ctypes.c_uint16, 1),
        ("track_08_validity", ctypes.c_uint16, 1),
        ("track_07_validity", ctypes.c_uint16, 1),
        ("track_06_validity", ctypes.c_uint16, 1),
        ("track_05_validity", ctypes.c_uint16, 1),
        ("track_04_validity", ctypes.c_uint16, 1),
        ("track_03_validity", ctypes.c_uint16, 1),
        ("track_02_validity", ctypes.c_uint16, 1),
        ("track_01_validity", ctypes.c_uint16, 1),
    ]

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

    def get_track_01_validity(self):
        return TwsTargetValidity((self.raw >> 15) & 0x01)
    def set_track_01_validity(self, value):
        self.raw &= ~(0x01 << 15)
        self.raw |= (value << 15)

    def get_track_02_validity(self):
        return TwsTargetValidity((self.raw >> 14) & 0x01)
    def set_track_02_validity(self, value):
        self.raw &= ~(0x01 << 14)
        self.raw |= (value << 14)

    def get_track_03_validity(self):
        return TwsTargetValidity((self.raw >> 13) & 0x01)
    def set_track_03_validity(self, value):
        self.raw &= ~(0x01 << 13)
        self.raw |= (value << 13)

    def get_track_04_validity(self):
        return TwsTargetValidity((self.raw >> 12) & 0x01)
    def set_track_04_validity(self, value):
        self.raw &= ~(0x01 << 12)
        self.raw |= (value << 12)

    def get_track_05_validity(self):
        return TwsTargetValidity((self.raw >> 11) & 0x01)
    def set_track_05_validity(self, value):
        self.raw &= ~(0x01 << 11)
        self.raw |= (value << 11)

    def get_track_06_validity(self):
        return TwsTargetValidity((self.raw >> 10) & 0x01)
    def set_track_06_validity(self, value):
        self.raw &= ~(0x01 << 10)
        self.raw |= (value << 10)

    def get_track_07_validity(self):
        return TwsTargetValidity((self.raw >> 9) & 0x01)
    def set_track_07_validity(self, value):
        self.raw &= ~(0x01 << 9)
        self.raw |= (value << 9)

    def get_track_08_validity(self):
        return TwsTargetValidity((self.raw >> 8) & 0x01)
    def set_track_08_validity(self, value):
        self.raw &= ~(0x01 << 8)
        self.raw |= (value << 8)

    def get_tws_target_which_field(self):
        return TwsTargetWhich((self.raw >> 4) & 0x0F)
    def set_tws_target_which_field(self, value):
        self.raw &= ~(0x0F << 4)
        self.raw |= (value << 4)

    def get_spare(self):
        return (self.raw >> 0) & 0x0F
    def set_spare(self, value):
        self.raw &= ~(0x0F << 0)
        self.raw |= (value << 0)