from bustoken import create_token, MSGPRT_LOW
from msgbus import TK_SYS_NAK, MBException
from sysactions import action, send_message, show_messagebox, close_asynch_dialog

TK_POS_UPDATER_PERFORM_USER_UPDATE = create_token(MSGPRT_LOW, "95", "3")


@action
def importEmployees(pos_id):
    wait_dlg_id = show_messagebox(pos_id, "$PLEASE_WAIT", "", buttons="", asynch=True)
    success = False
    try:
        reply = send_message("PosUpdater", TK_POS_UPDATER_PERFORM_USER_UPDATE, timeout=30000000)
        if reply.token == TK_SYS_NAK:
            raise MBException(reply.data, "")
        success = True
        return True
    except MBException as _:
        return False
    finally:
        close_asynch_dialog(pos_id, wait_dlg_id)
        if success:
            show_messagebox(pos_id, title="", message="$OPERATION_SUCCEEDED")
        else:
            show_messagebox(pos_id, title="", message="$OPERATION_FAILED")
