import * as fs from "fs";
import path from "path";

export type SETTINGS =
    "setupDone"
    | "secondsPer100ml"
    | "arduino_proxy_enabled"
    | "led_enabled"
    | "remote_enabled"
    | "hotspot_enabled"
    | "led_gpio"
    | "ambient_color";

export class Settings {
    private static _setupDone: boolean;

    static get setupDone(): boolean {
        return this._setupDone;
    }

    static set setupDone(value: boolean) {
        this._setupDone = value;
        this.set("setupDone", value);
    }

    private static _json: {};

    static get json(): {} {
        return this._json;
    }

    public static loadSettings() {
        if (!fs.existsSync(path.join(__dirname, "../config.json"))) {
            this._setupDone = false;
            fs.writeFileSync(path.join(__dirname, "../config.json"), `{"setupDone":false}`);
        }

        let file = fs.readFileSync(path.join(__dirname, "../config.json"));
        this._json = JSON.parse(file.toString("utf8"));

        this._setupDone = this._json["setupDone"];
    }

    public static saveSettings() {
        fs.writeFileSync(path.join(__dirname, "../config.json"), JSON.stringify(this._json, null, 1));
    }

    public static get(key: SETTINGS): any {
        return this._json[key];
    }

    public static set(key: string, value: any) {
        this._json[key] = value;
        this.saveSettings();
    }
}