from xml.etree import cElementTree as eTree

from pos_model import TableStatus
from msgbusboundary import MsgBusTableService as tableService
from mw_helper import show_message_dialog


def validate_if_table_opened_another_pos(
    pos_id,                 # type: str
    current_table_picture,  # type: eTree.Element
):
    # type: (...) -> bool

    current_pos_id = current_table_picture.get('currentPOSId', None)
    if int(current_table_picture.get('status')) == TableStatus.LINKED:
        linked_table_id = current_table_picture.get("parentLinkedTableId")
        linked_table_picture = eTree.XML(
            tableService().get_table_info(
                pos_id=pos_id,
                table_id=linked_table_id,
            )
        )
        linked_current_pos_id = linked_table_picture.get('currentPOSId', None)
        current_pos_id = linked_current_pos_id if linked_current_pos_id else current_pos_id

    elif current_table_picture.get("sliceParentTableId"):
        parent_table_id = current_table_picture.get("sliceParentTableId")
        parent_table_picture = eTree.XML(
            tableService().get_table_info(
                pos_id=pos_id,
                table_id=parent_table_id,
            )
        )
        parent_current_pos_id = parent_table_picture.get("currentPOSId")
        current_pos_id = parent_current_pos_id if parent_current_pos_id else current_pos_id

    if current_pos_id and current_pos_id != pos_id:
        message = "$TABLE_OPENED_IN_ANOTHER_POS|{}".format(current_pos_id)
        show_message_dialog(
            pos_id=pos_id,
            title="$INFORMATION",
            message=message,
        )
        return True

    return False
