"""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 std_msgs
from lcm_msgs import sensor_msgs


class DisparityImage(object):
    msg_name = "stereo_msgs.DisparityImage"

    __slots__ = [
        "header",
        "image",
        "f",
        "T",
        "valid_window",
        "min_disparity",
        "max_disparity",
        "delta_d",
    ]

    __typenames__ = [
        "std_msgs.Header",
        "sensor_msgs.Image",
        "float",
        "float",
        "sensor_msgs.RegionOfInterest",
        "float",
        "float",
        "float",
    ]

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

    header: std_msgs.Header
    image: sensor_msgs.Image
    f: "float"
    T: "float"
    valid_window: sensor_msgs.RegionOfInterest
    min_disparity: "float"
    max_disparity: "float"
    delta_d: "float"

    def __init__(
        self,
        header=std_msgs.Header(),
        image=sensor_msgs.Image(),
        f=0.0,
        T=0.0,
        valid_window=sensor_msgs.RegionOfInterest(),
        min_disparity=0.0,
        max_disparity=0.0,
        delta_d=0.0,
    ):
        # LCM Type: std_msgs.Header
        self.header = header
        # LCM Type: sensor_msgs.Image
        self.image = image
        # LCM Type: float
        self.f = f
        # LCM Type: float
        self.T = T
        # LCM Type: sensor_msgs.RegionOfInterest
        self.valid_window = valid_window
        # LCM Type: float
        self.min_disparity = min_disparity
        # LCM Type: float
        self.max_disparity = max_disparity
        # LCM Type: float
        self.delta_d = delta_d

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

    def _encode_one(self, buf):
        assert (
            self.header._get_packed_fingerprint()
            == std_msgs.Header._get_packed_fingerprint()
        )
        self.header._encode_one(buf)
        assert (
            self.image._get_packed_fingerprint()
            == sensor_msgs.Image._get_packed_fingerprint()
        )
        self.image._encode_one(buf)
        buf.write(struct.pack(">ff", self.f, self.T))
        assert (
            self.valid_window._get_packed_fingerprint()
            == sensor_msgs.RegionOfInterest._get_packed_fingerprint()
        )
        self.valid_window._encode_one(buf)
        buf.write(
            struct.pack(">fff", self.min_disparity, self.max_disparity, self.delta_d)
        )

    @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.header = cls._get_field_type("header")._decode_one(buf)
        self.image = cls._get_field_type("image")._decode_one(buf)
        self.f, self.T = struct.unpack(">ff", buf.read(8))
        self.valid_window = cls._get_field_type("valid_window")._decode_one(buf)
        self.min_disparity, self.max_disparity, self.delta_d = struct.unpack(
            ">fff", buf.read(12)
        )
        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 = (
            0xDEB7A557A2B9258A
            + std_msgs.Header._get_hash_recursive(newparents)
            + sensor_msgs.Image._get_hash_recursive(newparents)
            + sensor_msgs.RegionOfInterest._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]
