from datetime import date

from actions.update import get_catalog_version_to_apply, get_message_update, apply_update
from actions.rupture.get_ruptured_products import has_rupture_items
from msgbus import TK_SYS_ACK
from sysactions import show_messagebox, format_date, StopAction

BUTTONS = "$OK|$CANCEL"


class OpenDayInteractor(object):
    @staticmethod
    def check_has_update_menu(pos_id):
        # type: (int) -> None

        today = date.today().strftime("%Y%m%d")
        confirm_message = "$CONFIRM_OPEN_CURRENT_DAY|{0}".format(format_date(today))
        msg = get_catalog_version_to_apply()
        if msg.token == TK_SYS_ACK:
            get_message_update(pos_id, "$APPLYING_UPDATE_MENU_VERSION_PENDING|" + msg.data)
            has_rupture_items(pos_id)
            applied_update = apply_update(pos_id)
            if not applied_update:
                confirm_message = "$UPDATE_MENU_ERROR_PROCEED"

        confirm = show_messagebox(pos_id, message=confirm_message, buttons=BUTTONS)
        if (confirm is None) or (confirm == 1):
            raise StopAction()
