from datetime import datetime

from application.interactor import UserUpdaterInteractor
from application.model import UpdateType
from application.model.configuration import Configurations
from application.model.customexception import NoUpdateFound
from application.repository.pos import (
    UserUpdaterPOSRepository,
    BinaryUpdaterPOSRepository,
)
from application.util import LoggerUtil

from ._UpdaterManager import UpdaterManager

logger = LoggerUtil.get_logger_name(__name__)


class UserUpdaterManager(UpdaterManager):
    def __init__(
        self,
        configs,                # type: Configurations
        interactor,             # type: UserUpdaterInteractor
        pos_repository,         # type: UserUpdaterPOSRepository
        binaries_repository,    # type: BinaryUpdaterPOSRepository
    ):
        # type: (...) -> None

        name = UpdateType.user.name
        super(UserUpdaterManager, self).__init__(name, configs, interactor, pos_repository, logger)
        self.logger = logger
        self.interactor = interactor
        self.binaries_repository = binaries_repository

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

        self.interactor.update_users([])

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

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

        last_update_date = self.pos_repository.get_last_user_update_date()
        today = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
        update_to_apply = last_update_date is None or last_update_date < today
        if not update_to_apply:
            raise NoUpdateFound()
