"""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


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

    __slots__ = [
        "points_length",
        "outline_colors_length",
        "header",
        "ns",
        "id",
        "type",
        "action",
        "position",
        "scale",
        "outline_color",
        "filled",
        "fill_color",
        "lifetime",
        "points",
        "outline_colors",
    ]

    __typenames__ = [
        "int32_t",
        "int32_t",
        "std_msgs.Header",
        "string",
        "int32_t",
        "int32_t",
        "int32_t",
        "geometry_msgs.Point",
        "float",
        "std_msgs.ColorRGBA",
        "byte",
        "std_msgs.ColorRGBA",
        "std_msgs.Duration",
        "geometry_msgs.Point",
        "std_msgs.ColorRGBA",
    ]

    __dimensions__ = [
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        ["points_length"],
        ["outline_colors_length"],
    ]

    points_length: "int32_t"
    outline_colors_length: "int32_t"
    header: std_msgs.Header
    ns: "string"
    id: "int32_t"
    type: "int32_t"
    action: "int32_t"
    position: geometry_msgs.Point
    scale: "float"
    outline_color: std_msgs.ColorRGBA
    filled: "byte"
    fill_color: std_msgs.ColorRGBA
    lifetime: std_msgs.Duration
    points: geometry_msgs.Point
    outline_colors: std_msgs.ColorRGBA

    CIRCLE = 0
    LINE_STRIP = 1
    LINE_LIST = 2
    POLYGON = 3
    POINTS = 4
    ADD = 0
    REMOVE = 1

    def __init__(
        self,
        points_length=0,
        outline_colors_length=0,
        header=std_msgs.Header(),
        ns="",
        id=0,
        type=0,
        action=0,
        position=geometry_msgs.Point(),
        scale=0.0,
        outline_color=std_msgs.ColorRGBA(),
        filled=0,
        fill_color=std_msgs.ColorRGBA(),
        lifetime=std_msgs.Duration(),
        points=[],
        outline_colors=[],
    ):
        # LCM Type: int32_t
        self.points_length = points_length
        # LCM Type: int32_t
        self.outline_colors_length = outline_colors_length
        # LCM Type: std_msgs.Header
        self.header = header
        # LCM Type: string
        self.ns = ns
        # LCM Type: int32_t
        self.id = id
        # LCM Type: int32_t
        self.type = type
        # LCM Type: int32_t
        self.action = action
        # LCM Type: geometry_msgs.Point
        self.position = position
        # LCM Type: float
        self.scale = scale
        # LCM Type: std_msgs.ColorRGBA
        self.outline_color = outline_color
        # LCM Type: byte
        self.filled = filled
        # LCM Type: std_msgs.ColorRGBA
        self.fill_color = fill_color
        # LCM Type: std_msgs.Duration
        self.lifetime = lifetime
        # LCM Type: geometry_msgs.Point[points_length]
        self.points = points
        # LCM Type: std_msgs.ColorRGBA[outline_colors_length]
        self.outline_colors = outline_colors

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

    def _encode_one(self, buf):
        buf.write(struct.pack(">ii", self.points_length, self.outline_colors_length))
        assert (
            self.header._get_packed_fingerprint()
            == std_msgs.Header._get_packed_fingerprint()
        )
        self.header._encode_one(buf)
        __ns_encoded = self.ns.encode("utf-8")
        buf.write(struct.pack(">I", len(__ns_encoded) + 1))
        buf.write(__ns_encoded)
        buf.write(b"\0")
        buf.write(struct.pack(">iii", self.id, self.type, self.action))
        assert (
            self.position._get_packed_fingerprint()
            == geometry_msgs.Point._get_packed_fingerprint()
        )
        self.position._encode_one(buf)
        buf.write(struct.pack(">f", self.scale))
        assert (
            self.outline_color._get_packed_fingerprint()
            == std_msgs.ColorRGBA._get_packed_fingerprint()
        )
        self.outline_color._encode_one(buf)
        buf.write(struct.pack(">B", self.filled))
        assert (
            self.fill_color._get_packed_fingerprint()
            == std_msgs.ColorRGBA._get_packed_fingerprint()
        )
        self.fill_color._encode_one(buf)
        assert (
            self.lifetime._get_packed_fingerprint()
            == std_msgs.Duration._get_packed_fingerprint()
        )
        self.lifetime._encode_one(buf)
        for i0 in range(self.points_length):
            assert (
                self.points[i0]._get_packed_fingerprint()
                == geometry_msgs.Point._get_packed_fingerprint()
            )
            self.points[i0]._encode_one(buf)
        for i0 in range(self.outline_colors_length):
            assert (
                self.outline_colors[i0]._get_packed_fingerprint()
                == std_msgs.ColorRGBA._get_packed_fingerprint()
            )
            self.outline_colors[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.points_length, self.outline_colors_length = struct.unpack(
            ">ii", buf.read(8)
        )
        self.header = cls._get_field_type("header")._decode_one(buf)
        __ns_len = struct.unpack(">I", buf.read(4))[0]
        self.ns = buf.read(__ns_len)[:-1].decode("utf-8", "replace")
        self.id, self.type, self.action = struct.unpack(">iii", buf.read(12))
        self.position = cls._get_field_type("position")._decode_one(buf)
        self.scale = struct.unpack(">f", buf.read(4))[0]
        self.outline_color = cls._get_field_type("outline_color")._decode_one(buf)
        self.filled = struct.unpack(">B", buf.read(1))[0]
        self.fill_color = cls._get_field_type("fill_color")._decode_one(buf)
        self.lifetime = cls._get_field_type("lifetime")._decode_one(buf)
        self.points = []
        for i0 in range(self.points_length):
            self.points.append(cls._get_field_type("points")._decode_one(buf))
        self.outline_colors = []
        for i0 in range(self.outline_colors_length):
            self.outline_colors.append(
                cls._get_field_type("outline_colors")._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 = (
            0x3A3EA371B474D924
            + std_msgs.Header._get_hash_recursive(newparents)
            + geometry_msgs.Point._get_hash_recursive(newparents)
            + std_msgs.ColorRGBA._get_hash_recursive(newparents)
            + std_msgs.ColorRGBA._get_hash_recursive(newparents)
            + std_msgs.Duration._get_hash_recursive(newparents)
            + geometry_msgs.Point._get_hash_recursive(newparents)
            + std_msgs.ColorRGBA._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]
