package dev.micle.firefly_bush_backport.config;

import dev.micle.firefly_bush_backport.FireflyBushBackport;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.event.config.ModConfigEvent;
import org.apache.commons.lang3.tuple.Pair;

@Mod.EventBusSubscriber(modid = FireflyBushBackport.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
public final class Config {
    public static final Client CLIENT;
    public static final ForgeConfigSpec CLIENT_SPEC;
    public static final Common COMMON;
    public static final ForgeConfigSpec COMMON_SPEC;
    public static final Server SERVER;
    public static final ForgeConfigSpec SERVER_SPEC;

    static {
        Pair<Client, ForgeConfigSpec> clientSpecPair = new ForgeConfigSpec.Builder().configure(Client::new);
        CLIENT = clientSpecPair.getLeft();
        CLIENT_SPEC = clientSpecPair.getRight();

        Pair<Common, ForgeConfigSpec> commonSpecPair = new ForgeConfigSpec.Builder().configure(Common::new);
        COMMON = commonSpecPair.getLeft();
        COMMON_SPEC = commonSpecPair.getRight();

        Pair<Server, ForgeConfigSpec> serverSpecPair = new ForgeConfigSpec.Builder().configure(Server::new);
        SERVER = serverSpecPair.getLeft();
        SERVER_SPEC = serverSpecPair.getRight();
    }

    public static void register() {
        FireflyBushBackport.getFMLJavaModLoadingContext().registerConfig(ModConfig.Type.CLIENT, CLIENT_SPEC);
        FireflyBushBackport.getFMLJavaModLoadingContext().registerConfig(ModConfig.Type.COMMON, COMMON_SPEC);
        FireflyBushBackport.getFMLJavaModLoadingContext().registerConfig(ModConfig.Type.SERVER, SERVER_SPEC);
    }

    @SubscribeEvent
    public static void onConfigReloadEvent(ModConfigEvent event) {
        if (event.getConfig().getSpec() == CLIENT_SPEC) {
            Client.onConfigReload();
        } else if (event.getConfig().getSpec() == COMMON_SPEC) {
            Common.onConfigReload();
        } else if (event.getConfig().getSpec() == SERVER_SPEC) {
            Server.onConfigReload();
        }
    }

    public static class Client {
        public static ForgeConfigSpec.DoubleValue bushFireflyChancePerTick;
        public static ForgeConfigSpec.DoubleValue bushFireflyHorizontalRange;
        public static ForgeConfigSpec.DoubleValue bushFireflyVerticalRange;
        public static ForgeConfigSpec.IntValue bushFireflySpawnMaxBrightnessLevel;
        public static ForgeConfigSpec.IntValue bushFireflyAmbientSoundChanceOneIn;
        public static ForgeConfigSpec.DoubleValue bushFireflyAmbientSoundVolume;
        public static ForgeConfigSpec.DoubleValue bushFireflyAmbientSoundPitch;
        public static ForgeConfigSpec.IntValue bushFireflyLightLevel;

        Client(ForgeConfigSpec.Builder builder) {
            builder.comment("Official settings for the firefly bush.").push("bush");
            bushFireflyChancePerTick = builder
                    .defineInRange("bushFireflyChancePerTick", 0.7, 0, 1);
            bushFireflyHorizontalRange = builder
                    .defineInRange("bushFireflyHorizontalRange", 10.0, 0, Double.MAX_VALUE);
            bushFireflyVerticalRange = builder
                    .defineInRange("bushFireflyVerticalRange", 5.0, 0, Double.MAX_VALUE);
            bushFireflySpawnMaxBrightnessLevel = builder
                    .defineInRange("bushFireflySpawnMaxBrightnessLevel", 13, 0, Level.MAX_BRIGHTNESS);
            bushFireflyAmbientSoundChanceOneIn = builder
                    .defineInRange("bushFireflyAmbientSoundChanceOneIn", 30, 0, Integer.MAX_VALUE);
            bushFireflyAmbientSoundVolume = builder
                    .defineInRange("bushFireflyAmbientSoundVolume", 1.0, 0, Double.MAX_VALUE);
            bushFireflyAmbientSoundPitch = builder
                    .defineInRange("bushFireflyAmbientSoundPitch", 1.0, 0, Double.MAX_VALUE);
            bushFireflyLightLevel = builder
                    .comment("NOTE: This option requires a restart!")
                    .defineInRange("bushFireflyLightLevel", 2, 0, Level.MAX_BRIGHTNESS);
            builder.pop();
        }

        private static void onConfigReload() {}
    }

    public static class Common {
        Common(ForgeConfigSpec.Builder builder) {}

        private static void onConfigReload() {}
    }

    public static class Server {
        Server(ForgeConfigSpec.Builder builder) {}

        private static void onConfigReload() {}
    }
}
