package net.halbear.aotg.registries;

import net.halbear.aotg.AgeoftheGods;
import net.minecraft.core.HolderGetter;
import net.minecraft.core.HolderSet;
import net.minecraft.core.registries.Registries;
import net.minecraft.data.worldgen.BootstrapContext;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.Biomes;
import net.minecraft.world.level.levelgen.GenerationStep;
import net.minecraft.world.level.levelgen.placement.PlacedFeature;
import net.neoforged.neoforge.common.world.BiomeModifier;
import net.neoforged.neoforge.common.world.BiomeModifiers;
import net.neoforged.neoforge.registries.NeoForgeRegistries;

import static net.halbear.aotg.registries.ModPlacedFeatures.*;

public class ModBiomeModifiers {
    public static final ResourceKey<BiomeModifier> ADD_TWISTY_TREE =
            ResourceKey.create(NeoForgeRegistries.Keys.BIOME_MODIFIERS, Identifier.fromNamespaceAndPath(AgeoftheGods.MODID, "add_twisty_tree"));
    public static final ResourceKey<BiomeModifier> ADD_SHORT_TWISTY_TREE =
            ResourceKey.create(NeoForgeRegistries.Keys.BIOME_MODIFIERS, Identifier.fromNamespaceAndPath(AgeoftheGods.MODID, "add_short_twisty_tree"));
    public static final ResourceKey<BiomeModifier> ADD_TWISTY_BUSH_TREE =
            ResourceKey.create(NeoForgeRegistries.Keys.BIOME_MODIFIERS, Identifier.fromNamespaceAndPath(AgeoftheGods.MODID, "add_twisty_bush_tree"));
    public static final ResourceKey<BiomeModifier> ADD_TALL_THIN_TWISTY_TREE =
            ResourceKey.create(NeoForgeRegistries.Keys.BIOME_MODIFIERS, Identifier.fromNamespaceAndPath(AgeoftheGods.MODID, "add_tall_thin_twisty_tree"));
    public static final ResourceKey<BiomeModifier> ADD_SHORT_THIN_TWISTY_TREE =
            ResourceKey.create(NeoForgeRegistries.Keys.BIOME_MODIFIERS, Identifier.fromNamespaceAndPath(AgeoftheGods.MODID, "add_short_thin_twisty_tree"));
    public static final ResourceKey<BiomeModifier> GRANITE_BOULDER_BIOME_MODIFIER = ResourceKey.create(
            NeoForgeRegistries.Keys.BIOME_MODIFIERS, Identifier.fromNamespaceAndPath(AgeoftheGods.MODID, "add_granite_boulder"));
    public static final ResourceKey<BiomeModifier> GRAVEL_BOULDER_BIOME_MODIFIER = ResourceKey.create(
            NeoForgeRegistries.Keys.BIOME_MODIFIERS, Identifier.fromNamespaceAndPath(AgeoftheGods.MODID, "add_gravel_boulder"));
    public static final ResourceKey<BiomeModifier> SHALE_BOULDER_BIOME_MODIFIER = ResourceKey.create(
            NeoForgeRegistries.Keys.BIOME_MODIFIERS, Identifier.fromNamespaceAndPath(AgeoftheGods.MODID, "add_shale_boulder"));
    public static final ResourceKey<BiomeModifier> LIMESTONE_BOULDER_BIOME_MODIFIER = ResourceKey.create(
            NeoForgeRegistries.Keys.BIOME_MODIFIERS, Identifier.fromNamespaceAndPath(AgeoftheGods.MODID, "limestone_shale_boulder"));


