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

class MsgSvGameInfo(PrettyPrint):
    def __init__(
            self,
            chunk_header: Optional[ChunkHeader] = None,
            game_flags: int = 0,
            score_limit: int = 0,
            time_limit: int = 0,
            match_num: int = 0,
            match_current: int = 0
    ) -> None:
        self.message_type: Literal['system', 'game'] = 'game'
        self.message_name: str = 'sv_game_info'
        self.system_message: bool = False
        self.message_id: int = 19
        if not chunk_header:
            chunk_header = ChunkHeader(version = '0.7')
        self.header: ChunkHeader = chunk_header

        self.game_flags: int = game_flags
        self.score_limit: int = score_limit
        self.time_limit: int = time_limit
        self.match_num: int = match_num
        self.match_current: int = match_current

    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 'game_flags', self.game_flags
        yield 'score_limit', self.score_limit
        yield 'time_limit', self.time_limit
        yield 'match_num', self.match_num
        yield 'match_current', self.match_current

    # 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.game_flags = unpacker.get_int() # TODO: this is a flag
        self.score_limit = unpacker.get_int()
        self.time_limit = unpacker.get_int()
        self.match_num = unpacker.get_int()
        self.match_current = unpacker.get_int()
        return True

    def pack(self) -> bytes:
        return pack_int(self.game_flags) + \
            pack_int(self.score_limit) + \
            pack_int(self.time_limit) + \
            pack_int(self.match_num) + \
            pack_int(self.match_current)