package net.halbear.aotg.utility.DataGenerators;

import net.halbear.aotg.AgeoftheGods;
import net.halbear.aotg.custom.WorldGen.ModNoiseSettings;
import net.halbear.aotg.registries.ModBiomeModifiers;
import net.halbear.aotg.registries.ModConfiguredFeatures;
import net.halbear.aotg.registries.ModPlacedFeatures;
import net.minecraft.core.HolderGetter;
import net.minecraft.core.RegistrySetBuilder;
import net.minecraft.core.registries.Registries;
import net.minecraft.data.DataGenerator;
import net.minecraft.data.PackOutput;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.levelgen.DensityFunction;
import net.minecraft.world.level.levelgen.NoiseGeneratorSettings;
import net.minecraft.world.level.levelgen.synth.NormalNoise;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.common.data.DatapackBuiltinEntriesProvider;
import net.neoforged.neoforge.data.event.GatherDataEvent;
import net.neoforged.neoforge.registries.NeoForgeRegistries;

import java.util.Set;

import static net.halbear.aotg.AgeoftheGods.LOGGER;
import static net.halbear.aotg.AgeoftheGods.MODID;

@EventBusSubscriber(modid = MODID)
public class AgeOfTheGodsDataGeneration {
    public static final ResourceKey<net.minecraft.world.level.levelgen.synth.NormalNoise.NoiseParameters> TINY_PATCHY_NOISE =
            ResourceKey.create(Registries.NOISE, Identifier.fromNamespaceAndPath(AgeoftheGods.MODID, "tiny_patchy_noise"));
    public static final ResourceKey<net.minecraft.world.level.levelgen.synth.NormalNoise.NoiseParameters> PATH_NOISE =
            ResourceKey.create(Registries.NOISE, Identifier.fromNamespaceAndPath(AgeoftheGods.MODID, "path_noise"));

    @SubscribeEvent
    public static void gatherData(GatherDataEvent.Client event) {
        LOGGER.info("gatherData event");
        RegistrySetBuilder builder = new RegistrySetBuilder().
                add(Registries.NOISE, context -> {
                    context.register(TINY_PATCHY_NOISE, new net.minecraft.world.level.levelgen.synth.NormalNoise.NoiseParameters(
                            0,
                            1.0, 1.0, 1.0
                    ));
                }).add(Registries.NOISE_SETTINGS, context -> {
                    HolderGetter<DensityFunction> densityFunctions = context.lookup(Registries.DENSITY_FUNCTION);
                    HolderGetter<NormalNoise.NoiseParameters> noiseParameters = context.lookup(Registries.NOISE);

                    NoiseGeneratorSettings vanillaSettings = NoiseGeneratorSettings.overworld(context, false, false);

                    context.register(
                            ResourceKey.create(Registries.NOISE_SETTINGS, Identifier.withDefaultNamespace("overworld")),
                            new NoiseGeneratorSettings(
                                    vanillaSettings.noiseSettings(),
                                    Blocks.STONE.defaultBlockState(),
                                    Blocks.WATER.defaultBlockState(),
                                    vanillaSettings.noiseRouter(),
                                    ModNoiseSettings.makeRules(context),
                                    vanillaSettings.spawnTarget(),
                                    63,
                                    false,
                                    true,
                                    true,
                                    false
                            )
                    );
                })
                .add(Registries.CONFIGURED_FEATURE, ModConfiguredFeatures::bootstrap)
                .add(Registries.PLACED_FEATURE, ModPlacedFeatures::bootstrap) // Add this
                .add(NeoForgeRegistries.Keys.BIOME_MODIFIERS, ModBiomeModifiers::bootstrap);;
        LOGGER.info("World Gen something or other");
        event.createProvider(output -> new DatapackBuiltinEntriesProvider(
                output,
                event.getLookupProvider(),
                builder,
                Set.of("minecraft", AgeoftheGods.MODID)
        ));
    }

    @SubscribeEvent
    public static void GatherClientData(GatherDataEvent.Client event){
        DataGenerator generator = event.getGenerator();
        PackOutput output = generator.getPackOutput();

     //   generator.addProvider(true, new AOTGModelProvider(output));
    }
}
