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

import net.minecraft.resources.ResourceLocation
import net.minecraft.util.valueproviders.UniformInt
import net.minecraft.world.entity.EntityType
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Rarity
import net.minecraft.world.level.storage.loot.BuiltInLootTables
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition
import net.minecraftforge.common.loot.LootTableIdCondition
import ru.dbotthepony.mc.otm.core.math.Decimal
import ru.dbotthepony.mc.otm.data.UniformDecimal
import ru.dbotthepony.mc.otm.data.condition.ChanceWithPlaytimeCondition
import ru.dbotthepony.mc.otm.data.condition.HasExoPackCondition
import ru.dbotthepony.mc.otm.data.condition.ItemInInventoryCondition
import ru.dbotthepony.mc.otm.data.condition.KilledByRealPlayerOrIndirectly
import ru.dbotthepony.mc.otm.data.loot.LootPoolAppender
import ru.dbotthepony.mc.otm.item.ProceduralBatteryItem
import ru.dbotthepony.mc.otm.item.exopack.ProceduralExoPackSlotUpgradeItem
import ru.dbotthepony.mc.otm.registry.MItems

@Suppress("FunctionName")
fun LootTableIdCondition(location: String): LootItemCondition {
	return LootTableIdCondition.Builder(ResourceLocation("minecraft", location)).build()
}

@Suppress("FunctionName")
fun LootTableIdCondition(location: ResourceLocation): LootItemCondition {
	return LootTableIdCondition.Builder(location).build()
}

