# -*- 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 ..util.get_menu_user_logged import get_menu_manager_authenticated_user


@action
def update_chrome(
    pos_id   # type: str
):
    # type: (...) -> None

    resp = show_messagebox(
        posid=pos_id,
        message="$UPDATE_CHROME_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.chrome_update(
        user_id=user_id,
        validate_version=False,
    )

    if reply.token == TK_SYS_NAK or reply.data:
        show_messagebox(
            posid=pos_id,
            message="$UPDATE_CHROME_" + reply.data.upper(),
            buttons="OK",
        )
        return

    show_messagebox(
        posid=pos_id,
        message="$UPDATE_CHROME_MESSAGE",
        buttons="OK",
    )
