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

from io import BytesIO
import struct
import sys


class TF2Error(object):
    msg_name = "tf2_msgs.TF2Error"

    __slots__ = ["error", "error_string"]

    __typenames__ = ["byte", "string"]

    __dimensions__ = [None, None]

    error: "byte"
    error_string: "string"

    NO_ERROR = 0
    LOOKUP_ERROR = 1
    CONNECTIVITY_ERROR = 2
    EXTRAPOLATION_ERROR = 3
    INVALID_ARGUMENT_ERROR = 4
    TIMEOUT_ERROR = 5
    TRANSFORM_ERROR = 6

    def __init__(self, error=0, error_string=""):
        # LCM Type: byte
        self.error = error
        # LCM Type: string
        self.error_string = error_string

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

    def _encode_one(self, buf):
        buf.write(struct.pack(">B", self.error))
        __error_string_encoded = self.error_string.encode("utf-8")
        buf.write(struct.pack(">I", len(__error_string_encoded) + 1))
        buf.write(__error_string_encoded)
        buf.write(b"\0")

    @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()
        self.error = struct.unpack(">B", buf.read(1))[0]
        __error_string_len = struct.unpack(">I", buf.read(4))[0]
        self.error_string = buf.read(__error_string_len)[:-1].decode("utf-8", "replace")
        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 = (0x1E8FF0D80D02EE55) & 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]
