# -*- coding: utf-8 -*-

import sysactions
from bustoken import TK_REMOTE_ORDER_VOID_REMOTE_ORDER
from msgbus import TK_SYS_ACK
from systools import sys_log_exception

from .. import mb_context


def void_remote_order(
    pos_id,                     # type: str
    remote_id,                  # type: str
):
    # type: (...) -> bool

    try:
        token = TK_REMOTE_ORDER_VOID_REMOTE_ORDER
        data = str(remote_id)
        timeout = 10000 * 1000
        msg = mb_context.MB_EasySendMessage('RemoteOrder', token, data=data, timeout=timeout)

        if msg.token == TK_SYS_ACK:
            return True

        model = sysactions.get_model(pos_id)
        error_message = sysactions.translate_message(model, "UNKNOWN_ERROR")
        if msg.data:
            error_message = sysactions.translate_message(
                model=model,
                message_key=msg.data.replace("$", ""),
            )

        sysactions.show_messagebox(
            posid=pos_id,
            message=error_message,
        )
        return False

    except Exception:
        sys_log_exception("Error voiding delivery order")
        raise
