"""
*  ____           _
* |  _ \ ___   __| |_ __ _   _ _ __ ___
* | |_) / _ \ / _` | '__| | | | '_ ` _ \
* |  __/ (_) | (_| | |  | |_| | | | | | |
* |_|   \___/ \__,_|_|   \__,_|_| |_| |_|
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
"""

import os
import glob
import json

from podrum.utils.TextFormat import TextFormat
from podrum.wizard import Wizard

class Base:

    """
    getLangFiles and getLangNames are 
    for the wizard's use
    """

    langsList = [0]

    def getLangFiles():
        path = Wizard.ServerFS.getLangDir()
        allFiles = glob.glob(path + '/*.json')
        for file in allFiles:
            with open(f'{file}', 'r', encoding="utf8") as langFiles:
                data = json.load(langFiles)
                if "langName" in data:
                    name = data['langName']
                else:
                    name = 'Unknown name'
            langs = str(file.replace(path, '').replace("\\", "").replace('.json', '').replace("/", ""))
            Base.langsList.append(langs)
            print(f'[{langs}] -> {name}')

    def getLangNames(dir):
        return Base.langsList

    """
    A translation from the selected 
    language is returned
    """
    def get(string):
        if Wizard.Wizard.isInWizard == True:
            path = Wizard.ServerFS.getLangDir()
            with open(f'{path}/{Wizard.Wizard.options[0]}.json', 'r', encoding="utf8") as lF:
                data = json.load(lF)
                if string in data:
                    return data[string]
                else:
                    print(f"The value '{string}' does not exists in this language.")
        else:
            if not Wizard.ServerFS.checkForFile(os.getcwd(), "server.json") or os.path.getsize(os.getcwd() + "/server.json") == 0:
                with open(Wizard.ServerFS.getLangDir() + "/en.json", 'r', encoding='utf-8') as lF:
                    data = json.load(lF)
                    if string in data:
                        return data[string]
                    else:
                        return print(f"{TextFormat.RED}The value '{string}' does not exists in this language")
            serverConfig = os.getcwd() + "/server.json"
            with open(f'{serverConfig}', 'r', encoding="utf8") as serverCfg:
                pref = json.load(serverCfg)
                if "Language" in pref:
                    lang = pref["Language"]
                else:
                    print(f"{TextFormat.RED}Language not found in server.json")
            path = Wizard.ServerFS.getLangDir()
            with open(f'{path}/{lang}.json', 'r', encoding="utf8") as lF:
                data = json.load(lF)
                if string in data:
                    return data[string]
                else:
                    print(f"{TextFormat.RED}The value '{string}' does not exists in this language")
