# generated by scripts/generate_messages.py

from twnet_parser.pretty_print import PrettyPrint
from twnet_parser.packer import Unpacker
from twnet_parser.chunk_header import ChunkHeader
from twnet_parser.packer import pack_int, pack_str
from typing import Annotated, Literal, Optional

class MsgMapChange(PrettyPrint):
    def __init__(
            self,
            chunk_header: Optional[ChunkHeader] = None,
            name: str = 'default',
            crc: int = 0,
            size: int = 0,
            num_response_chunks_per_request: int = 0,
            chunk_size: int = 0,
            sha256: Annotated[bytes, 32] = bytes(32)
    ) -> None:
        self.message_type: Literal['system', 'game'] = 'system'
        self.message_name: str = 'map_change'
        self.system_message: bool = True
        self.message_id: int = 2
        if not chunk_header:
            chunk_header = ChunkHeader(version = '0.7')
        self.header: ChunkHeader = chunk_header

        self.name: str = name
        self.crc: int = crc
        self.size: int = size
        self.num_response_chunks_per_request: int = num_response_chunks_per_request
        self.chunk_size: int = chunk_size
        self.sha256: Annotated[bytes, 32] = sha256

    def __iter__(self):
        yield 'message_type', self.message_type
        yield 'message_name', self.message_name
        yield 'system_message', self.system_message
        yield 'message_id', self.message_id
        yield 'header', dict(self.header)

        yield 'name', self.name
        yield 'crc', self.crc
        yield 'size', self.size
        yield 'num_response_chunks_per_request', self.num_response_chunks_per_request
        yield 'chunk_size', self.chunk_size
        yield 'sha256', self.sha256

    # first byte of data
    # has to be the first byte of the message payload
    # NOT the chunk header and NOT the message id
    def unpack(self, data: bytes) -> bool:
        unpacker = Unpacker(data)
        self.name = unpacker.get_str()
        self.crc = unpacker.get_int()
        self.size = unpacker.get_int()
        self.num_response_chunks_per_request = unpacker.get_int()
        self.chunk_size = unpacker.get_int()
        self.sha256 = unpacker.get_raw(32)
        return True

    def pack(self) -> bytes:
        return pack_str(self.name) + \
            pack_int(self.crc) + \
            pack_int(self.size) + \
            pack_int(self.num_response_chunks_per_request) + \
            pack_int(self.chunk_size) + \
            self.sha256