package fr.koka99cab.sanctuary.worldgen;

import fr.koka99cab.sanctuary.SanctuaryMod;
import fr.koka99cab.sanctuary.worldgen.feature.MatterDepthMaterializationFeature;
import fr.koka99cab.sanctuary.worldgen.feature.SanctuaryWaterLakeFeature;
import net.fabricmc.fabric.api.biome.v1.BiomeModifications;
import net.fabricmc.fabric.api.biome.v1.BiomeSelectors;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.levelgen.GenerationStep;
import net.minecraft.world.level.levelgen.feature.Feature;
import net.minecraft.world.level.levelgen.feature.LakeFeature;
import net.minecraft.world.level.levelgen.feature.configurations.NoneFeatureConfiguration;
import net.minecraft.world.level.levelgen.placement.PlacedFeature;

public final class SanctuaryFeatures {
	public static final Identifier MATTER_DEPTH_MATERIALIZATION_ID =
		Identifier.fromNamespaceAndPath(SanctuaryMod.MOD_ID, "matter_depth_materialization");
	public static final ResourceKey<PlacedFeature> MATTER_DEPTH_MATERIALIZATION_PLACED_FEATURE =
		ResourceKey.create(Registries.PLACED_FEATURE, MATTER_DEPTH_MATERIALIZATION_ID);
	public static final Feature<NoneFeatureConfiguration> MATTER_DEPTH_MATERIALIZATION =
		new MatterDepthMaterializationFeature(NoneFeatureConfiguration.CODEC);
	public static final Identifier WATER_LAKE_ID =
		Identifier.fromNamespaceAndPath(SanctuaryMod.MOD_ID, "water_lake");
	public static final ResourceKey<PlacedFeature> WATER_LAKE_UNDERGROUND_PLACED_FEATURE =
		ResourceKey.create(Registries.PLACED_FEATURE, Identifier.fromNamespaceAndPath(SanctuaryMod.MOD_ID, "water_lake_underground"));
	public static final ResourceKey<PlacedFeature> WATER_LAKE_SURFACE_PLACED_FEATURE =
		ResourceKey.create(Registries.PLACED_FEATURE, Identifier.fromNamespaceAndPath(SanctuaryMod.MOD_ID, "water_lake_surface"));
	public static final Feature<LakeFeature.Configuration> WATER_LAKE =
		new SanctuaryWaterLakeFeature(LakeFeature.Configuration.CODEC);

	private SanctuaryFeatures() {
	}

	public static void register() {
		Registry.register(
			BuiltInRegistries.FEATURE,
			MATTER_DEPTH_MATERIALIZATION_ID,
			MATTER_DEPTH_MATERIALIZATION
		);
		Registry.register(
			BuiltInRegistries.FEATURE,
			WATER_LAKE_ID,
			WATER_LAKE
		);

		addOverworldFeatureIfMissing(GenerationStep.Decoration.LAKES, WATER_LAKE_UNDERGROUND_PLACED_FEATURE);
		addOverworldFeatureIfMissing(GenerationStep.Decoration.LAKES, WATER_LAKE_SURFACE_PLACED_FEATURE);
		addOverworldFeatureIfMissing(GenerationStep.Decoration.UNDERGROUND_ORES, MATTER_DEPTH_MATERIALIZATION_PLACED_FEATURE);
	}

	private static void addOverworldFeatureIfMissing(GenerationStep.Decoration step, ResourceKey<PlacedFeature> feature) {
		BiomeModifications.addFeature(
			BiomeSelectors.foundInOverworld().and(context -> !context.hasPlacedFeature(feature)),
			step,
			feature
		);
	}
}
