# -*- coding: utf-8 -*-

from application.model.customexception import NoUpdateFound, DisabledFunctionality
from application.repository.pos import CatalogUpdaterPOSRepository
from application.service import UpdaterService
from application.util import LoggerUtil
from helper import remove_accents

logger = LoggerUtil.get_logger_name(__name__)


class GetCatalogVersionToApplyInteractor:
    def __init__(self, pos_repo, updater_service, enabled):
        # type: (CatalogUpdaterPOSRepository, UpdaterService, bool) -> None
        self.pos_repo = pos_repo
        self.updater_service = updater_service
        self.enabled = enabled

    def get_catalog_version_to_apply(self, need_backoffice_json):
        if not self.enabled:
            raise DisabledFunctionality()

        downloaded_version_name = self.updater_service.download_campaign_pending_version(need_backoffice_json)
        if not downloaded_version_name:
            logger.info("No updates available")
            raise NoUpdateFound()

        downloaded_version_name = remove_accents(downloaded_version_name)
        logger.info("Available update version {} to apply".format(downloaded_version_name))
        return downloaded_version_name