fun addLootModifiers(it: LootModifiers) {
	it.add("dungeon_exopack", LootPoolAppender(
		arrayOf(LootTableIdCondition(BuiltInLootTables.SIMPLE_DUNGEON)),

		singleItem(MItems.ExopackUpgrades.INVENTORY_UPGRADE_PROCEDURAL) {
			chanceCondition(0.2)
			apply(ProceduralExoPackSlotUpgradeItem.Randomizer(UniformInt.of(6, 9)))
		},

		singleItem(MItems.ExopackUpgrades.INVENTORY_UPGRADE_PROCEDURAL) {
			chanceCondition(0.05)
			apply(ProceduralExoPackSlotUpgradeItem.Randomizer(UniformInt.of(9, 18)))
		},
	))

	it.add("mineshaft_additions", LootPoolAppender(
		arrayOf(LootTableIdCondition(BuiltInLootTables.ABANDONED_MINESHAFT)),

		singleItem(MItems.ExopackUpgrades.INVENTORY_UPGRADE_PROCEDURAL) {
			chanceCondition(0.1)
			apply(ProceduralExoPackSlotUpgradeItem.Randomizer(UniformInt.of(4, 8)))
		},

		singleItem(MItems.ExopackUpgrades.INVENTORY_UPGRADE_PROCEDURAL) {
			chanceCondition(0.1)
			apply(ProceduralExoPackSlotUpgradeItem.Randomizer(UniformInt.of(4, 10)))
		},

		singleItem(MItems.PROCEDURAL_BATTERY) {
			chanceCondition(0.15)

			apply(ProceduralBatteryItem.Randomizer(
				maxBatteryLevel = UniformDecimal(Decimal(8_000_000), Decimal(40_000_000)),
				batteryLevel = UniformDecimal(Decimal(0), Decimal(20_000_000)),
				maxInput = UniformDecimal(Decimal(700), Decimal(4_000)),
			))
		}
	))

	it.add("desert_pyramid_exosuit", LootPoolAppender(
		arrayOf(LootTableIdCondition(BuiltInLootTables.DESERT_PYRAMID)),

		singleItem(MItems.ExopackUpgrades.INVENTORY_UPGRADE_PROCEDURAL) {
			chanceCondition(0.1)
			apply(ProceduralExoPackSlotUpgradeItem.Randomizer(UniformInt.of(12, 18)))
		},

		singleItem(MItems.ExopackUpgrades.INVENTORY_UPGRADE_PROCEDURAL) {
			chanceCondition(0.25)
			apply(ProceduralExoPackSlotUpgradeItem.Randomizer(UniformInt.of(4, 9)))
		},
	))

	it.add("jungle_temple_exosuit", LootPoolAppender(
		arrayOf(LootTableIdCondition(BuiltInLootTables.JUNGLE_TEMPLE)),

		singleItem(MItems.ExopackUpgrades.INVENTORY_UPGRADE_PROCEDURAL) {
			chanceCondition(0.15)
			apply(ProceduralExoPackSlotUpgradeItem.Randomizer(UniformInt.of(9, 18)))
		},

		singleItem(MItems.ExopackUpgrades.INVENTORY_UPGRADE_PROCEDURAL) {
			chanceCondition(0.35)
			apply(ProceduralExoPackSlotUpgradeItem.Randomizer(UniformInt.of(16, 28), UniformInt.of(2, 6)))
		},
	))

	it.add("end_city_exosuit", LootPoolAppender(
		arrayOf(LootTableIdCondition(BuiltInLootTables.END_CITY_TREASURE)),

		singleItem(MItems.ExopackUpgrades.INVENTORY_UPGRADE_PROCEDURAL) {
			chanceCondition(0.4)
			apply(ProceduralExoPackSlotUpgradeItem.Randomizer(UniformInt.of(9, 18)))
		},

		singleItem(MItems.ExopackUpgrades.INVENTORY_UPGRADE_PROCEDURAL) {
			chanceCondition(0.2)
			apply(ProceduralExoPackSlotUpgradeItem.Randomizer(UniformInt.of(14, 27), UniformInt.of(2, 6)))
		},

		singleItem(MItems.ExopackUpgrades.INVENTORY_UPGRADE_PROCEDURAL) {
			chanceCondition(0.1)
			apply(ProceduralExoPackSlotUpgradeItem.Randomizer(UniformInt.of(27, 56), UniformInt.of(2, 6)))
		},
	))

	it.add("dungeon_pill", PlainLootAppender(
		arrayOf(LootTableIdCondition(BuiltInLootTables.SIMPLE_DUNGEON)),
		ItemStack(MItems.PILL_ANDROID, 1) to 0.1,
		ItemStack(MItems.PILL_HEAL, 2) to 0.5,
		ItemStack(MItems.PILL_HEAL, 1) to 0.75,
	))

	it.add("mineshaft_pill", PlainLootAppender(
		arrayOf(LootTableIdCondition(BuiltInLootTables.ABANDONED_MINESHAFT)),
		ItemStack(MItems.PILL_ANDROID, 1) to 0.04,
		ItemStack(MItems.PILL_HEAL, 2) to 0.1,
		ItemStack(MItems.PILL_HEAL, 1) to 0.4,
	))

	it.add("mineshaft_nutrient_paste", PlainLootAppender(
		arrayOf(LootTableIdCondition(BuiltInLootTables.ABANDONED_MINESHAFT)),
		ItemStack(MItems.NUTRIENT_PASTE, 6) to 0.5,
		ItemStack(MItems.NUTRIENT_PASTE, 8) to 0.35,
		ItemStack(MItems.NUTRIENT_PASTE, 12) to 0.15,
	))

	it.add("desert_pyramid_pill", PlainLootAppender(
		arrayOf(LootTableIdCondition(BuiltInLootTables.DESERT_PYRAMID)),
		ItemStack(MItems.PILL_ANDROID, 1) to 0.15,
		ItemStack(MItems.PILL_HEAL, 1) to 0.3,
	))

	it.add("jungle_temple_pill", PlainLootAppender(
		arrayOf(LootTableIdCondition(BuiltInLootTables.JUNGLE_TEMPLE)),
		ItemStack(MItems.PILL_ANDROID, 1) to 0.5
	))

	it.add("end_city_modifications", PlainLootAppender(
		arrayOf(LootTableIdCondition(BuiltInLootTables.END_CITY_TREASURE)),
		ItemStack(MItems.PILL_ANDROID, 1) to 0.15,
		ItemStack(MItems.PILL_HUMANE, 1) to 0.3,
		ItemStack(MItems.PILL_OBLIVION, 1) to 0.5,
		ItemStack(MItems.ZPM_BATTERY, 1) to 0.005,
	))

	it.add("shipwreck_supply_pill", PlainLootAppender(
		arrayOf(LootTableIdCondition(BuiltInLootTables.SHIPWRECK_SUPPLY)),
		ItemStack(MItems.PILL_HUMANE, 1) to 0.4,
		ItemStack(MItems.PILL_HEAL, 1) to 0.6,
		ItemStack(MItems.PILL_HEAL, 1) to 0.6,
		ItemStack(MItems.PILL_HEAL, 1) to 0.6,
		ItemStack(MItems.PILL_HEAL, 1) to 0.6,
	))

	it.add("shipwreck_supply_nutrient_paste", PlainLootAppender(
		arrayOf(LootTableIdCondition(BuiltInLootTables.SHIPWRECK_SUPPLY)),
		ItemStack(MItems.NUTRIENT_PASTE, 12) to 0.85,
		ItemStack(MItems.NUTRIENT_PASTE, 24) to 0.35,
		ItemStack(MItems.NUTRIENT_PASTE, 38) to 0.1,
	))

	it.add("exosuit_probe_enderman_drop", BasicLootAppender(
		arrayOf(
			LootTableIdCondition(EntityType.ENDERMAN.defaultLootTable),
			HasExoPackCondition.INVERTED,
			KilledByRealPlayerOrIndirectly,
			ChanceWithPlaytimeCondition(
				minPlaytime = 20 * 60 * 10,
				maxPlaytime = 20 * 60 * 120,
				minProbability = 0.1,
				maxProbability = 0.5,
			),
			ItemInInventoryCondition(
				ItemStack(MItems.EXOPACK_PROBE)
			).invert().build()
		),
		ItemStack(MItems.EXOPACK_PROBE)
	))

	it.add("wither_exosuit_upgrades", BasicLootAppender(
		arrayOf(
			LootTableIdCondition(EntityType.WITHER.defaultLootTable),
			KilledByRealPlayerOrIndirectly,
		),
		ItemStack(MItems.ExopackUpgrades.INVENTORY_UPGRADE_WITHER),
	))

	it.add("ender_dragon_exosuit_upgrades", BasicLootAppender(
		arrayOf(
			LootTableIdCondition(EntityType.ENDER_DRAGON.defaultLootTable),
			KilledByRealPlayerOrIndirectly,
		),
		ItemStack(MItems.ExopackUpgrades.INVENTORY_UPGRADE_ENDER_DRAGON),
	))
}
