from application.interactor import MediaUpdaterInteractor
from application.manager._UpdaterManager import UpdaterManager
from application.model import UpdateType
from application.model.configuration import Configurations
from application.model.customexception import NoUpdateFound
from application.repository.pos import (
    MediaUpdaterPOSRepository,
    BinaryUpdaterPOSRepository,
)
from application.util import LoggerUtil

logger = LoggerUtil.get_logger_name(__name__)


class MediaUpdaterManager(UpdaterManager):
    def __init__(
        self,
        configs,                # type: Configurations
        interactor,             # type: MediaUpdaterInteractor
        pos_repository,         # type: MediaUpdaterPOSRepository
        binaries_repository,    # type: BinaryUpdaterPOSRepository
    ):
        # type: (...) -> None # noqa

        name = UpdateType.media.name
        super(MediaUpdaterManager, self).__init__(name, configs, interactor, pos_repository, logger)
        self.interactor = interactor
        self.binaries_repository = binaries_repository

        self.logger = logger

    def run(self, validate_window=True):
        # type: (bool) -> None

        super(MediaUpdaterManager, self).run(False)

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

        self.interactor.update_media()

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

        if not self.binaries_repository.has_already_applied_update():
            raise NoUpdateFound()

        if not self.pos_repository.has_pending_update_to_apply():
            raise NoUpdateFound()

        if not self.pos_repository.has_pending_update_to_apply(UpdateType.banners.value):
            raise NoUpdateFound()
