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

from io import BytesIO
import struct
import sys


class SolidPrimitive(object):
    msg_name = "shape_msgs.SolidPrimitive"

    __slots__ = ["dimensions_length", "type", "dimensions"]

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

    __dimensions__ = [None, None, ["dimensions_length"]]

    dimensions_length: "int32_t"
    type: "byte"
    dimensions: "double"

    BOX = 1
    SPHERE = 2
    CYLINDER = 3
    CONE = 4
    BOX_X = 0
    BOX_Y = 1
    BOX_Z = 2
    SPHERE_RADIUS = 0
    CYLINDER_HEIGHT = 0
    CYLINDER_RADIUS = 1
    CONE_HEIGHT = 0
    CONE_RADIUS = 1

    def __init__(self, dimensions_length=0, type=0, dimensions=[]):
        # LCM Type: int32_t
        self.dimensions_length = dimensions_length
        # LCM Type: byte
        self.type = type
        # LCM Type: double[dimensions_length]
        self.dimensions = dimensions

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

    def _encode_one(self, buf):
        buf.write(struct.pack(">iB", self.dimensions_length, self.type))
        buf.write(
            struct.pack(
                ">%dd" % self.dimensions_length,
                *self.dimensions[: self.dimensions_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.dimensions_length, self.type = struct.unpack(">iB", buf.read(5))
        self.dimensions = struct.unpack(
            ">%dd" % self.dimensions_length, buf.read(self.dimensions_length * 8)
        )
        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 = (0xB8A8E7178CAB07AC) & 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]
