package com.github.slidekb.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Optional;

import com.github.slidekb.back.MainBack;
import com.github.slidekb.back.settings.GlobalSettings;
import com.github.slidekb.back.settings.PluginSettings;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class SettingsHelper {
    private static GlobalSettings settings = MainBack.getSettings();
    private static Gson gson = new GsonBuilder().setPrettyPrinting().create();

    public static void addProcess(String pluginID, String processName) {
        settings.getPlugins().computeIfAbsent(pluginID, key -> new PluginSettings()).getProcesses().add(processName);
        save();
    }

    public static void removeProcess(String pluginID, String processName) {
        Optional.ofNullable(settings.getPlugins().get(pluginID)).ifPresent(pluginSettings -> pluginSettings.getProcesses().remove(processName));
        save();
    }

    public static void addHotkey(String pluginID, String hotkey) {
        settings.getPlugins().computeIfAbsent(pluginID, key -> new PluginSettings()).getHotkeys().add(hotkey);
        save();
    }

    public static void removeHotkey(String pluginID, String hotkey) {
        Optional.ofNullable(settings.getPlugins().get(pluginID)).ifPresent(pluginSettings -> pluginSettings.getHotkeys().remove(hotkey));
        save();
    }

    private static void save() {
        try {
            File settingsFile = new File("settings.json");
            settingsFile.createNewFile();

            try (Writer writer = new FileWriter(settingsFile)) {
                gson.toJson(settings, writer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@@ -1438,24 +1438,437 @@
 e();%0A    %7D%0A%0A
+    public static void setAlwaysRun(String pluginID, boolean alwaysRun) %7B%0A        settings.getPlugins().computeIfAbsent(pluginID, key -%3E new PluginSettings()).setAlwaysRun(alwaysRun);%0A        save();%0A    %7D%0A%0A    public static void setUsedSlider(String pluginID, String sliderID) %7B%0A        settings.getPlugins().computeIfAbsent(pluginID, key -%3E new PluginSettings()).setUsedSlider(sliderID);%0A        save();%0A    %7D%0A%0A
     private 

set alwaysRun and usedSlider via SettingsHelper