"""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


class Joy(object):
    msg_name = "sensor_msgs.Joy"

    __slots__ = ["axes_length", "buttons_length", "header", "axes", "buttons"]

    __typenames__ = ["int32_t", "int32_t", "std_msgs.Header", "float", "int32_t"]

    __dimensions__ = [None, None, None, ["axes_length"], ["buttons_length"]]

    axes_length: "int32_t"
    buttons_length: "int32_t"
    header: std_msgs.Header
    axes: "float"
    buttons: "int32_t"

    def __init__(
        self,
        axes_length=0,
        buttons_length=0,
        header=std_msgs.Header(),
        axes=[],
        buttons=[],
    ):
        # LCM Type: int32_t
        self.axes_length = axes_length
        # LCM Type: int32_t
        self.buttons_length = buttons_length
        # LCM Type: std_msgs.Header
        self.header = header
        # LCM Type: float[axes_length]
        self.axes = axes
        # LCM Type: int32_t[buttons_length]
        self.buttons = buttons

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

    def _encode_one(self, buf):
        buf.write(struct.pack(">ii", self.axes_length, self.buttons_length))
        assert (
            self.header._get_packed_fingerprint()
            == std_msgs.Header._get_packed_fingerprint()
        )
        self.header._encode_one(buf)
        buf.write(
            struct.pack(">%df" % self.axes_length, *self.axes[: self.axes_length])
        )
        buf.write(
            struct.pack(
                ">%di" % self.buttons_length, *self.buttons[: self.buttons_length]
            )
        )

    @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.axes_length, self.buttons_length = struct.unpack(">ii", buf.read(8))
        self.header = cls._get_field_type("header")._decode_one(buf)
        self.axes = struct.unpack(
            ">%df" % self.axes_length, buf.read(self.axes_length * 4)
        )
        self.buttons = struct.unpack(
            ">%di" % self.buttons_length, buf.read(self.buttons_length * 4)
        )
        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 = (
            0x209D2C19077A692F + std_msgs.Header._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]
