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

from io import BytesIO
import struct
import sys

from . import *
from lcm_msgs import builtin_interfaces
from .Point2 import Point2
from .Color import Color


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

    __slots__ = [
        "points_length",
        "outline_colors_length",
        "timestamp",
        "type",
        "points",
        "outline_color",
        "outline_colors",
        "fill_color",
        "thickness",
    ]

    __typenames__ = [
        "int32_t",
        "int32_t",
        "builtin_interfaces.Time",
        "byte",
        "Point2",
        "Color",
        "Color",
        "Color",
        "double",
    ]

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

    points_length: "int32_t"
    outline_colors_length: "int32_t"
    timestamp: builtin_interfaces.Time
    type: "byte"
    points: Point2
    outline_color: Color
    outline_colors: Color
    fill_color: Color
    thickness: "double"

    UNKNOWN = 0
    POINTS = 1
    LINE_LOOP = 2
    LINE_STRIP = 3
    LINE_LIST = 4

    def __init__(
        self,
        points_length=0,
        outline_colors_length=0,
        timestamp=builtin_interfaces.Time(),
        type=0,
        points=[],
        outline_color=Color(),
        outline_colors=[],
        fill_color=Color(),
        thickness=0.0,
    ):
        # LCM Type: int32_t
        self.points_length = points_length
        # LCM Type: int32_t
        self.outline_colors_length = outline_colors_length
        # LCM Type: builtin_interfaces.Time
        self.timestamp = timestamp
        # LCM Type: byte
        self.type = type
        # LCM Type: Point2[points_length]
        self.points = points
        # LCM Type: Color
        self.outline_color = outline_color
        # LCM Type: Color[outline_colors_length]
        self.outline_colors = outline_colors
        # LCM Type: Color
        self.fill_color = fill_color
        # LCM Type: double
        self.thickness = thickness

    def lcm_encode(self):
        buf = BytesIO()
        buf.write(PointsAnnotation._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.timestamp._get_packed_fingerprint()
            == builtin_interfaces.Time._get_packed_fingerprint()
        )
        self.timestamp._encode_one(buf)
        buf.write(struct.pack(">B", self.type))
        for i0 in range(self.points_length):
            assert (
                self.points[i0]._get_packed_fingerprint()
                == Point2._get_packed_fingerprint()
            )
            self.points[i0]._encode_one(buf)
        assert (
            self.outline_color._get_packed_fingerprint()
            == Color._get_packed_fingerprint()
        )
        self.outline_color._encode_one(buf)
        for i0 in range(self.outline_colors_length):
            assert (
                self.outline_colors[i0]._get_packed_fingerprint()
                == Color._get_packed_fingerprint()
            )
            self.outline_colors[i0]._encode_one(buf)
        assert (
            self.fill_color._get_packed_fingerprint() == Color._get_packed_fingerprint()
        )
        self.fill_color._encode_one(buf)
        buf.write(struct.pack(">d", self.thickness))

    @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.timestamp = cls._get_field_type("timestamp")._decode_one(buf)
        self.type = 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.outline_color = cls._get_field_type("outline_color")._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)
            )
        self.fill_color = cls._get_field_type("fill_color")._decode_one(buf)
        self.thickness = struct.unpack(">d", buf.read(8))[0]
        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 = (
            0x97465363CC7C2A18
            + builtin_interfaces.Time._get_hash_recursive(newparents)
            + Point2._get_hash_recursive(newparents)
            + Color._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]
