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

    __slots__ = ["map_load_time", "resolution", "width", "height", "origin"]

    __typenames__ = [
        "std_msgs.Time",
        "float",
        "int32_t",
        "int32_t",
        "geometry_msgs.Pose",
    ]

    __dimensions__ = [None, None, None, None, None]

    map_load_time: std_msgs.Time
    resolution: "float"
    width: "int32_t"
    height: "int32_t"
    origin: geometry_msgs.Pose

    def __init__(
        self,
        map_load_time=std_msgs.Time(),
        resolution=0.0,
        width=0,
        height=0,
        origin=geometry_msgs.Pose(),
    ):
        # LCM Type: std_msgs.Time
        self.map_load_time = map_load_time
        # LCM Type: float
        self.resolution = resolution
        # LCM Type: int32_t
        self.width = width
        # LCM Type: int32_t
        self.height = height
        # LCM Type: geometry_msgs.Pose
        self.origin = origin

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

    def _encode_one(self, buf):
        assert (
            self.map_load_time._get_packed_fingerprint()
            == std_msgs.Time._get_packed_fingerprint()
        )
        self.map_load_time._encode_one(buf)
        buf.write(struct.pack(">fii", self.resolution, self.width, self.height))
        assert (
            self.origin._get_packed_fingerprint()
            == geometry_msgs.Pose._get_packed_fingerprint()
        )
        self.origin._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.map_load_time = cls._get_field_type("map_load_time")._decode_one(buf)
        self.resolution, self.width, self.height = struct.unpack(">fii", buf.read(12))
        self.origin = cls._get_field_type("origin")._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 = (
            0x3245F3CDB468BA93
            + std_msgs.Time._get_hash_recursive(newparents)
            + geometry_msgs.Pose._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]