    public static void bootstrap(BootstrapContext<BiomeModifier> context) {
        HolderGetter<PlacedFeature> placedFeatures = context.lookup(Registries.PLACED_FEATURE);
        HolderGetter<Biome> biomes = context.lookup(Registries.BIOME);

        var placedTreeHolder = placedFeatures.getOrThrow(ModPlacedFeatures.SHORT_TWISTY_TREE_PLACED_KEY);

        var targetBiomes = HolderSet.direct(biomes.getOrThrow(ModBiomes.MAQUIS_SHRUBLAND));

        context.register(ADD_SHORT_TWISTY_TREE, new BiomeModifiers.AddFeaturesBiomeModifier(
                targetBiomes,
                HolderSet.direct(placedTreeHolder),
                GenerationStep.Decoration.VEGETAL_DECORATION
        ));


        var placedLookup = context.lookup(Registries.PLACED_FEATURE);
        var placedHolder = placedLookup.getOrThrow(GRANITE_BOULDER_PLACED);

        var modifier = new BiomeModifiers.AddFeaturesBiomeModifier(
                targetBiomes,
                HolderSet.direct(placedHolder),
                GenerationStep.Decoration.VEGETAL_DECORATION
        );

        context.register(GRANITE_BOULDER_BIOME_MODIFIER, modifier);
        placedHolder = placedLookup.getOrThrow(SHALE_BOULDER_PLACED);

         modifier = new BiomeModifiers.AddFeaturesBiomeModifier(
                targetBiomes,
                HolderSet.direct(placedHolder),
                GenerationStep.Decoration.VEGETAL_DECORATION
        );
        context.register(SHALE_BOULDER_BIOME_MODIFIER, modifier);
        placedHolder = placedLookup.getOrThrow(GRAVEL_BOULDER_PLACED);

         modifier = new BiomeModifiers.AddFeaturesBiomeModifier(
                targetBiomes,
                HolderSet.direct(placedHolder),
                GenerationStep.Decoration.VEGETAL_DECORATION
        );
        context.register(GRAVEL_BOULDER_BIOME_MODIFIER, modifier);
        targetBiomes = HolderSet.direct(biomes.getOrThrow(ModBiomes.GARRIGUE), biomes.getOrThrow(ModBiomes.MEDITERRANEAN_FOREST));
        placedHolder = placedLookup.getOrThrow(LIMESTONE_BOULDER_PLACED);

        modifier = new BiomeModifiers.AddFeaturesBiomeModifier(
                targetBiomes,
                HolderSet.direct(placedHolder),
                GenerationStep.Decoration.VEGETAL_DECORATION
        );
        context.register(LIMESTONE_BOULDER_BIOME_MODIFIER, modifier);
        placedTreeHolder = placedFeatures.getOrThrow(SHORT_GARRIGUE_TWISTY_TREE_PLACED_KEY);
        context.register(ADD_TWISTY_BUSH_TREE, new BiomeModifiers.AddFeaturesBiomeModifier(
                targetBiomes,
                HolderSet.direct(placedTreeHolder),
                GenerationStep.Decoration.VEGETAL_DECORATION
        ));

        placedTreeHolder = placedFeatures.getOrThrow(TALL_THIN_TWISTY_TREE_PLACED_KEY);

        targetBiomes = HolderSet.direct(biomes.getOrThrow(ModBiomes.MEDITERRANEAN_FOREST));

        context.register(ADD_TALL_THIN_TWISTY_TREE, new BiomeModifiers.AddFeaturesBiomeModifier(
                targetBiomes,
                HolderSet.direct(placedTreeHolder),
                GenerationStep.Decoration.VEGETAL_DECORATION
        ));

        placedTreeHolder = placedFeatures.getOrThrow(TWISTY_TREE_PLACED_KEY);

        context.register(ADD_TWISTY_TREE, new BiomeModifiers.AddFeaturesBiomeModifier(
                targetBiomes,
                HolderSet.direct(placedTreeHolder),
                GenerationStep.Decoration.VEGETAL_DECORATION
        ));
        placedTreeHolder = placedFeatures.getOrThrow(SHORT_THIN_TWISTY_TREE_PLACED_KEY);

        context.register(ADD_SHORT_THIN_TWISTY_TREE, new BiomeModifiers.AddFeaturesBiomeModifier(
                targetBiomes,
                HolderSet.direct(placedTreeHolder),
                GenerationStep.Decoration.VEGETAL_DECORATION
        ));

    }
}
