package net.minecraft.data.loot.packs;

import net.minecraft.advancements.predicates.LocationPredicate;
import net.minecraft.core.HolderGetter;
import net.minecraft.core.HolderSet;
import net.minecraft.core.registries.Registries;
import net.minecraft.data.loot.LootTableSubProvider;
import net.minecraft.tags.InstrumentTags;
import net.minecraft.world.item.Instrument;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.alchemy.Potions;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.Biomes;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
import net.minecraft.world.level.storage.loot.LootPool;
import net.minecraft.world.level.storage.loot.LootTable;
import net.minecraft.world.level.storage.loot.entries.EmptyLootItem;
import net.minecraft.world.level.storage.loot.entries.LootItem;
import net.minecraft.world.level.storage.loot.functions.EnchantRandomlyFunction;
import net.minecraft.world.level.storage.loot.functions.EnchantWithLevelsFunction;
import net.minecraft.world.level.storage.loot.functions.SetInstrumentFunction;
import net.minecraft.world.level.storage.loot.functions.SetItemCountFunction;
import net.minecraft.world.level.storage.loot.functions.SetItemDamageFunction;
import net.minecraft.world.level.storage.loot.functions.SetPotionFunction;
import net.minecraft.world.level.storage.loot.predicates.LocationCheck;
import net.minecraft.world.level.storage.loot.providers.number.floats.ContextFloatProviders;
import net.minecraft.world.level.storage.loot.providers.number.ints.ContextIntProviders;

public class TradeRebalanceChestLoot implements LootTableSubProvider {
	private final LootTableSubProvider.Context output;
	private final HolderGetter<Enchantment> enchantments;
	private final HolderGetter<Biome> biomes;
	private final HolderGetter<Instrument> instruments;

	public TradeRebalanceChestLoot(final LootTableSubProvider.Context output) {
		this.output = output;
		this.enchantments = output.lookup(Registries.ENCHANTMENT);
		this.biomes = output.lookup(Registries.BIOME);
		this.instruments = output.lookup(Registries.INSTRUMENT);
	}

