# 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 Literal, Optional
import twnet_parser

class MsgInfo(PrettyPrint):
    def __init__(
            self,
            chunk_header: Optional[ChunkHeader] = None,
            version: str = '0.7 802f1be60a05665f',
            password: str = '',
            client_version: int = 1797
    ) -> None:
        self.message_type: Literal['system', 'game'] = 'system'
        self.message_name: str = 'info'
        self.system_message: bool = True
        self.message_id: int = 1
        if not chunk_header:
            chunk_header = ChunkHeader(version = '0.7')
        self.header: ChunkHeader = chunk_header

        self.version: str = version
        self.password: Optional[str] = password
        self.client_version: Optional[int] = client_version

    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 'version', self.version
        yield 'password', self.password
        yield 'client_version', self.client_version

    # 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.version = unpacker.get_str()
        self.password = unpacker.get_optional_str()
        self.client_version = unpacker.get_optional_int()
        return True

    def pack(self) -> bytes:
        lib_version = b''
        if self.password is not None and self.client_version is not None:
            lib_version = pack_str(f"twnet_parser {twnet_parser.__version__}")
        return pack_str(self.version) + \
            (pack_str(self.password) if self.password is not None else b'') + \
            (pack_int(self.client_version) if self.client_version is not None else b'') + \
            lib_version
