package com.acethewildfire.acesbs.world;

import com.acethewildfire.acesbs.AcesBS;
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 {
    // CF -> PF -> WG
    // CF: Could be a house
    // PF: How is it placed
    // WG: Where it is placed

    public static final RegistryKey<PlacedFeature> ENTROPY_ORE_PLACED_KEY = registerKey("entropy_ore_placed");
    public static final RegistryKey<PlacedFeature> DEEPSLATE_ENTROPY_ORE_PLACED_KEY = registerKey("deepslate_entropy_ore_placed");
    public static final RegistryKey<PlacedFeature> INFERNAL_ASHES_ORE_PLACED_KEY = registerKey("infernal_ashes_ore_placed");
    public static final RegistryKey<PlacedFeature> ENDTROPY_ORE_PLACED_KEY = registerKey("endtropy_ore_placed");

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


        register(context, ENTROPY_ORE_PLACED_KEY, configuredFeatures.getOrThrow(ModConfiguredFeatures.ENTROPY_ORE_KEY),
                ModOrePlacement.modifiersWithCount(12,
                        HeightRangePlacementModifier.uniform(YOffset.fixed(-70), YOffset.fixed(80)))
        );
        register(context, DEEPSLATE_ENTROPY_ORE_PLACED_KEY, configuredFeatures.getOrThrow(ModConfiguredFeatures.DEEPSLATE_ENTROPY_ORE_KEY),
                ModOrePlacement.modifiersWithCount(12,
                        HeightRangePlacementModifier.uniform(YOffset.fixed(-70), YOffset.fixed(80)))
                );
        register(context, INFERNAL_ASHES_ORE_PLACED_KEY, configuredFeatures.getOrThrow(ModConfiguredFeatures.INFERNAL_ASHES_ORE_KEY),
                ModOrePlacement.modifiersWithCount(30,
                        HeightRangePlacementModifier.uniform(YOffset.fixed(-70), YOffset.fixed(80)))
        );
        register(context, ENDTROPY_ORE_PLACED_KEY, configuredFeatures.getOrThrow(ModConfiguredFeatures.ENDTROPY_ORE_KEY),
                ModOrePlacement.modifiersWithCount(12,
                        HeightRangePlacementModifier.uniform(YOffset.fixed(-70), YOffset.fixed(80)))
        );

    }

    public static RegistryKey<PlacedFeature> registerKey(String name) {
        return RegistryKey.of(RegistryKeys.PLACED_FEATURE, Identifier.of(AcesBS.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));
    }

}
