import os
import json
from logging import Logger
from typing import Dict

from interactor.route.customization.get_custom_images.dto import GetCustomImagesRequest
from model.config import Config
from model.custom_images import CustomImages
from validation_util import validate_api_key


class GetCustomImagesInteractor(object):
    def __init__(
        self,
        logger,    # type: Logger
        config,    # type: Config
    ):
        # type: (...) -> None

        self.logger = logger
        self.config = config

    def execute(
        self,
        request,    # type: GetCustomImagesRequest
    ):
        # type: (...) -> Dict

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

        if not self.config.totem_config_path or not os.path.exists(self.config.totem_config_path):
            return CustomImages({})

        with open(self.config.totem_config_path, 'rb') as f:
            data = json.load(f)

        custom_images = {}
        for custom_image in data.get('customImages'):
            key = custom_image.keys()[0]
            custom_images[key] = custom_image.get(key)
        return custom_images
