"""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 std_msgs
from . import *
from .InteractiveMarkerControl import InteractiveMarkerControl
from .MenuEntry import MenuEntry


class InteractiveMarker(object):
    msg_name = "visualization_msgs.InteractiveMarker"

    __slots__ = [
        "menu_entries_length",
        "controls_length",
        "header",
        "pose",
        "name",
        "description",
        "scale",
        "menu_entries",
        "controls",
    ]

    __typenames__ = [
        "int32_t",
        "int32_t",
        "std_msgs.Header",
        "geometry_msgs.Pose",
        "string",
        "string",
        "float",
        "MenuEntry",
        "InteractiveMarkerControl",
    ]

    __dimensions__ = [
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        ["menu_entries_length"],
        ["controls_length"],
    ]

    menu_entries_length: "int32_t"
    controls_length: "int32_t"
    header: std_msgs.Header
    pose: geometry_msgs.Pose
    name: "string"
    description: "string"
    scale: "float"
    menu_entries: MenuEntry
    controls: InteractiveMarkerControl

    def __init__(
        self,
        menu_entries_length=0,
        controls_length=0,
        header=std_msgs.Header(),
        pose=geometry_msgs.Pose(),
        name="",
        description="",
        scale=0.0,
        menu_entries=[],
        controls=[],
    ):
        # LCM Type: int32_t
        self.menu_entries_length = menu_entries_length
        # LCM Type: int32_t
        self.controls_length = controls_length
        # LCM Type: std_msgs.Header
        self.header = header
        # LCM Type: geometry_msgs.Pose
        self.pose = pose
        # LCM Type: string
        self.name = name
        # LCM Type: string
        self.description = description
        # LCM Type: float
        self.scale = scale
        # LCM Type: MenuEntry[menu_entries_length]
        self.menu_entries = menu_entries
        # LCM Type: InteractiveMarkerControl[controls_length]
        self.controls = controls

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

    def _encode_one(self, buf):
        buf.write(struct.pack(">ii", self.menu_entries_length, self.controls_length))
        assert (
            self.header._get_packed_fingerprint()
            == std_msgs.Header._get_packed_fingerprint()
        )
        self.header._encode_one(buf)
        assert (
            self.pose._get_packed_fingerprint()
            == geometry_msgs.Pose._get_packed_fingerprint()
        )
        self.pose._encode_one(buf)
        __name_encoded = self.name.encode("utf-8")
        buf.write(struct.pack(">I", len(__name_encoded) + 1))
        buf.write(__name_encoded)
        buf.write(b"\0")
        __description_encoded = self.description.encode("utf-8")
        buf.write(struct.pack(">I", len(__description_encoded) + 1))
        buf.write(__description_encoded)
        buf.write(b"\0")
        buf.write(struct.pack(">f", self.scale))
        for i0 in range(self.menu_entries_length):
            assert (
                self.menu_entries[i0]._get_packed_fingerprint()
                == MenuEntry._get_packed_fingerprint()
            )
            self.menu_entries[i0]._encode_one(buf)
        for i0 in range(self.controls_length):
            assert (
                self.controls[i0]._get_packed_fingerprint()
                == InteractiveMarkerControl._get_packed_fingerprint()
            )
            self.controls[i0]._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.menu_entries_length, self.controls_length = struct.unpack(
            ">ii", buf.read(8)
        )
        self.header = cls._get_field_type("header")._decode_one(buf)
        self.pose = cls._get_field_type("pose")._decode_one(buf)
        __name_len = struct.unpack(">I", buf.read(4))[0]
        self.name = buf.read(__name_len)[:-1].decode("utf-8", "replace")
        __description_len = struct.unpack(">I", buf.read(4))[0]
        self.description = buf.read(__description_len)[:-1].decode("utf-8", "replace")
        self.scale = struct.unpack(">f", buf.read(4))[0]
        self.menu_entries = []
        for i0 in range(self.menu_entries_length):
            self.menu_entries.append(
                cls._get_field_type("menu_entries")._decode_one(buf)
            )
        self.controls = []
        for i0 in range(self.controls_length):
            self.controls.append(cls._get_field_type("controls")._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 = (
            0xE9ED8E5F9A72545F
            + std_msgs.Header._get_hash_recursive(newparents)
            + geometry_msgs.Pose._get_hash_recursive(newparents)
            + MenuEntry._get_hash_recursive(newparents)
            + InteractiveMarkerControl._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]
