from typing import Optional
from application.util import LoggerUtil

logger = LoggerUtil.get_logger_name(__name__)

PENDING_UPDATE = "$PENDING_UPDATE"
SUCCESS_UPDATE = "$SUCCESS_UPDATE"


class UpdateControl(object):
    def __init__(self):
        self.catalog = PENDING_UPDATE
        self.user = PENDING_UPDATE
        self.loader = PENDING_UPDATE
        self.binaries = PENDING_UPDATE
        self.updating = False

    def clear_all_status(self):
        # type: () -> None

        self.set_status('catalog', None)
        self.set_status('user', None)
        self.set_status('loader', None)
        self.set_status('binaries', None)

    def set_status(self, key, value):
        # type: (str, Optional[str]) -> None

        setattr(self, key, value)

    def check_status(self, pos_version):
        # type: (str) -> None

        attributes = vars(pos_version)
        for item in attributes:
            if not hasattr(self, item) or getattr(self, item) not in [SUCCESS_UPDATE, PENDING_UPDATE, None]:
                continue

            value = PENDING_UPDATE if attributes[item] == '' else SUCCESS_UPDATE
            self.set_status(item, value)
