from bustoken import TK_REMOTE_ORDER_GET_POS_ID
from msgbus import (
    FM_PARAM,
    TK_SYS_NAK,
)
from sysactions import send_message

pos_id_remote_order = None


def is_delivery_order(model):
    order = model.find("CurrentOrder/Order")
    if not order:
        return False

    return order.find("CustomOrderProperties/OrderProperty[@key='REMOTE_ORDER_ID']") is not None


def get_pos_id_of_remote_order():
    # type: (...) -> int

    global pos_id_remote_order

    if pos_id_remote_order is not None:
        return pos_id_remote_order

    msg = send_message(
        dest_name="RemoteOrder",
        token=TK_REMOTE_ORDER_GET_POS_ID,
        format=FM_PARAM,
        timeout=100000,
    )
    if msg.token == TK_SYS_NAK:
        return 0

    pos_id_remote_order = int(msg.data)
    return pos_id_remote_order
