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

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

class I18NManager:

    DEFAULT_SETTINGS:dict[str, Any|None] = {}
    
    def __init__(self:Self, ot:OpoTestsInterface) -> None:
        self.ot:OpoTestsInterface = ot
        self.__sentences:dict[str, dict[str, str|list[str]|tuple[str]]] = {}
        self.__default_language:str = "espanol"
        self.__language_selected:str = "espanol"
    
    def start(self:Self) -> None:
        
        key:str

        self.__default_language = self.ot.settings.get(("default_language", "i18n_default_language"), None, self.__default_language)
        self.__language_selected = self.ot.settings.get(("language_selected", "i18n_language_selected"), None, self.__language_selected)

        for key in ("default_i18n_files", "default_i18n", "i18n_files", "i18n"):
            self.add(self.ot.settings.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):

            language:str
            sentences:dict[str, str|list|tuple|None]

            for language, sentences in set.items():
                if Check.is_dictionary(sentences):

                    key:str
                    text:str|list[str]|tuple[str]|None

                    if language not in self.__sentences:
                        self.__sentences[language] = {}
                    for key, text in sentences.items():
                        if overwrite or key not in self.__sentences[language]:
                            self.__sentences[language][key] = text

    def get(self:Self, texts:str|list|tuple, inputs:Optional[dict[str, Any|None]|list[Any|None]|tuple[Any|None]] = None, _default:Optional[Any] = None) -> str|None:
        
        keys:list[str] = Utils.get_keys(texts)
        text:str|tuple[str]|list[str]|None = None

        if len(keys):
            
            used:tuple[str] = tuple()
            language:str

            for language in [self.__language_selected, self.__default_language] + list(self.__sentences.keys()):
                if language not in used:
                    used += (language,)
                    if language in self.__sentences:
                        for key in keys:
                            if key in self.__sentences[language] and self.__sentences[language][key] is not None:
                                text = self.__sentences[language][key]
                                break
                if text is not None:
                    break

        if text is None:

            strings:list[str] = Utils.get_strings(texts)

            text = strings[0] if _default == None and len(strings) else _default

        return Utils.string_variables((
            "".join(text) if Check.is_array(text) else 
        text), inputs, _default) if text else text