package de.winniepat.citrus.draggui;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import de.winniepat.citrus.Client;
import net.fabricmc.loader.api.FabricLoader;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class HudConfig {
    private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
    private static final File CUSTOM_DIR = new File(FabricLoader.getInstance().getGameDir().toFile(), "citrus");
    private static final File FILE = new File(CUSTOM_DIR, "hud.json");

    public int fpsX = 10, fpsY = 10;
    public int pingX = 10, pingY = 25;
    public int serverX = 10, serverY = 40;

    public int keyX = 10, keyY = 60;
    public int armorX = 10, armorY = 120;
    public int potionX = 10, potionY = 180;

    public int fpsColor = 0xFFFFFF;
    public int pingColor = 0xFFFFFF;
    public int serverColor = 0xFFFFFF;
    public int keyColor = 0xFFFFFF;
    public int armorColor = 0xFFFFFF;
    public int potionColor = -1; // -1 ist einfach rot, wenn schlecht und grün, wenn gut

    public float uiScale = 1.0f;

    public boolean fpsEnabled = true;
    public boolean pingEnabled = true;
    public boolean serverEnabled = true;
    public boolean keyEnabled = true;
    public boolean armorEnabled = true;
    public boolean potionEnabled = true;

    public boolean isModuleEnabled(String label) {
        return switch (label) {
            case "FPS" -> fpsEnabled;
            case "Ping" -> pingEnabled;
            case "Server" -> serverEnabled;
            case "Keys" -> keyEnabled;
            case "Armor" -> armorEnabled;
            case "Potion" -> potionEnabled;
            default -> false;
        };
    }

    public static HudConfig INSTANCE = new HudConfig();

    public static void load() {
        if (!CUSTOM_DIR.exists()) CUSTOM_DIR.mkdirs();
        if (FILE.exists()) {
            try (FileReader reader = new FileReader(FILE)) {
                INSTANCE = GSON.fromJson(reader, HudConfig.class);
                Client.debugLog("Loaded from " + FILE.getAbsolutePath());
            } catch (Exception e) {
                Client.logger.error("Error while loading config", e);
            }
        }
    }

    public static void save() {
        try {
            if (!CUSTOM_DIR.exists()) CUSTOM_DIR.mkdirs();
            try (FileWriter writer = new FileWriter(FILE)) {
                GSON.toJson(INSTANCE, writer);
            }
        } catch (Exception e) {
            Client.logger.error("Error while saving config", e);
        }
    }

}