from typing import (
    Optional,
)
from xml.etree import cElementTree as eTree

from msgbus import (
    FM_PARAM,
    TK_CMP_MESSAGE,
    TK_SYS_NAK,
)
from messagebus import (
    Message,
    MessageBus,
)


class TablePictureRepository(object):
    def __init__(
        self,
        message_bus,      # type: MessageBus
    ):
        # type: (...) -> None

        self.message_bus = message_bus

    def get_table_info(
        self,
        pos_id,      # type: int
        table_id,    # type: int
    ):
        # type: (...) -> eTree

        if not table_id:
            return None

        params = ("TABLE_PICTURE", pos_id, table_id)
        req_buf = "\0".join([str(p) for p in params])
        message = Message(
            token=TK_CMP_MESSAGE,
            data_type=FM_PARAM,
            data=req_buf,
        )
        msg = self.message_bus.send_message(
            component='TABLEMGR',
            message=message,
        )

        if msg.token != TK_SYS_NAK:
            return eTree.XML(msg.data)

        return None
