import json
import os

from logging import Logger

from interactor.route.customization.get_styles.dto import GetStylesRequest
from model.config import Config
from model.customization_styles import CustomizationStyle
from validation_util import validate_api_key


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

        self.logger = logger
        self.config = config

    def execute(
        self,
        request     # type: GetStylesRequest
    ):
        # type: (...) -> CustomizationStyle

        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 CustomizationStyle({})

        config_file = open(self.config.totem_config_path, 'rb')
        data = json.load(config_file)
        style = data.get("style", {})

        return CustomizationStyle(style)
