package com.straice.smoothdoors.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.fabricmc.loader.api.FabricLoader;

import java.nio.file.Files;
import java.nio.file.Path;

public final class SddConfigManager {
    private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
    private static final Path PATH = FabricLoader.getInstance().getConfigDir().resolve("smooth-double-doors.json");

    private static SddConfig config = new SddConfig();

    private SddConfigManager() {}

    public static SddConfig get() { return config; }

    public static void load() {
        try {
            if (Files.exists(PATH)) {
                String json = Files.readString(PATH);
                SddConfig read = GSON.fromJson(json, SddConfig.class);
                if (read != null) config = read;
            } else {
                save();
            }
        } catch (Exception ignored) {
            // si falla, usamos defaults
        }
    }

    public static void save() {
        try {
            Files.createDirectories(PATH.getParent());
            Files.writeString(PATH, GSON.toJson(config));
        } catch (Exception ignored) {}
    }
}
