"""
global settings has to be imported into all moduls
init should only be called once at GUI Startup
"""

import os
import json
import shutil
from utils.commonFunctions import *

PROGRAMM_VERSION = "0.1"

SETTINGS_DIR = "./Settings/"
SETTINGS_FILE = os.path.join(SETTINGS_DIR, "settings.json")

GEOMETRY_SETTINGS_DIR = os.path.join(SETTINGS_DIR, "autoSaves/")
GEOMETRY_SETTINGS_FILE = os.path.join(GEOMETRY_SETTINGS_DIR, "geometry_setup.json")

MACHINE_SETTINGS_DIR = os.path.join(SETTINGS_DIR, "machines/")
MACHINE_SETTINGS_FILE = os.path.join(MACHINE_SETTINGS_DIR, "Hermle_C32_U20230621_165905.json")

TOOL_SETTINGS_DIR = os.path.join(SETTINGS_DIR, "tools/")
TOOL_SETTINGS_FILE = os.path.join(TOOL_SETTINGS_DIR, "test20230607_145527.json")

PROCESSING_SETTINGS_DIR = os.path.join(SETTINGS_DIR, "autoSaves/")
PROCESSING_SETTINGS_FILE = os.path.join(PROCESSING_SETTINGS_DIR, "processing_setup.json")

LANGUAGE_PACK_DEFAULT = "./languagePacks/german.json"
SETTINGS_FILE_DEFAULT = "./exampleConfigFiles/settings.json"
MACHINE_SETTINGS_FILE_DEFAULT = "./exampleConfigFiles/machine.json"
TOOL_SETTINGS_FILE_DEFAULT = "./exampleConfigFiles/tool.json"
GEOMETRY_SETTINGS_FILE_DEFAULT = "./exampleConfigFiles/geometry_setup.json"
PROCESSING_SETTINGS_FILE_DEFAULT = "./exampleConfigFiles/processing_setup.json"

SETTINGS_ADVANCED = "./exampleConfigFiles/advanced_settings.json"
# SETTINGS_PROGRAMMER = "./exampleConfigFiles/programmer_settings.json"
# SETTINGS_MECHANIC = "./exampleConfigFiles/mechanic_settings.json"

PATH_LANGUAGE_PACK = os.path.join(SETTINGS_DIR, "languagePacks/")
PATH_TOOL_DEFAULT = TOOL_SETTINGS_DIR
PATH_MACHINE_DEFAULT = MACHINE_SETTINGS_DIR
PATH_PROCESSING_DEFAULT = PROCESSING_SETTINGS_DIR
PATH_AUTO_SAVES_DEFAULT = PROCESSING_SETTINGS_DIR

def init() -> None:
    """Init the Global Settings, should only be called once at GUI Startup"""
    if not os.path.isdir(SETTINGS_DIR):
        os.mkdir(SETTINGS_DIR)
    if not os.path.isdir(PATH_LANGUAGE_PACK):
        os.mkdir(PATH_LANGUAGE_PACK)
        if os.path.isdir("./languagePacks/"):
            for f in os.listdir("./languagePacks/"):
                filePath = "./languagePacks/" + f
                if os.path.isfile(filePath) and filePath.endswith('.json'):
                    shutil.copyfile(filePath, os.path.join(PATH_LANGUAGE_PACK, f))

    if not os.path.isdir(PATH_TOOL_DEFAULT):
        os.mkdir(PATH_TOOL_DEFAULT)
    if not os.path.isdir(PATH_MACHINE_DEFAULT):
        os.mkdir(PATH_MACHINE_DEFAULT)
    if not os.path.isdir(PATH_AUTO_SAVES_DEFAULT):
        os.mkdir(PATH_AUTO_SAVES_DEFAULT)

    if not os.path.isfile(SETTINGS_FILE):
        shutil.copyfile(SETTINGS_FILE_DEFAULT, SETTINGS_FILE)

    if not os.path.isfile(GEOMETRY_SETTINGS_FILE):
        shutil.copyfile(GEOMETRY_SETTINGS_FILE_DEFAULT, GEOMETRY_SETTINGS_FILE)

    if not os.path.isfile(MACHINE_SETTINGS_FILE):
        shutil.copyfile(MACHINE_SETTINGS_FILE_DEFAULT, MACHINE_SETTINGS_FILE)

    if not os.path.isfile(TOOL_SETTINGS_FILE):
        shutil.copyfile(TOOL_SETTINGS_FILE_DEFAULT, TOOL_SETTINGS_FILE)

    if not os.path.isfile(PROCESSING_SETTINGS_FILE):
        shutil.copyfile(PROCESSING_SETTINGS_FILE_DEFAULT, PROCESSING_SETTINGS_FILE)

    with open(SETTINGS_FILE_DEFAULT, 'r') as f:
        global GUI_SETTINGS
        GUI_SETTINGS = json.load(f)

    with open(SETTINGS_FILE, 'r') as f:
        try:
            tmpConfig = json.load(f)
            for (key, value) in tmpConfig.items():
                if isinstance(value, dict):
                    GUI_SETTINGS[key].update(value)
                else:
                    GUI_SETTINGS.update({key: value})
        except:
            pass

        global LANGUAGE_SETTINGS
        LANGUAGE_SETTINGS = __languageSettings(GUI_SETTINGS["languagePack"])

def writeSettings() -> None:
    """write the settings to the local settings file"""
    with open(SETTINGS_FILE, 'w') as f:
        json.dump(GUI_SETTINGS, f, indent=4)

def loadUserRole(user: str):
    """Load the user role specified by 'user' into the settings file"""
    if user == "advanced":
        with open(SETTINGS_ADVANCED, 'r') as f:
            tmpConfig = json.load(f)
            GUI_SETTINGS.update(tmpConfig)
            writeSettings()
    return

class __languageSettings():
    def __init__(self, languagePack: str):
        """Load a specified by the string of the languagePack location"""
        self.changeLanguage(languagePack)

    def changeLanguage(self, languagePack: str):
        """Load a specified by the string of the languagePack location"""
        with open(LANGUAGE_PACK_DEFAULT, 'r', encoding="utf-8") as f:
            self.languagePack = json.load(f)

        if os.path.isfile(languagePack):
            with open(languagePack, 'r', encoding="utf-8") as f:
                try:
                    language = json.load(f)
                    self.languagePack = mergeDicts(self.languagePack, language)
                except:
                    pass
