package com.mingliqiye.minecraft.enchantment.conflict.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mojang.logging.LogUtils;
import net.fabricmc.loader.api.FabricLoader;
import org.slf4j.Logger;

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

import static com.mingliqiye.minecraft.enchantment.conflict.Mod.MOD_ID;

public class ModConfig {
    private static final Path CONFIG_PATH =
            FabricLoader.getInstance().getConfigDir().resolve(MOD_ID + ".json");
    private static final Gson GSON =
            new GsonBuilder().setPrettyPrinting().serializeNulls().create();

    private static ModConfig instance;

    private boolean allowDamageEnchantment = true;
    private boolean allowInfinityEnchantment = true;
    private boolean allowPiercingEnchantment = true;
    private boolean allowProtectionEnchantment = true;
    private static final Logger LOGGER = LogUtils.getLogger();

    public ModConfig() {
    }

    public static ModConfig getInstance() {
        return ModConfig.instance;
    }

    public static void setInstance(ModConfig instance) {
        ModConfig.instance = instance;
    }

    public static ModConfig load() {
        try {
            if (Files.exists(CONFIG_PATH)) {
                return GSON.fromJson(Files.newBufferedReader(CONFIG_PATH),
                        ModConfig.class);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        ModConfig modConfig = new ModConfig();
        setInstance(modConfig);
        save();
        return modConfig;
    }

    public static void save() {
        try {
            Files.writeString(CONFIG_PATH, GSON.toJson(instance));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static {
        setInstance(load());
    }

    public boolean isAllowDamageEnchantment() {
        return allowDamageEnchantment;
    }

    public void setAllowDamageEnchantment(boolean allowDamageEnchantment) {
        this.allowDamageEnchantment = allowDamageEnchantment;
    }

    public boolean isAllowInfinityEnchantment() {
        return allowInfinityEnchantment;
    }

    public void setAllowInfinityEnchantment(boolean allowInfinityEnchantment) {
        this.allowInfinityEnchantment = allowInfinityEnchantment;
    }

    public boolean isAllowPiercingEnchantment() {
        return allowPiercingEnchantment;
    }

    public void setAllowPiercingEnchantment(boolean allowPiercingEnchantment) {
        this.allowPiercingEnchantment = allowPiercingEnchantment;
    }


    public String toString() {
        return this.getClass().getName() + "(" + "allowDamageEnchantment" +
                "=" + allowDamageEnchantment + "," +
                "allowInfinityEnchantment" + "=" + allowInfinityEnchantment +
                "," + "allowPiercingEnchantment" + "=" +
                allowPiercingEnchantment + "," +
                "allowProtectionEnchantment" + "=" +
                allowProtectionEnchantment + ")";
    }

    public boolean isAllowProtectionEnchantment() {
        return allowProtectionEnchantment;
    }

    public void setAllowProtectionEnchantment(boolean allowProtectionEnchantment) {
        this.allowProtectionEnchantment = allowProtectionEnchantment;
    }
}
