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

from sysactions import action, show_messagebox
from msgbus import TK_SYS_NAK

from api.pos_updater import get_pos_updater_service
from .update_util import show_nak_message
from ..util.get_menu_user_logged import get_menu_manager_authenticated_user


@action
def update_binaries(pos_id):

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

    user_id = get_menu_manager_authenticated_user(pos_id=int(pos_id))
    updater_service = get_pos_updater_service(pos_id=pos_id)
    reply = updater_service.binaries_update(
        user_id=user_id,
        force_update=True,
    )
    if reply.token == TK_SYS_NAK:
        show_nak_message(
            pos_id=pos_id,
            update_key="$UPDATE_BINARIES_",
            message=reply.data,
        )

    show_messagebox(pos_id, "$UPDATE_BINARIES_MESSAGE", buttons="OK")
