package dev.sillyangel.nuggetmod.worldgen;

import dev.sillyangel.nuggetmod.NuggetMod;
import net.minecraft.registry.Registerable;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.Identifier;
import net.minecraft.world.gen.YOffset;
import net.minecraft.world.gen.feature.ConfiguredFeature;
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.FeatureConfig;
import net.minecraft.world.gen.feature.PlacedFeature;
import net.minecraft.world.gen.placementmodifier.HeightRangePlacementModifier;
import net.minecraft.world.gen.placementmodifier.PlacementModifier;

import java.util.List;

public class ModPlacedFeatures {
    public static final RegistryKey<PlacedFeature> NUGGET_ORE_PLACED_KEY = registerKey("nugget_ore_placed");
    // public static final RegistryKey<PlacedFeature> NETHER_NUGGET_ORE_PLACED_KEY = registerKey("nether_nugget_ore_placed");
    // public static final RegistryKey<PlacedFeature> END_NUGGET_ORE_PLACED_KEY = registerKey("end_nugget_ore_placed");

    public static void bootstrap(Registerable<PlacedFeature> context) {
        var configuredFeatures = context.getRegistryLookup(RegistryKeys.CONFIGURED_FEATURE);

        register(context, NUGGET_ORE_PLACED_KEY, configuredFeatures.getOrThrow(ModConfiguredFeatures.NUGGET_ORE_KEY),
            ModOrePlacement.modifiersWithCount(14,
                HeightRangePlacementModifier.uniform(YOffset.fixed(-64), YOffset.fixed(80))));
        // register(context, NETHER_NUGGET_ORE_PLACED_KEY, configuredFeatures.getOrThrow(ModConfiguredFeatures.NETHER_NUGGET_ORE_KEY),
                // ModOrePlacement.modifiersWithCount(14,
                        // HeightRangePlacementModifier.uniform(YOffset.fixed(-80), YOffset.fixed(80))));
        // register(context, END_NUGGET_ORE_PLACED_KEY, configuredFeatures.getOrThrow(ModConfiguredFeatures.END_NUGGET_ORE_KEY),
                // ModOrePlacement.modifiersWithCount(14,
                        // HeightRangePlacementModifier.uniform(YOffset.fixed(-80), YOffset.fixed(80))));

    }

    public static RegistryKey<PlacedFeature> registerKey(String name) {
        return RegistryKey.of(RegistryKeys.PLACED_FEATURE, Identifier.of(NuggetMod.MOD_ID, name));
    }

    private static void register(Registerable<PlacedFeature> context, RegistryKey<PlacedFeature> key, RegistryEntry<ConfiguredFeature<?, ?>> configuration,
                                 List<PlacementModifier> modifiers) {
        context.register(key, new PlacedFeature(configuration, List.copyOf(modifiers)));
    }

    private static <FC extends FeatureConfig, F extends Feature<FC>> void register(Registerable<PlacedFeature> context, RegistryKey<PlacedFeature> key,
                                                                                   RegistryEntry<ConfiguredFeature<?, ?>> configuration,
                                                                                   PlacementModifier... modifiers) {
        register(context, key, configuration, List.of(modifiers));
    }
}