#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from Interfaces.OpoTestsInterface import OpoTestsInterface
from typing import Any, Optional, Self
from Utils.Utils import Utils

class SettingsManager:

    DEFAULT_SETTINGS:dict[str, Any|None] = {
        "default_settings_files" : ["/JSON/OpoTests.settings.json"], 
        "default_secrets_files" : ["/JSON/OpoTests.secrets.json"]
    }
    
    def __init__(self:Self, 
        ot:OpoTestsInterface, 
        inputs:Optional[dict[str, Any|None]|list[Any|None]|tuple[Any|None]] = None
    ) -> None:
        self.ot:OpoTestsInterface = ot
        self.__customs:dict[str, Any|None] = Utils.get_dictionary(inputs)
        self.__settings:dict[str, Any|None] = {}
        self.__secrets:dict[str, Any|None] = {}

    def start(self:Self) -> None:

        key:str

        for key in ("default_settings_files", "default_settings", "settings_files", "settings"):
            self.add(self.get(key), True)
        for key in ("default_secrets_files", "default_secrets", "secrets_files", "secrets"):
            self.add_secrets(self.get(key), True)

    def add(self:Self, inputs:dict[str, Any|None]|str|list|tuple, overwrite:bool = False) -> None:

        set:dict[str, Any|None]

        for set in self.ot.load_json_data(inputs):

            key:str
            value:Any|None

            for key, value in set.items():
                if overwrite or key not in self.__settings:
                    self.__settings[key] = value

    def add_secrets(self:Self, inputs:dict[str, Any|None]|str|list|tuple, overwrite:bool = False) -> None:

        set:dict[str, Any|None]

        for set in self.ot.load_json_data(inputs):

            key:str
            value:Any|None

            for key, value in set.items():
                if overwrite or key not in self.__secrets:
                    self.__secrets[key] = value

    def get(self:Self, keys:str|list|tuple, inputs:Optional[dict[str, Any|None]|list[Any|None]|tuple[Any|None]] = None, _default:Optional[Any] = None) -> Any|None:
        return Utils.get_values(keys, (inputs, self.__customs, self.__secrets, self.__settings, self.DEFAULT_SETTINGS), _default)