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

from io import BytesIO
import struct
import sys

from lcm_msgs import geometry_msgs
from lcm_msgs import builtin_interfaces


class FrameTransform(object):
    msg_name = "foxglove_msgs.FrameTransform"

    __slots__ = [
        "timestamp",
        "parent_frame_id",
        "child_frame_id",
        "translation",
        "rotation",
    ]

    __typenames__ = [
        "builtin_interfaces.Time",
        "string",
        "string",
        "geometry_msgs.Vector3",
        "geometry_msgs.Quaternion",
    ]

    __dimensions__ = [None, None, None, None, None]

    timestamp: builtin_interfaces.Time
    parent_frame_id: "string"
    child_frame_id: "string"
    translation: geometry_msgs.Vector3
    rotation: geometry_msgs.Quaternion

    def __init__(
        self,
        timestamp=builtin_interfaces.Time(),
        parent_frame_id="",
        child_frame_id="",
        translation=geometry_msgs.Vector3(),
        rotation=geometry_msgs.Quaternion(),
    ):
        # LCM Type: builtin_interfaces.Time
        self.timestamp = timestamp
        # LCM Type: string
        self.parent_frame_id = parent_frame_id
        # LCM Type: string
        self.child_frame_id = child_frame_id
        # LCM Type: geometry_msgs.Vector3
        self.translation = translation
        # LCM Type: geometry_msgs.Quaternion
        self.rotation = rotation

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

    def _encode_one(self, buf):
        assert (
            self.timestamp._get_packed_fingerprint()
            == builtin_interfaces.Time._get_packed_fingerprint()
        )
        self.timestamp._encode_one(buf)
        __parent_frame_id_encoded = self.parent_frame_id.encode("utf-8")
        buf.write(struct.pack(">I", len(__parent_frame_id_encoded) + 1))
        buf.write(__parent_frame_id_encoded)
        buf.write(b"\0")
        __child_frame_id_encoded = self.child_frame_id.encode("utf-8")
        buf.write(struct.pack(">I", len(__child_frame_id_encoded) + 1))
        buf.write(__child_frame_id_encoded)
        buf.write(b"\0")
        assert (
            self.translation._get_packed_fingerprint()
            == geometry_msgs.Vector3._get_packed_fingerprint()
        )
        self.translation._encode_one(buf)
        assert (
            self.rotation._get_packed_fingerprint()
            == geometry_msgs.Quaternion._get_packed_fingerprint()
        )
        self.rotation._encode_one(buf)

    @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.timestamp = cls._get_field_type("timestamp")._decode_one(buf)
        __parent_frame_id_len = struct.unpack(">I", buf.read(4))[0]
        self.parent_frame_id = buf.read(__parent_frame_id_len)[:-1].decode(
            "utf-8", "replace"
        )
        __child_frame_id_len = struct.unpack(">I", buf.read(4))[0]
        self.child_frame_id = buf.read(__child_frame_id_len)[:-1].decode(
            "utf-8", "replace"
        )
        self.translation = cls._get_field_type("translation")._decode_one(buf)
        self.rotation = cls._get_field_type("rotation")._decode_one(buf)
        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
        newparents = parents + [cls]
        tmphash = (
            0x16CB223D6329D1F0
            + builtin_interfaces.Time._get_hash_recursive(newparents)
            + geometry_msgs.Vector3._get_hash_recursive(newparents)
            + geometry_msgs.Quaternion._get_hash_recursive(newparents)
        ) & 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]
