# 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 SANITIZE_CC, pack_int, pack_str
from typing import Literal, Optional
import twnet_parser.enum7 as enum7

class MsgSvChat(PrettyPrint):
    def __init__(
            self,
            chunk_header: Optional[ChunkHeader] = None,
            mode: int = enum7.Chat.NONE.value,
            client_id: int = 0,
            target_id: int = 0,
            message: str = 'default'
    ) -> None:
        self.message_type: Literal['system', 'game'] = 'game'
        self.message_name: str = 'sv_chat'
        self.system_message: bool = False
        self.message_id: int = 3
        if not chunk_header:
            chunk_header = ChunkHeader(version = '0.7')
        self.header: ChunkHeader = chunk_header

        self.mode: int = mode
        self.client_id: int = client_id
        self.target_id: int = target_id
        self.message: str = message

    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 'mode', self.mode
        yield 'client_id', self.client_id
        yield 'target_id', self.target_id
        yield 'message', self.message

    # 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.mode = unpacker.get_int() # enum CHAT
        self.client_id = unpacker.get_int()
        self.target_id = unpacker.get_int()
        self.message = unpacker.get_str(SANITIZE_CC)
        return True

    def pack(self) -> bytes:
        return pack_int(self.mode) + \
            pack_int(self.client_id) + \
            pack_int(self.target_id) + \
            pack_str(self.message)