"""LCM type definitions
This file automatically generated by lcm.
DO NOT MODIFY BY HAND!!!!
"""


from io import BytesIO
import struct

class lcmt_viewer_draw(object):

    __slots__ = ["timestamp", "num_links", "link_name", "robot_num", "position", "quaternion"]

    __typenames__ = ["int64_t", "int32_t", "string", "int32_t", "float", "float"]

    __dimensions__ = [None, None, ["num_links"], ["num_links"], ["num_links", 3], ["num_links", 4]]

    def __init__(self):
        self.timestamp = 0
        """
        The timestamp in milliseconds.
        LCM Type: int64_t
        """

        self.num_links = 0
        """ LCM Type: int32_t """
        self.link_name = []
        """ LCM Type: string[num_links] """
        self.robot_num = []
        """ LCM Type: int32_t[num_links] """
        self.position = []
        """ LCM Type: float[num_links][3] """
        self.quaternion = []
        """
        x, y, z
        LCM Type: float[num_links][4]
        """


    def encode(self):
        buf = BytesIO()
        buf.write(lcmt_viewer_draw._get_packed_fingerprint())
        self._encode_one(buf)
        return buf.getvalue()

    def _encode_one(self, buf):
        buf.write(struct.pack(">qi", self.timestamp, self.num_links))
        for i0 in range(self.num_links):
            __link_name_encoded = self.link_name[i0].encode('utf-8')
            buf.write(struct.pack('>I', len(__link_name_encoded)+1))
            buf.write(__link_name_encoded)
            buf.write(b"\0")
        buf.write(struct.pack('>%di' % self.num_links, *self.robot_num[:self.num_links]))
        for i0 in range(self.num_links):
            buf.write(struct.pack('>3f', *self.position[i0][:3]))
        for i0 in range(self.num_links):
            buf.write(struct.pack('>4f', *self.quaternion[i0][:4]))

    @staticmethod
    def decode(data: bytes):
        if hasattr(data, 'read'):
            buf = data
        else:
            buf = BytesIO(data)
        if buf.read(8) != lcmt_viewer_draw._get_packed_fingerprint():
            raise ValueError("Decode error")
        return lcmt_viewer_draw._decode_one(buf)

    @staticmethod
    def _decode_one(buf):
        self = lcmt_viewer_draw()
        self.timestamp, self.num_links = struct.unpack(">qi", buf.read(12))
        self.link_name = []
        for i0 in range(self.num_links):
            __link_name_len = struct.unpack('>I', buf.read(4))[0]
            self.link_name.append(buf.read(__link_name_len)[:-1].decode('utf-8', 'replace'))
        self.robot_num = struct.unpack('>%di' % self.num_links, buf.read(self.num_links * 4))
        self.position = []
        for i0 in range(self.num_links):
            self.position.append(struct.unpack('>3f', buf.read(12)))
        self.quaternion = []
        for i0 in range(self.num_links):
            self.quaternion.append(struct.unpack('>4f', buf.read(16)))
        return self

    @staticmethod
    def _get_hash_recursive(parents):
        if lcmt_viewer_draw in parents: return 0
        tmphash = (0x20a785ff2d97a122) & 0xffffffffffffffff
        tmphash  = (((tmphash<<1)&0xffffffffffffffff) + (tmphash>>63)) & 0xffffffffffffffff
        return tmphash
    _packed_fingerprint = None

    @staticmethod
    def _get_packed_fingerprint():
        if lcmt_viewer_draw._packed_fingerprint is None:
            lcmt_viewer_draw._packed_fingerprint = struct.pack(">Q", lcmt_viewer_draw._get_hash_recursive([]))
        return lcmt_viewer_draw._packed_fingerprint

    def get_hash(self):
        """Get the LCM hash of the struct"""
        return struct.unpack(">Q", lcmt_viewer_draw._get_packed_fingerprint())[0]

