"""LCM type definitions
This file automatically generated by lcm.
DO NOT MODIFY BY HAND!!!!
"""

from io import BytesIO
import struct
import sys


class PointField(object):
    msg_name = "sensor_msgs.PointField"

    __slots__ = ["name", "offset", "datatype", "count"]

    __typenames__ = ["string", "int32_t", "byte", "int32_t"]

    __dimensions__ = [None, None, None, None]

    name: "string"
    offset: "int32_t"
    datatype: "byte"
    count: "int32_t"

    INT8 = 1
    UINT8 = 2
    INT16 = 3
    UINT16 = 4
    INT32 = 5
    UINT32 = 6
    FLOAT32 = 7
    FLOAT64 = 8

    def __init__(self, name="", offset=0, datatype=0, count=0):
        # LCM Type: string
        self.name = name
        # LCM Type: int32_t
        self.offset = offset
        # LCM Type: byte
        self.datatype = datatype
        # LCM Type: int32_t
        self.count = count

    def lcm_encode(self):
        buf = BytesIO()
        buf.write(PointField._get_packed_fingerprint())
        self._encode_one(buf)
        return buf.getvalue()

    def _encode_one(self, buf):
        __name_encoded = self.name.encode("utf-8")
        buf.write(struct.pack(">I", len(__name_encoded) + 1))
        buf.write(__name_encoded)
        buf.write(b"\0")
        buf.write(struct.pack(">iBi", self.offset, self.datatype, self.count))

    @classmethod
    def lcm_decode(cls, data: bytes):
        if hasattr(data, "read"):
            buf = data
        else:
            buf = BytesIO(data)
        if buf.read(8) != cls._get_packed_fingerprint():
            raise ValueError("Decode error")
        return cls._decode_one(buf)

    @classmethod
    def _decode_one(cls, buf):
        self = cls()
        __name_len = struct.unpack(">I", buf.read(4))[0]
        self.name = buf.read(__name_len)[:-1].decode("utf-8", "replace")
        self.offset, self.datatype, self.count = struct.unpack(">iBi", buf.read(9))
        return self

    @classmethod
    def _get_field_type(cls, field_name):
        """Get the type for a field from annotations."""
        annotation = cls.__annotations__.get(field_name)
        if annotation is None:
            return None
        if isinstance(annotation, str):
            module = sys.modules[cls.__module__]
            if hasattr(module, annotation):
                return getattr(module, annotation)
            return None
        return annotation

    @classmethod
    def _get_hash_recursive(cls, parents):
        if cls in parents:
            return 0
        tmphash = (0x702E0E03F04285D7) & 0xFFFFFFFFFFFFFFFF
        tmphash = (
            ((tmphash << 1) & 0xFFFFFFFFFFFFFFFF) + (tmphash >> 63)
        ) & 0xFFFFFFFFFFFFFFFF
        return tmphash

    _packed_fingerprint = None

    @classmethod
    def _get_packed_fingerprint(cls):
        if cls._packed_fingerprint is None:
            cls._packed_fingerprint = struct.pack(">Q", cls._get_hash_recursive([]))
        return cls._packed_fingerprint

    def get_hash(self):
        """Get the LCM hash of the struct"""
        return struct.unpack(">Q", cls._get_packed_fingerprint())[0]
