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

    __slots__ = [
        "header",
        "orientation",
        "orientation_covariance",
        "angular_velocity",
        "angular_velocity_covariance",
        "linear_acceleration",
        "linear_acceleration_covariance",
    ]

    __typenames__ = [
        "std_msgs.Header",
        "geometry_msgs.Quaternion",
        "double",
        "geometry_msgs.Vector3",
        "double",
        "geometry_msgs.Vector3",
        "double",
    ]

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

    header: std_msgs.Header
    orientation: geometry_msgs.Quaternion
    orientation_covariance: "double"
    angular_velocity: geometry_msgs.Vector3
    angular_velocity_covariance: "double"
    linear_acceleration: geometry_msgs.Vector3
    linear_acceleration_covariance: "double"

    def __init__(
        self,
        header=std_msgs.Header(),
        orientation=geometry_msgs.Quaternion(),
        orientation_covariance=[0.0 for dim0 in range(9)],
        angular_velocity=geometry_msgs.Vector3(),
        angular_velocity_covariance=[0.0 for dim0 in range(9)],
        linear_acceleration=geometry_msgs.Vector3(),
        linear_acceleration_covariance=[0.0 for dim0 in range(9)],
    ):
        # LCM Type: std_msgs.Header
        self.header = header
        # LCM Type: geometry_msgs.Quaternion
        self.orientation = orientation
        # LCM Type: double[9]
        self.orientation_covariance = orientation_covariance
        # LCM Type: geometry_msgs.Vector3
        self.angular_velocity = angular_velocity
        # LCM Type: double[9]
        self.angular_velocity_covariance = angular_velocity_covariance
        # LCM Type: geometry_msgs.Vector3
        self.linear_acceleration = linear_acceleration
        # LCM Type: double[9]
        self.linear_acceleration_covariance = linear_acceleration_covariance

    def lcm_encode(self):
        buf = BytesIO()
        buf.write(Imu._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.orientation._get_packed_fingerprint()
            == geometry_msgs.Quaternion._get_packed_fingerprint()
        )
        self.orientation._encode_one(buf)
        buf.write(struct.pack(">9d", *self.orientation_covariance[:9]))
        assert (
            self.angular_velocity._get_packed_fingerprint()
            == geometry_msgs.Vector3._get_packed_fingerprint()
        )
        self.angular_velocity._encode_one(buf)
        buf.write(struct.pack(">9d", *self.angular_velocity_covariance[:9]))
        assert (
            self.linear_acceleration._get_packed_fingerprint()
            == geometry_msgs.Vector3._get_packed_fingerprint()
        )
        self.linear_acceleration._encode_one(buf)
        buf.write(struct.pack(">9d", *self.linear_acceleration_covariance[:9]))

    @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.orientation = cls._get_field_type("orientation")._decode_one(buf)
        self.orientation_covariance = struct.unpack(">9d", buf.read(72))
        self.angular_velocity = cls._get_field_type("angular_velocity")._decode_one(buf)
        self.angular_velocity_covariance = struct.unpack(">9d", buf.read(72))
        self.linear_acceleration = cls._get_field_type(
            "linear_acceleration"
        )._decode_one(buf)
        self.linear_acceleration_covariance = struct.unpack(">9d", buf.read(72))
        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 = (
            0x55C1E238541325F6
            + std_msgs.Header._get_hash_recursive(newparents)
            + geometry_msgs.Quaternion._get_hash_recursive(newparents)
            + geometry_msgs.Vector3._get_hash_recursive(newparents)
            + geometry_msgs.Vector3._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]
