"""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 .SceneEntity import SceneEntity
from .SceneEntityDeletion import SceneEntityDeletion


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

    __slots__ = ["deletions_length", "entities_length", "deletions", "entities"]

    __typenames__ = ["int32_t", "int32_t", "SceneEntityDeletion", "SceneEntity"]

    __dimensions__ = [None, None, ["deletions_length"], ["entities_length"]]

    deletions_length: "int32_t"
    entities_length: "int32_t"
    deletions: SceneEntityDeletion
    entities: SceneEntity

    def __init__(
        self, deletions_length=0, entities_length=0, deletions=[], entities=[]
    ):
        # LCM Type: int32_t
        self.deletions_length = deletions_length
        # LCM Type: int32_t
        self.entities_length = entities_length
        # LCM Type: SceneEntityDeletion[deletions_length]
        self.deletions = deletions
        # LCM Type: SceneEntity[entities_length]
        self.entities = entities

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

    def _encode_one(self, buf):
        buf.write(struct.pack(">ii", self.deletions_length, self.entities_length))
        for i0 in range(self.deletions_length):
            assert (
                self.deletions[i0]._get_packed_fingerprint()
                == SceneEntityDeletion._get_packed_fingerprint()
            )
            self.deletions[i0]._encode_one(buf)
        for i0 in range(self.entities_length):
            assert (
                self.entities[i0]._get_packed_fingerprint()
                == SceneEntity._get_packed_fingerprint()
            )
            self.entities[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.deletions_length, self.entities_length = struct.unpack(">ii", buf.read(8))
        self.deletions = []
        for i0 in range(self.deletions_length):
            self.deletions.append(cls._get_field_type("deletions")._decode_one(buf))
        self.entities = []
        for i0 in range(self.entities_length):
            self.entities.append(cls._get_field_type("entities")._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 = (
            0x8F9D4EE9E2A92D31
            + SceneEntityDeletion._get_hash_recursive(newparents)
            + SceneEntity._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]
