"""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 .Color import Color


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

    __slots__ = [
        "pose",
        "shaft_length",
        "shaft_diameter",
        "head_length",
        "head_diameter",
        "color",
    ]

    __typenames__ = [
        "geometry_msgs.Pose",
        "double",
        "double",
        "double",
        "double",
        "Color",
    ]

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

    pose: geometry_msgs.Pose
    shaft_length: "double"
    shaft_diameter: "double"
    head_length: "double"
    head_diameter: "double"
    color: Color

    def __init__(
        self,
        pose=geometry_msgs.Pose(),
        shaft_length=0.0,
        shaft_diameter=0.0,
        head_length=0.0,
        head_diameter=0.0,
        color=Color(),
    ):
        # LCM Type: geometry_msgs.Pose
        self.pose = pose
        # LCM Type: double
        self.shaft_length = shaft_length
        # LCM Type: double
        self.shaft_diameter = shaft_diameter
        # LCM Type: double
        self.head_length = head_length
        # LCM Type: double
        self.head_diameter = head_diameter
        # LCM Type: Color
        self.color = color

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

    def _encode_one(self, buf):
        assert (
            self.pose._get_packed_fingerprint()
            == geometry_msgs.Pose._get_packed_fingerprint()
        )
        self.pose._encode_one(buf)
        buf.write(
            struct.pack(
                ">dddd",
                self.shaft_length,
                self.shaft_diameter,
                self.head_length,
                self.head_diameter,
            )
        )
        assert self.color._get_packed_fingerprint() == Color._get_packed_fingerprint()
        self.color._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.pose = cls._get_field_type("pose")._decode_one(buf)
        self.shaft_length, self.shaft_diameter, self.head_length, self.head_diameter = (
            struct.unpack(">dddd", buf.read(32))
        )
        self.color = cls._get_field_type("color")._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 = (
            0xB1F9D32968E60FBB
            + geometry_msgs.Pose._get_hash_recursive(newparents)
            + Color._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]
