from application.interactor import BinaryUpdaterInteractor
from application.manager._UpdaterManager import UpdaterManager
from application.model import UpdateType
from application.model.configuration import Configurations
from application.repository.pos import BinaryUpdaterPOSRepository
from application.util import LoggerUtil
from typing import Optional

logger = LoggerUtil.get_logger_name(__name__)


class BinaryUpdaterManager(UpdaterManager):
    def __init__(
        self,
        configs,            # type: Configurations
        interactor,         # type: BinaryUpdaterInteractor
        pos_repository,     # type: BinaryUpdaterPOSRepository
    ):
        # type: (...) -> None

        name = UpdateType.binaries.name
        super(BinaryUpdaterManager, self).__init__(name, configs, interactor, pos_repository, logger)
        self.logger = logger
        self.configs = configs
        self.update_type = UpdateType.binaries
        self.local_configs = configs.updaters[self.update_type.name]
        self.active_frequency = int(self.local_configs.active_frequency)
        self.interactor = interactor
        self.pos_repository = pos_repository

    def run(
        self,
        validate_window=True,   # type: Optional[bool]
    ):
        # type: (...) -> None
        if not self.interactor.start_stop_updater_service.has_genesis_root_path():
            self.interactor.start_stop_updater_service.update_start_stop()
            self.pos_repository.restart_hv()

        super(BinaryUpdaterManager, self).run()

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

        self.interactor.update_binaries(None, True)
