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


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

    __slots__ = [
        "data_length",
        "timestamp",
        "data",
        "format",
        "sample_rate",
        "number_of_channels",
    ]

    __typenames__ = [
        "int32_t",
        "builtin_interfaces.Time",
        "byte",
        "string",
        "int32_t",
        "int32_t",
    ]

    __dimensions__ = [None, None, ["data_length"], None, None, None]

    data_length: "int32_t"
    timestamp: builtin_interfaces.Time
    data: "byte"
    format: "string"
    sample_rate: "int32_t"
    number_of_channels: "int32_t"

    def __init__(
        self,
        data_length=0,
        timestamp=builtin_interfaces.Time(),
        data=b"",
        format="",
        sample_rate=0,
        number_of_channels=0,
    ):
        # LCM Type: int32_t
        self.data_length = data_length
        # LCM Type: builtin_interfaces.Time
        self.timestamp = timestamp
        # LCM Type: byte[data_length]
        self.data = data
        # LCM Type: string
        self.format = format
        # LCM Type: int32_t
        self.sample_rate = sample_rate
        # LCM Type: int32_t
        self.number_of_channels = number_of_channels

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

    def _encode_one(self, buf):
        buf.write(struct.pack(">i", self.data_length))
        assert (
            self.timestamp._get_packed_fingerprint()
            == builtin_interfaces.Time._get_packed_fingerprint()
        )
        self.timestamp._encode_one(buf)
        buf.write(bytearray(self.data[: self.data_length]))
        __format_encoded = self.format.encode("utf-8")
        buf.write(struct.pack(">I", len(__format_encoded) + 1))
        buf.write(__format_encoded)
        buf.write(b"\0")
        buf.write(struct.pack(">ii", self.sample_rate, self.number_of_channels))

    @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.data_length = struct.unpack(">i", buf.read(4))[0]
        self.timestamp = cls._get_field_type("timestamp")._decode_one(buf)
        self.data = buf.read(self.data_length)
        __format_len = struct.unpack(">I", buf.read(4))[0]
        self.format = buf.read(__format_len)[:-1].decode("utf-8", "replace")
        self.sample_rate, self.number_of_channels = struct.unpack(">ii", buf.read(8))
        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 = (
            0xC7F2A980CD122F7D + builtin_interfaces.Time._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]
