from datetime import datetime

from typing import Optional, Union

from msgbus import MBEasyContext

from application.model import UpdateControl
from typing import Optional

update_control = UpdateControl()
settings_mb_context = None


def init(
        mb_context  # type: MBEasyContext
):
    # type: (...) -> None

    global settings_mb_context
    settings_mb_context = mb_context


def set_update_control(key, value):
    # type: (str, Optional[Union[str, bool]]) -> None

    global update_control
    update_control.set_status(key, value)

    if key == "updating" and value is True:
        show_update_version_screen(settings_mb_context)


def check_status(pos_version):

    global update_control
    update_control.check_status(pos_version)


def clear_status():

    global update_control
    update_control.clear_all_status()


def show_update_version_screen(
        mb_context  # type: Optional[MBEasyContext]
):
    # type: (...) -> None

    data = str(datetime.now())
    mb_context.MB_EasyEvtSend("FETCH_POS_VERSIONS", "", data)
