from os import path as _pth, mkdir as _mkdir
from json import load as _load, dump as _dump
CURRENT_DIR = _pth.abspath(_pth.dirname(__file__))
CONFIG_DIR = _pth.join(CURRENT_DIR, "config")
CONFIG_FILE = _pth.join(CONFIG_DIR, "config.json")
if not _pth.exists(CONFIG_DIR):
    _mkdir(CONFIG_DIR)

class JSONConfig(dict):
    def __init__(self, jsonpath):
        self.__path = jsonpath
        try:
            with open(jsonpath, "r") as f:
                super().__init__(_load(f))
        except:
            super().__init__()
    
    def commit(self):
        with open(self.__path, "w") as f:
            _dump(self, f, indent=2)

xray_manager_config = JSONConfig(_pth.join(CONFIG_DIR, "xray_manager.json"))
xray_config = JSONConfig(CONFIG_FILE)
xray_local_config = JSONConfig(_pth.join(CONFIG_DIR, "local.json"))
