package ru.dbotthepony.mc.otm.datagen

import net.minecraft.core.RegistrySetBuilder
import net.minecraft.core.registries.Registries
import net.minecraft.data.worldgen.BootstapContext
import net.minecraft.resources.ResourceKey
import net.minecraft.tags.BlockTags
import net.minecraft.world.level.levelgen.VerticalAnchor
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature
import net.minecraft.world.level.levelgen.feature.Feature
import net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration
import net.minecraft.world.level.levelgen.placement.CountPlacement
import net.minecraft.world.level.levelgen.placement.HeightRangePlacement
import net.minecraft.world.level.levelgen.placement.InSquarePlacement
import net.minecraft.world.level.levelgen.placement.PlacedFeature
import net.minecraft.world.level.levelgen.structure.templatesystem.TagMatchTest
import net.minecraftforge.common.data.DatapackBuiltinEntriesProvider
import net.minecraftforge.data.event.GatherDataEvent
import ru.dbotthepony.mc.otm.registry.MBlocks

private val oreKey by lazy { ResourceKey.create(Registries.CONFIGURED_FEATURE, modLocation("tritanium_ore")) }

fun registerConfiguredFeatures(context: BootstapContext<ConfiguredFeature<*, *>>) {
	val stone = TagMatchTest(BlockTags.STONE_ORE_REPLACEABLES)
	val deepslate = TagMatchTest(BlockTags.DEEPSLATE_ORE_REPLACEABLES)

	val target = listOf(
		OreConfiguration.target(stone, MBlocks.TRITANIUM_ORE.defaultBlockState()),
		OreConfiguration.target(deepslate, MBlocks.DEEPSLATE_TRITANIUM_ORE.defaultBlockState()),
	)

	context.register(oreKey, ConfiguredFeature(Feature.ORE, OreConfiguration(target, 9)))
}

fun registerPlacedFeatures(context: BootstapContext<PlacedFeature>) {
	fun location(name: String) = ResourceKey.create(Registries.PLACED_FEATURE, modLocation(name))

	val configured = context.lookup(Registries.CONFIGURED_FEATURE)
	val ore = configured.getOrThrow(oreKey)

	context.register(location("normal_tritanium"), PlacedFeature(
		ore,
		listOf(
			CountPlacement.of(8),
			InSquarePlacement.spread(),
			HeightRangePlacement.triangle(VerticalAnchor.absolute(0), VerticalAnchor.absolute(50))
		)
	))

	context.register(location("deep_tritanium"), PlacedFeature(
		ore,
		listOf(
			CountPlacement.of(10),
			InSquarePlacement.spread(),
			HeightRangePlacement.uniform(VerticalAnchor.aboveBottom(8), VerticalAnchor.absolute(0))
		)
	))
}
