from logging import Logger
from typing import List

from interactor.route.menu.get_navigation.dto import GetNavigationRequest
from model.config import Config
from model.error_type import ExternalServicesErrorsType
from model.exception import ExternalServiceErrors
from model.repository import PosRepositoryModel
from validation_util import validate_api_key


class GetNavigationInteractor(object):
    def __init__(
            self,
            logger,             # type: Logger
            config,             # type: Config
            pos_repository,     # type: PosRepositoryModel
            cache_navigation    # type: List
    ):
        # type: (...) -> None

        self.logger = logger
        self.config = config
        self.pos_repository = pos_repository
        self.cache_menu_navigation = cache_navigation

    def execute(
        self,
        request     # type: GetNavigationRequest
    ):
        # type: (...) -> dict

        pos_id = validate_api_key(
            request=request,
            secure_api_key_list=self.config.api_key_mapping
        )

        navigation = None
        if not self.cache_menu_navigation:
            navigation = self.pos_repository.get_navigation(pos_id)

        if navigation:
            self.cache_menu_navigation.append(navigation)

        if not self.cache_menu_navigation:
            raise ExternalServiceErrors(ExternalServicesErrorsType.MenuNavigationNotAvailable)

        return self.cache_menu_navigation[0]
