package ru.dbotthepony.mc.otm.datagen.loot

import net.minecraft.data.DataGenerator
import net.minecraft.world.item.ItemStack
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition
import net.minecraftforge.common.data.GlobalLootModifierProvider
import ru.dbotthepony.mc.otm.data.loot.LootPoolAppender
import ru.dbotthepony.mc.otm.datagen.DataGen
import java.util.Arrays
import java.util.stream.Stream

@Suppress("FunctionName")
fun BasicLootAppender(
	conditions: Array<out LootItemCondition>,
	items: Stream<ItemStack>
): LootPoolAppender {
	return LootPoolAppender(conditions, items.map {
		lootPool {
			item(it.item) {
				setCount(it.count)
			}
		}
	})
}

@Suppress("FunctionName")
fun BasicLootAppender(
	conditions: Array<out LootItemCondition>,
	vararg items: ItemStack
) = BasicLootAppender(conditions, Arrays.stream(items))

@Suppress("FunctionName")
fun PlainLootAppender(
	conditions: Array<out LootItemCondition>,
	items: Stream<Pair<ItemStack, Double>>
): LootPoolAppender {
	return LootPoolAppender(conditions, items.map {
		lootPool {
			item(it.first.item) {
				setCount(it.first.count)
			}

			chanceCondition(it.second)
		}
	})
}

@Suppress("FunctionName")
fun PlainLootAppender(
	conditions: Array<out LootItemCondition>,
	vararg items: Pair<ItemStack, Double>
) = PlainLootAppender(conditions, Arrays.stream(items))

class LootModifiers(generator: DataGenerator) : GlobalLootModifierProvider(generator.packOutput, DataGen.MOD_ID) {
	private val lambdas = ArrayList<(LootModifiers) -> Unit>()

	fun lambda(lambda: (LootModifiers) -> Unit) {
		lambdas.add(lambda)
	}

	override fun start() {
		for (lambda in lambdas) {
			lambda.invoke(this)
		}
	}
}
