"""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 LinePrimitive(object):
    msg_name = "foxglove_msgs.LinePrimitive"

    __slots__ = [
        "points_length",
        "colors_length",
        "indices_length",
        "type",
        "pose",
        "thickness",
        "scale_invariant",
        "points",
        "color",
        "colors",
        "indices",
    ]

    __typenames__ = [
        "int32_t",
        "int32_t",
        "int32_t",
        "byte",
        "geometry_msgs.Pose",
        "double",
        "boolean",
        "geometry_msgs.Point",
        "Color",
        "Color",
        "int32_t",
    ]

    __dimensions__ = [
        None,
        None,
        None,
        None,
        None,
        None,
        None,
        ["points_length"],
        None,
        ["colors_length"],
        ["indices_length"],
    ]

    points_length: "int32_t"
    colors_length: "int32_t"
    indices_length: "int32_t"
    type: "byte"
    pose: geometry_msgs.Pose
    thickness: "double"
    scale_invariant: "boolean"
    points: geometry_msgs.Point
    color: Color
    colors: Color
    indices: "int32_t"

    LINE_STRIP = 0
    LINE_LOOP = 1
    LINE_LIST = 2

    def __init__(
        self,
        points_length=0,
        colors_length=0,
        indices_length=0,
        type=0,
        pose=geometry_msgs.Pose(),
        thickness=0.0,
        scale_invariant=False,
        points=[],
        color=Color(),
        colors=[],
        indices=[],
    ):
        # LCM Type: int32_t
        self.points_length = points_length
        # LCM Type: int32_t
        self.colors_length = colors_length
        # LCM Type: int32_t
        self.indices_length = indices_length
        # LCM Type: byte
        self.type = type
        # LCM Type: geometry_msgs.Pose
        self.pose = pose
        # LCM Type: double
        self.thickness = thickness
        # LCM Type: boolean
        self.scale_invariant = scale_invariant
        # LCM Type: geometry_msgs.Point[points_length]
        self.points = points
        # LCM Type: Color
        self.color = color
        # LCM Type: Color[colors_length]
        self.colors = colors
        # LCM Type: int32_t[indices_length]
        self.indices = indices

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

    def _encode_one(self, buf):
        buf.write(
            struct.pack(
                ">iiiB",
                self.points_length,
                self.colors_length,
                self.indices_length,
                self.type,
            )
        )
        assert (
            self.pose._get_packed_fingerprint()
            == geometry_msgs.Pose._get_packed_fingerprint()
        )
        self.pose._encode_one(buf)
        buf.write(struct.pack(">db", self.thickness, self.scale_invariant))
        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)
        assert self.color._get_packed_fingerprint() == Color._get_packed_fingerprint()
        self.color._encode_one(buf)
        for i0 in range(self.colors_length):
            assert (
                self.colors[i0]._get_packed_fingerprint()
                == Color._get_packed_fingerprint()
            )
            self.colors[i0]._encode_one(buf)
        buf.write(
            struct.pack(
                ">%di" % self.indices_length, *self.indices[: self.indices_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.points_length, self.colors_length, self.indices_length, self.type = (
            struct.unpack(">iiiB", buf.read(13))
        )
        self.pose = cls._get_field_type("pose")._decode_one(buf)
        self.thickness = struct.unpack(">d", buf.read(8))[0]
        self.scale_invariant = bool(struct.unpack("b", buf.read(1))[0])
        self.points = []
        for i0 in range(self.points_length):
            self.points.append(cls._get_field_type("points")._decode_one(buf))
        self.color = cls._get_field_type("color")._decode_one(buf)
        self.colors = []
        for i0 in range(self.colors_length):
            self.colors.append(cls._get_field_type("colors")._decode_one(buf))
        self.indices = struct.unpack(
            ">%di" % self.indices_length, buf.read(self.indices_length * 4)
        )
        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 = (
            0x3B39F8EB653B3CD3
            + geometry_msgs.Pose._get_hash_recursive(newparents)
            + geometry_msgs.Point._get_hash_recursive(newparents)
            + Color._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]
