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

from sysactions import action, show_messagebox # noqa
from bustoken import TK_POS_UPDATER_PERFORM_LOADER_UPDATE
from msgbus import TK_SYS_NAK # noqa

from .update_util import show_nak_message
from .. import mb_context


@action
def update_loaders(pos_id):

    resp = show_messagebox(pos_id, "$UPDATE_LOADERS_MESSAGE_QUESTION", buttons="$OK|$CANCEL")
    if resp == 1:
        return

    restart_hv = "true"
    timeout = 60000000 * 5
    loader_update_token = TK_POS_UPDATER_PERFORM_LOADER_UPDATE
    msg = mb_context.MB_EasySendMessage("PosUpdater", loader_update_token, data=restart_hv, timeout=timeout)
    if msg.token == TK_SYS_NAK:
        response_message = msg.data
        show_nak_message(pos_id, "$UPDATE_LOADERS_", response_message)

    if restart_hv == "false":
        show_messagebox(pos_id, "$UPDATE_LOADERS_MESSAGE", buttons="OK")
