"""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 . import *
from lcm_msgs import builtin_interfaces
from .PackedElementField import PackedElementField


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

    __slots__ = [
        "fields_length",
        "data_length",
        "timestamp",
        "frame_id",
        "pose",
        "point_stride",
        "fields",
        "data",
    ]

    __typenames__ = [
        "int32_t",
        "int32_t",
        "builtin_interfaces.Time",
        "string",
        "geometry_msgs.Pose",
        "int32_t",
        "PackedElementField",
        "byte",
    ]

    __dimensions__ = [
        None,
        None,
        None,
        None,
        None,
        None,
        ["fields_length"],
        ["data_length"],
    ]

    fields_length: "int32_t"
    data_length: "int32_t"
    timestamp: builtin_interfaces.Time
    frame_id: "string"
    pose: geometry_msgs.Pose
    point_stride: "int32_t"
    fields: PackedElementField
    data: "byte"

    def __init__(
        self,
        fields_length=0,
        data_length=0,
        timestamp=builtin_interfaces.Time(),
        frame_id="",
        pose=geometry_msgs.Pose(),
        point_stride=0,
        fields=[],
        data=b"",
    ):
        # LCM Type: int32_t
        self.fields_length = fields_length
        # LCM Type: int32_t
        self.data_length = data_length
        # LCM Type: builtin_interfaces.Time
        self.timestamp = timestamp
        # LCM Type: string
        self.frame_id = frame_id
        # LCM Type: geometry_msgs.Pose
        self.pose = pose
        # LCM Type: int32_t
        self.point_stride = point_stride
        # LCM Type: PackedElementField[fields_length]
        self.fields = fields
        # LCM Type: byte[data_length]
        self.data = data

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

    def _encode_one(self, buf):
        buf.write(struct.pack(">ii", self.fields_length, self.data_length))
        assert (
            self.timestamp._get_packed_fingerprint()
            == builtin_interfaces.Time._get_packed_fingerprint()
        )
        self.timestamp._encode_one(buf)
        __frame_id_encoded = self.frame_id.encode("utf-8")
        buf.write(struct.pack(">I", len(__frame_id_encoded) + 1))
        buf.write(__frame_id_encoded)
        buf.write(b"\0")
        assert (
            self.pose._get_packed_fingerprint()
            == geometry_msgs.Pose._get_packed_fingerprint()
        )
        self.pose._encode_one(buf)
        buf.write(struct.pack(">i", self.point_stride))
        for i0 in range(self.fields_length):
            assert (
                self.fields[i0]._get_packed_fingerprint()
                == PackedElementField._get_packed_fingerprint()
            )
            self.fields[i0]._encode_one(buf)
        buf.write(bytearray(self.data[: self.data_length]))

    @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.fields_length, self.data_length = struct.unpack(">ii", buf.read(8))
        self.timestamp = cls._get_field_type("timestamp")._decode_one(buf)
        __frame_id_len = struct.unpack(">I", buf.read(4))[0]
        self.frame_id = buf.read(__frame_id_len)[:-1].decode("utf-8", "replace")
        self.pose = cls._get_field_type("pose")._decode_one(buf)
        self.point_stride = struct.unpack(">i", buf.read(4))[0]
        self.fields = []
        for i0 in range(self.fields_length):
            self.fields.append(cls._get_field_type("fields")._decode_one(buf))
        self.data = buf.read(self.data_length)
        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 = (
            0x4F3713AA9B8DC878
            + builtin_interfaces.Time._get_hash_recursive(newparents)
            + geometry_msgs.Pose._get_hash_recursive(newparents)
            + PackedElementField._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]
