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

import net.minecraft.util.valueproviders.UniformInt
import net.minecraft.world.item.Items
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets
import ru.dbotthepony.mc.otm.core.math.Decimal
import ru.dbotthepony.mc.otm.data.world.UniformDecimal
import ru.dbotthepony.mc.otm.datagen.modLootTable
import ru.dbotthepony.mc.otm.item.ProceduralBatteryItem
import ru.dbotthepony.mc.otm.item.exopack.ProceduralExopackSlotUpgradeItem
import ru.dbotthepony.mc.otm.item.matter.MatterDustItem
import ru.dbotthepony.mc.otm.registry.game.MItems

fun addChestLootTables(loot: LootTables) {
	loot.builder(LootContextParamSets.CHEST, modLootTable("food_box")) {
		lootPool {
			item(Items.PACKED_ICE) { setCount(minimal = 1, maximal = 3) }
			item(MItems.NUTRIENT_PASTE) { setCount(minimal = 1, maximal = 2) }
			item(Items.SNOWBALL) { setCount(minimal = 1, maximal = 5) }

			setRolls(3)
		}
	}

	loot.builder(LootContextParamSets.CHEST, modLootTable("salvage_crate")) {
		lootPool {
			item(MItems.MECHANICAL_PARTS) { setCount(minimal = 2, maximal = 3) }
			item(MItems.IRON_PLATE) { setCount(minimal = 2, maximal = 3) }
			item(MItems.ELECTRIC_PARTS) { setCount(minimal = 2, maximal = 3) }
			item(MItems.MECHANICAL_PARTS) { setCount(minimal = 2, maximal = 3) }
			item(MItems.CIRCUIT_PLATING) { setCount(minimal = 1, maximal = 3) }

			item(MItems.METAL_JUNK) { setCount(minimal = 3, maximal = 5) }
			item(Items.COPPER_INGOT) { setCount(minimal = 1, maximal = 3) }
			item(Items.EXPOSED_COPPER) { setCount(minimal = 1, maximal = 2) }

			item(MItems.ExopackUpgrades.INVENTORY_UPGRADE_PROCEDURAL) {
				chanceCondition(0.1)
				setWeight(1)
				apply(ProceduralExopackSlotUpgradeItem.Randomizer(UniformInt.of(1, 9), UniformInt.of(0, 3)))
			}

			setRolls(7)
		}
	}

	loot.builder(LootContextParamSets.CHEST, modLootTable("frigate_cargo")) {
		lootPool {
			item(Items.IRON_INGOT) {
				setCount(minimal = 1, maximal = 3)
				setWeight(3)
			}
			item(Items.GOLD_INGOT) { setCount(minimal = 1, maximal = 3) }
			item(Items.EMERALD) { setCount(minimal = 1, maximal = 3) }

			item(MItems.TRITANIUM_INGOT) { setCount(minimal = 1, maximal = 3) }
			item(MItems.MECHANICAL_PARTS) {
				setCount(minimal = 2, maximal = 3)
				setWeight(2)
			}
			item(MItems.CIRCUIT_PLATING) { setCount(minimal = 2, maximal = 3) }
			item(MItems.ENERGY_BUS) { setCount(minimal = 0, maximal = 2) }

			item(MItems.ROFLITE_ALLOY_INGOT) { setCount(minimal = 0, maximal = 3) }
			item(MItems.WITHERED_STEEL) { setCount(minimal = 0, maximal = 3) }
			item(Items.SADDLE) { setCount(minimal = 0, maximal = 1) }
			item(Items.DIAMOND) { setCount(minimal = 0, maximal = 3) }

			item(MItems.ExopackUpgrades.INVENTORY_UPGRADE_PROCEDURAL) {
				chanceCondition(0.2)
				setWeight(3)
				apply(ProceduralExopackSlotUpgradeItem.Randomizer(UniformInt.of(1, 9), UniformInt.of(0, 3)))
			}
			item(MItems.ExopackUpgrades.INVENTORY_UPGRADE_PROCEDURAL) {
				chanceCondition(0.1)
				setWeight(2)
				apply(ProceduralExopackSlotUpgradeItem.Randomizer(UniformInt.of(9, 18), UniformInt.of(0, 3)))
			}

			item(MItems.PROCEDURAL_BATTERY) {
				chanceCondition(0.05)
				setWeight(1)
				apply(
					ProceduralBatteryItem.Randomizer(
						maxBatteryLevel = UniformDecimal(Decimal(10_000_000), Decimal(50_000_000)),
						batteryLevel = UniformDecimal(Decimal(0), Decimal(25_000_000)),
						maxInput = UniformDecimal(Decimal(1_000), Decimal(5_000)),
					))
			}

			item(MItems.ZPM_BATTERY) {
				chanceCondition(0.001)
				setWeight(1)
			}

			item(MItems.MATTER_DUST) {
				chanceCondition(0.1)
				setWeight(4)
				apply(MatterDustItem.Randomizer(UniformDecimal(Decimal(100), Decimal(2_500))))
			}

			setRolls(12)
		}
	}

	loot.builder(LootContextParamSets.CHEST, modLootTable("laboratory/supply")) {
		lootPool {
			item(Items.BREAD) { setCount(minimal = 2, maximal = 3) }
			item(Items.HONEY_BOTTLE) { setCount(minimal = 1, maximal = 2) }
			item(MItems.NUTRIENT_PASTE) { setCount(minimal = 2, maximal = 3) }
			item(Items.SNOWBALL) { setCount(minimal = 1, maximal = 5) }
			item(Items.PACKED_ICE) { setCount(minimal = 2, maximal = 3) }

			setRolls(7)
		}
	}

	loot.builder(LootContextParamSets.CHEST, modLootTable("laboratory/reward")) {
		lootPool {
			item(MItems.NUTRIENT_PASTE) { setCount(minimal = 2, maximal = 3) }


			setRolls(5)
		}
	}
}