	@Override
	public void run() {
		this.output
			.accept(
				BuiltInLootTables.ABANDONED_MINESHAFT,
				LootTable.lootTable()
					.withPool(
						LootPool.lootPool()
							.setRolls(ContextIntProviders.exactly(1))
							.add(LootItem.lootTableItem(Items.GOLDEN_APPLE).setWeight(20))
							.add(LootItem.lootTableItem(Items.ENCHANTED_GOLDEN_APPLE))
							.add(LootItem.lootTableItem(Items.NAME_TAG).setWeight(30))
							.add(LootItem.lootTableItem(Items.BOOK).setWeight(10).apply(EnchantRandomlyFunction.randomApplicableEnchantment(this.enchantments)))
							.add(LootItem.lootTableItem(Items.IRON_PICKAXE).setWeight(5))
							.add(EmptyLootItem.emptyItem().setWeight(5))
					)
					.withPool(
						LootPool.lootPool()
							.setRolls(ContextIntProviders.between(2, 4))
							.add(LootItem.lootTableItem(Items.IRON_INGOT).setWeight(10).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 5))))
							.add(LootItem.lootTableItem(Items.GOLD_INGOT).setWeight(5).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 3))))
							.add(LootItem.lootTableItem(Items.REDSTONE).setWeight(5).apply(SetItemCountFunction.setCount(ContextIntProviders.between(4, 9))))
							.add(LootItem.lootTableItem(Items.LAPIS_LAZULI).setWeight(5).apply(SetItemCountFunction.setCount(ContextIntProviders.between(4, 9))))
							.add(LootItem.lootTableItem(Items.DIAMOND).setWeight(3).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 2))))
							.add(LootItem.lootTableItem(Items.COAL).setWeight(10).apply(SetItemCountFunction.setCount(ContextIntProviders.between(3, 8))))
							.add(LootItem.lootTableItem(Items.BREAD).setWeight(15).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 3))))
							.add(LootItem.lootTableItem(Items.GLOW_BERRIES).setWeight(15).apply(SetItemCountFunction.setCount(ContextIntProviders.between(3, 6))))
							.add(LootItem.lootTableItem(Items.MELON_SEEDS).setWeight(10).apply(SetItemCountFunction.setCount(ContextIntProviders.between(2, 4))))
							.add(LootItem.lootTableItem(Items.PUMPKIN_SEEDS).setWeight(10).apply(SetItemCountFunction.setCount(ContextIntProviders.between(2, 4))))
							.add(LootItem.lootTableItem(Items.BEETROOT_SEEDS).setWeight(10).apply(SetItemCountFunction.setCount(ContextIntProviders.between(2, 4))))
					)
					.withPool(
						LootPool.lootPool()
							.setRolls(ContextIntProviders.exactly(3))
							.add(LootItem.lootTableItem(Blocks.RAIL).setWeight(20).apply(SetItemCountFunction.setCount(ContextIntProviders.between(4, 8))))
							.add(LootItem.lootTableItem(Blocks.POWERED_RAIL).setWeight(5).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 4))))
							.add(LootItem.lootTableItem(Blocks.DETECTOR_RAIL).setWeight(5).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 4))))
							.add(LootItem.lootTableItem(Blocks.ACTIVATOR_RAIL).setWeight(5).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 4))))
							.add(LootItem.lootTableItem(Blocks.TORCH).setWeight(15).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 16))))
							.add(
								LootItem.lootTableItem(Items.MUSIC_DISC_BOUNCE)
									.when(LocationCheck.checkLocation(LocationPredicate.Builder.location().setBiomes(HolderSet.direct(this.biomes.getOrThrow(Biomes.SULFUR_CAVES)))))
									.setWeight(10)
							)
					)
					.withPool(
						LootPool.lootPool()
							.setRolls(ContextIntProviders.exactly(1))
							.add(EmptyLootItem.emptyItem().setWeight(4))
							.add(
								LootItem.lootTableItem(Items.BOOK)
									.setWeight(1)
									.apply(new EnchantRandomlyFunction.Builder().withEnchantment(this.enchantments.getOrThrow(Enchantments.EFFICIENCY)))
							)
					)
			);
		this.output.accept(BuiltInLootTables.ANCIENT_CITY, this.ancientCityLootTable());
		this.output.accept(BuiltInLootTables.DESERT_PYRAMID, this.desertPyramidLootTable());
		this.output.accept(BuiltInLootTables.JUNGLE_TEMPLE, this.jungleTempleLootTable());
		this.output.accept(BuiltInLootTables.PILLAGER_OUTPOST, this.pillagerOutpostLootTable());
	}

	public LootTable.Builder pillagerOutpostLootTable() {
		return LootTable.lootTable()
			.withPool(LootPool.lootPool().setRolls(ContextIntProviders.between(0, 1)).add(LootItem.lootTableItem(Items.CROSSBOW)))
			.withPool(
				LootPool.lootPool()
					.setRolls(ContextIntProviders.between(2, 3))
					.add(LootItem.lootTableItem(Items.WHEAT).setWeight(7).apply(SetItemCountFunction.setCount(ContextIntProviders.between(3, 5))))
					.add(LootItem.lootTableItem(Items.POTATO).setWeight(5).apply(SetItemCountFunction.setCount(ContextIntProviders.between(2, 5))))
					.add(LootItem.lootTableItem(Items.CARROT).setWeight(5).apply(SetItemCountFunction.setCount(ContextIntProviders.between(3, 5))))
			)
			.withPool(
				LootPool.lootPool()
					.setRolls(ContextIntProviders.between(1, 3))
					.add(LootItem.lootTableItem(Blocks.DARK_OAK_LOG).apply(SetItemCountFunction.setCount(ContextIntProviders.between(2, 3))))
			)
			.withPool(
				LootPool.lootPool()
					.setRolls(ContextIntProviders.between(2, 3))
					.add(LootItem.lootTableItem(Items.EXPERIENCE_BOTTLE).setWeight(7))
					.add(LootItem.lootTableItem(Items.STRING).setWeight(4).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 6))))
					.add(LootItem.lootTableItem(Items.ARROW).setWeight(4).apply(SetItemCountFunction.setCount(ContextIntProviders.between(2, 7))))
					.add(LootItem.lootTableItem(Items.TRIPWIRE_HOOK).setWeight(3).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 3))))
					.add(LootItem.lootTableItem(Items.IRON_INGOT).setWeight(3).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 3))))
					.add(LootItem.lootTableItem(Items.BOOK).setWeight(1).apply(EnchantRandomlyFunction.randomApplicableEnchantment(this.enchantments)))
			)
			.withPool(
				LootPool.lootPool()
					.setRolls(ContextIntProviders.between(0, 1))
					.add(LootItem.lootTableItem(Items.GOAT_HORN))
					.apply(SetInstrumentFunction.setInstrumentOptions(this.instruments.getOrThrow(InstrumentTags.REGULAR_GOAT_HORNS)))
			)
			.withPool(
				LootPool.lootPool()
					.setRolls(ContextIntProviders.exactly(1))
					.add(EmptyLootItem.emptyItem().setWeight(3))
					.add(LootItem.lootTableItem(Items.SENTRY_ARMOR_TRIM_SMITHING_TEMPLATE).setWeight(1).apply(SetItemCountFunction.setCount(ContextIntProviders.exactly(2))))
			)
			.withPool(
				LootPool.lootPool()
					.setRolls(ContextIntProviders.exactly(1))
					.add(EmptyLootItem.emptyItem().setWeight(1))
					.add(
						LootItem.lootTableItem(Items.BOOK)
							.setWeight(2)
							.apply(new EnchantRandomlyFunction.Builder().withEnchantment(this.enchantments.getOrThrow(Enchantments.QUICK_CHARGE)))
					)
			);
	}

	public LootTable.Builder desertPyramidLootTable() {
		return LootTable.lootTable()
			.withPool(
				LootPool.lootPool()
					.setRolls(ContextIntProviders.between(2, 4))
					.add(LootItem.lootTableItem(Items.DIAMOND).setWeight(5).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 3))))
					.add(LootItem.lootTableItem(Items.IRON_INGOT).setWeight(15).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 5))))
					.add(LootItem.lootTableItem(Items.GOLD_INGOT).setWeight(15).apply(SetItemCountFunction.setCount(ContextIntProviders.between(2, 7))))
					.add(LootItem.lootTableItem(Items.EMERALD).setWeight(15).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 3))))
					.add(LootItem.lootTableItem(Items.BONE).setWeight(25).apply(SetItemCountFunction.setCount(ContextIntProviders.between(4, 6))))
					.add(LootItem.lootTableItem(Items.SPIDER_EYE).setWeight(25).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 3))))
					.add(LootItem.lootTableItem(Items.ROTTEN_FLESH).setWeight(25).apply(SetItemCountFunction.setCount(ContextIntProviders.between(3, 7))))
					.add(LootItem.lootTableItem(Items.SADDLE).setWeight(20))
					.add(LootItem.lootTableItem(Items.COPPER_HORSE_ARMOR).setWeight(15))
					.add(LootItem.lootTableItem(Items.IRON_HORSE_ARMOR).setWeight(15))
					.add(LootItem.lootTableItem(Items.GOLDEN_HORSE_ARMOR).setWeight(10))
					.add(LootItem.lootTableItem(Items.DIAMOND_HORSE_ARMOR).setWeight(5))
					.add(LootItem.lootTableItem(Items.BOOK).setWeight(10).apply(EnchantRandomlyFunction.randomApplicableEnchantment(this.enchantments)))
					.add(LootItem.lootTableItem(Items.GOLDEN_APPLE).setWeight(20))
					.add(LootItem.lootTableItem(Items.ENCHANTED_GOLDEN_APPLE).setWeight(2))
					.add(EmptyLootItem.emptyItem().setWeight(15))
			)
			.withPool(
				LootPool.lootPool()
					.setRolls(ContextIntProviders.exactly(4))
					.add(LootItem.lootTableItem(Items.BONE).setWeight(10).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 8))))
					.add(LootItem.lootTableItem(Items.GUNPOWDER).setWeight(10).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 8))))
					.add(LootItem.lootTableItem(Items.ROTTEN_FLESH).setWeight(10).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 8))))
					.add(LootItem.lootTableItem(Items.STRING).setWeight(10).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 8))))
					.add(LootItem.lootTableItem(Blocks.SAND).setWeight(10).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 8))))
			)
			.withPool(
				LootPool.lootPool()
					.setRolls(ContextIntProviders.exactly(1))
					.add(EmptyLootItem.emptyItem().setWeight(4))
					.add(LootItem.lootTableItem(Items.DUNE_ARMOR_TRIM_SMITHING_TEMPLATE).setWeight(1).apply(SetItemCountFunction.setCount(ContextIntProviders.exactly(2))))
					.add(
						LootItem.lootTableItem(Items.BOOK)
							.setWeight(2)
							.apply(new EnchantRandomlyFunction.Builder().withEnchantment(this.enchantments.getOrThrow(Enchantments.UNBREAKING)))
					)
			);
	}

	public LootTable.Builder ancientCityLootTable() {
		return LootTable.lootTable()
			.withPool(
				LootPool.lootPool()
					.setRolls(ContextIntProviders.between(5, 10))
					.add(LootItem.lootTableItem(Items.ENCHANTED_GOLDEN_APPLE).setWeight(1).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 2))))
					.add(LootItem.lootTableItem(Items.MUSIC_DISC_OTHERSIDE).setWeight(1))
					.add(LootItem.lootTableItem(Items.COMPASS).setWeight(2).apply(SetItemCountFunction.setCount(ContextIntProviders.exactly(1))))
					.add(LootItem.lootTableItem(Items.SCULK_CATALYST).setWeight(2).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 2))))
					.add(
						LootItem.lootTableItem(Items.DIAMOND_HOE)
							.setWeight(2)
							.apply(SetItemCountFunction.setCount(ContextIntProviders.exactly(1)))
							.apply(SetItemDamageFunction.setDamage(ContextFloatProviders.between(0.8F, 1.0F)))
							.apply(EnchantWithLevelsFunction.enchantWithLevels(this.enchantments, ContextIntProviders.between(30, 50)))
					)
					.add(LootItem.lootTableItem(Items.LEAD).setWeight(2).apply(SetItemCountFunction.setCount(ContextIntProviders.exactly(1))))
					.add(LootItem.lootTableItem(Items.DIAMOND_HORSE_ARMOR).setWeight(2).apply(SetItemCountFunction.setCount(ContextIntProviders.exactly(1))))
					.add(LootItem.lootTableItem(Items.SADDLE).setWeight(2).apply(SetItemCountFunction.setCount(ContextIntProviders.exactly(1))))
					.add(LootItem.lootTableItem(Items.MUSIC_DISC_13).setWeight(2))
					.add(LootItem.lootTableItem(Items.MUSIC_DISC_CAT).setWeight(2))
					.add(
						LootItem.lootTableItem(Items.DIAMOND_LEGGINGS)
							.setWeight(2)
							.apply(EnchantWithLevelsFunction.enchantWithLevels(this.enchantments, ContextIntProviders.between(30, 50)))
					)
					.add(
						LootItem.lootTableItem(Items.BOOK)
							.setWeight(3)
							.apply(new EnchantRandomlyFunction.Builder().withEnchantment(this.enchantments.getOrThrow(Enchantments.SWIFT_SNEAK)))
					)
					.add(LootItem.lootTableItem(Items.SCULK).setWeight(3).apply(SetItemCountFunction.setCount(ContextIntProviders.between(4, 10))))
					.add(LootItem.lootTableItem(Items.SCULK_SENSOR).setWeight(3).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 3))))
					.add(LootItem.lootTableItem(Items.CANDLE).setWeight(3).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 4))))
					.add(LootItem.lootTableItem(Items.AMETHYST_SHARD).setWeight(3).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 15))))
					.add(LootItem.lootTableItem(Items.EXPERIENCE_BOTTLE).setWeight(3).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 3))))
					.add(LootItem.lootTableItem(Items.GLOW_BERRIES).setWeight(3).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 15))))
					.add(
						LootItem.lootTableItem(Items.IRON_LEGGINGS)
							.setWeight(3)
							.apply(EnchantWithLevelsFunction.enchantWithLevels(this.enchantments, ContextIntProviders.between(20, 39)))
					)
					.add(LootItem.lootTableItem(Items.ECHO_SHARD).setWeight(4).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 3))))
					.add(LootItem.lootTableItem(Items.DISC_FRAGMENT_5).setWeight(4).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 3))))
					.add(
						LootItem.lootTableItem(Items.POTION)
							.setWeight(5)
							.apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 3)))
							.apply(SetPotionFunction.setPotion(Potions.STRONG_REGENERATION))
					)
					.add(LootItem.lootTableItem(Items.BOOK).setWeight(5).apply(EnchantRandomlyFunction.randomApplicableEnchantment(this.enchantments)))
					.add(LootItem.lootTableItem(Items.BOOK).setWeight(5).apply(SetItemCountFunction.setCount(ContextIntProviders.between(3, 10))))
					.add(LootItem.lootTableItem(Items.BONE).setWeight(5).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 15))))
					.add(LootItem.lootTableItem(Items.SOUL_TORCH).setWeight(5).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 15))))
					.add(LootItem.lootTableItem(Items.COAL).setWeight(7).apply(SetItemCountFunction.setCount(ContextIntProviders.between(6, 15))))
			)
			.withPool(
				LootPool.lootPool()
					.setRolls(ContextIntProviders.exactly(1))
					.add(EmptyLootItem.emptyItem().setWeight(71))
					.add(
						LootItem.lootTableItem(Items.BOOK)
							.setWeight(4)
							.apply(new EnchantRandomlyFunction.Builder().withEnchantment(this.enchantments.getOrThrow(Enchantments.MENDING)))
					)
					.add(LootItem.lootTableItem(Items.WARD_ARMOR_TRIM_SMITHING_TEMPLATE).setWeight(4))
					.add(LootItem.lootTableItem(Items.SILENCE_ARMOR_TRIM_SMITHING_TEMPLATE).setWeight(1))
			);
	}

	public LootTable.Builder jungleTempleLootTable() {
		return LootTable.lootTable()
			.withPool(
				LootPool.lootPool()
					.setRolls(ContextIntProviders.between(2, 6))
					.add(LootItem.lootTableItem(Items.DIAMOND).setWeight(3).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 3))))
					.add(LootItem.lootTableItem(Items.IRON_INGOT).setWeight(10).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 5))))
					.add(LootItem.lootTableItem(Items.GOLD_INGOT).setWeight(15).apply(SetItemCountFunction.setCount(ContextIntProviders.between(2, 7))))
					.add(LootItem.lootTableItem(Blocks.BAMBOO).setWeight(15).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 3))))
					.add(LootItem.lootTableItem(Items.EMERALD).setWeight(2).apply(SetItemCountFunction.setCount(ContextIntProviders.between(1, 3))))
					.add(LootItem.lootTableItem(Items.BONE).setWeight(20).apply(SetItemCountFunction.setCount(ContextIntProviders.between(4, 6))))
					.add(LootItem.lootTableItem(Items.ROTTEN_FLESH).setWeight(16).apply(SetItemCountFunction.setCount(ContextIntProviders.between(3, 7))))
					.add(LootItem.lootTableItem(Items.SADDLE).setWeight(3))
					.add(LootItem.lootTableItem(Items.COPPER_HORSE_ARMOR))
					.add(LootItem.lootTableItem(Items.IRON_HORSE_ARMOR))
					.add(LootItem.lootTableItem(Items.GOLDEN_HORSE_ARMOR))
					.add(LootItem.lootTableItem(Items.DIAMOND_HORSE_ARMOR))
					.add(LootItem.lootTableItem(Items.BOOK).apply(EnchantWithLevelsFunction.enchantWithLevels(this.enchantments, ContextIntProviders.exactly(30))))
			)
			.withPool(
				LootPool.lootPool()
					.setRolls(ContextIntProviders.exactly(1))
					.add(EmptyLootItem.emptyItem().setWeight(2))
					.add(LootItem.lootTableItem(Items.WILD_ARMOR_TRIM_SMITHING_TEMPLATE).setWeight(1).apply(SetItemCountFunction.setCount(ContextIntProviders.exactly(2))))
			)
			.withPool(
				LootPool.lootPool()
					.setRolls(ContextIntProviders.exactly(1))
					.add(EmptyLootItem.emptyItem().setWeight(1))
					.add(
						LootItem.lootTableItem(Items.BOOK).apply(new EnchantRandomlyFunction.Builder().withEnchantment(this.enchantments.getOrThrow(Enchantments.UNBREAKING)))
					)
			);
	}
}
