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

import net.minecraft.world.item.Items
import net.minecraft.world.level.block.state.properties.BlockStateProperties
import net.minecraft.world.level.block.state.properties.DoubleBlockHalf
import net.minecraft.world.level.storage.loot.entries.LootItem
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets
import net.minecraft.world.level.storage.loot.predicates.ExplosionCondition
import ru.dbotthepony.mc.otm.datagen.modLocation
import ru.dbotthepony.mc.otm.registry.MBlocks
import ru.dbotthepony.mc.otm.registry.MItems
import ru.dbotthepony.mc.otm.registry.MRegistry

fun addLootTables(lootTables: LootTables) {
	lootTables.dropsSelf(MRegistry.DECORATIVE_CRATE.allBlocks.values) { condition(ExplosionCondition.survivesExplosion()) }

	lootTables.dropsSelf(MRegistry.CARGO_CRATES.allBlocks.values) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MRegistry.INDUSTRIAL_GLASS.allBlocks.values) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MRegistry.INDUSTRIAL_GLASS_PANE.allBlocks.values) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MRegistry.TRITANIUM_BLOCK.allBlocks.values) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MRegistry.TRITANIUM_WALL.allBlocks.values) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MRegistry.TRITANIUM_STAIRS.allBlocks.values) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.createSlabItemTable(MRegistry.TRITANIUM_SLAB.allBlocks.values)
	lootTables.dropsSelf(MRegistry.VENT.allBlocks.values) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MRegistry.VENT_ALTERNATIVE.allBlocks.values) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MRegistry.FLOOR_TILES.blocks.values) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MRegistry.UNREFINED_FLOOR_TILES.blocks.values) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MRegistry.TRITANIUM_STRIPED_BLOCK.flatBlocks) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MRegistry.TRITANIUM_STRIPED_STAIRS.flatBlocks) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MRegistry.TRITANIUM_STRIPED_WALL.flatBlocks) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.createSlabItemTable(MRegistry.TRITANIUM_STRIPED_SLAB.flatBlocks) { condition(ExplosionCondition.survivesExplosion()) }

	lootTables.dropsSelf(MRegistry.FLOOR_TILES_STAIRS.blocks.values) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.createSlabItemTable(MRegistry.FLOOR_TILES_SLAB.blocks.values) { condition(ExplosionCondition.survivesExplosion()) }

	lootTables.dropsSelf(MBlocks.CARBON_FIBRE_BLOCK) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MBlocks.TRITANIUM_RAW_BLOCK) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MBlocks.TRITANIUM_STRIPED_BLOCK) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MBlocks.TRITANIUM_STRIPED_WALL) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MBlocks.TRITANIUM_STRIPED_STAIRS) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.createSlabItemTable(MBlocks.TRITANIUM_STRIPED_SLAB) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MBlocks.MATTER_CABLE) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MBlocks.GRAVITATION_STABILIZER)
	lootTables.dropsOther(MBlocks.GRAVITATION_STABILIZER_LENS, MBlocks.GRAVITATION_STABILIZER)

	lootTables.dropsSelf(MBlocks.LABORATORY_LAMP) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MBlocks.LABORATORY_LAMP_INVERTED) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MBlocks.DANGER_STRIPE_BLOCK) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MBlocks.METAL_BEAM) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MBlocks.TRITANIUM_INGOT_BLOCK) { condition(ExplosionCondition.survivesExplosion()) }
	lootTables.dropsSelf(MBlocks.TRITANIUM_BARS) { condition(ExplosionCondition.survivesExplosion()) }

	lootTables.dropsSelf(MBlocks.ENGINE) { condition(ExplosionCondition.survivesExplosion()) }

	for (block in MBlocks.TRITANIUM_ANVIL)
		lootTables.dropsSelf(block) { condition(ExplosionCondition.survivesExplosion()) }

	for (door in MBlocks.TRITANIUM_TRAPDOOR.values)
		lootTables.dropsSelf(door) { condition(ExplosionCondition.survivesExplosion()) }

	lootTables.dropsSelf(MRegistry.TRITANIUM_PRESSURE_PLATE.allBlocks.values) { condition(ExplosionCondition.survivesExplosion()) }

	lootTables.block(MBlocks.PHANTOM_ATTRACTOR) {
		item(MBlocks.PHANTOM_ATTRACTOR) {
			blockStateCondition(MBlocks.PHANTOM_ATTRACTOR) {
				this[BlockStateProperties.DOUBLE_BLOCK_HALF] = DoubleBlockHalf.LOWER
			}
		}

		condition(ExplosionCondition.survivesExplosion())
	}

	for (door in MBlocks.TRITANIUM_DOOR.values) {
		lootTables.block(door) {
			item(door) {
				blockStateCondition(door) {
					this[BlockStateProperties.DOUBLE_BLOCK_HALF] = DoubleBlockHalf.LOWER
				}
			}

			condition(ExplosionCondition.survivesExplosion())
		}
	}

	lootTables.builder(LootContextParamSets.ADVANCEMENT_ENTITY, modLocation("research_all_android")) {
		lootPool {
			add(LootItem.lootTableItem(MItems.ExopackUpgrades.INVENTORY_UPGRADE_HUGE))
		}
	}

	lootTables.builder(LootContextParamSets.ADVANCEMENT_ENTITY, modLocation("tritanium_block3")) {
		lootPool { item(Items.WHITE_DYE) { setCount(8) } }
		lootPool { item(Items.ORANGE_DYE) { setCount(8) } }
		lootPool { item(Items.MAGENTA_DYE) { setCount(8) } }
		lootPool { item(Items.LIGHT_BLUE_DYE) { setCount(8) } }
		lootPool { item(Items.YELLOW_DYE) { setCount(8) } }
		lootPool { item(Items.LIME_DYE) { setCount(8) } }
		lootPool { item(Items.PINK_DYE) { setCount(8) } }
		lootPool { item(Items.GRAY_DYE) { setCount(8) } }
		lootPool { item(Items.LIGHT_GRAY_DYE) { setCount(8) } }
		lootPool { item(Items.CYAN_DYE) { setCount(8) } }
		lootPool { item(Items.PURPLE_DYE) { setCount(8) } }
		lootPool { item(Items.BLUE_DYE) { setCount(8) } }
		lootPool { item(Items.BROWN_DYE) { setCount(8) } }
		lootPool { item(Items.GREEN_DYE) { setCount(8) } }
		lootPool { item(Items.RED_DYE) { setCount(8) } }
		lootPool { item(Items.BLACK_DYE) { setCount(8) } }
	}

	lootTables.builder(LootContextParamSets.ADVANCEMENT_ENTITY, modLocation("tritanium_block4")) {
		lootPool { item(Items.WHITE_DYE) { setCount(64) } }
		lootPool { item(Items.ORANGE_DYE) { setCount(64) } }
		lootPool { item(Items.MAGENTA_DYE) { setCount(64) } }
		lootPool { item(Items.LIGHT_BLUE_DYE) { setCount(64) } }
		lootPool { item(Items.YELLOW_DYE) { setCount(64) } }
		lootPool { item(Items.LIME_DYE) { setCount(64) } }
		lootPool { item(Items.PINK_DYE) { setCount(64) } }
		lootPool { item(Items.GRAY_DYE) { setCount(64) } }
		lootPool { item(Items.LIGHT_GRAY_DYE) { setCount(64) } }
		lootPool { item(Items.CYAN_DYE) { setCount(64) } }
		lootPool { item(Items.PURPLE_DYE) { setCount(64) } }
		lootPool { item(Items.BLUE_DYE) { setCount(64) } }
		lootPool { item(Items.BROWN_DYE) { setCount(64) } }
		lootPool { item(Items.GREEN_DYE) { setCount(64) } }
		lootPool { item(Items.RED_DYE) { setCount(64) } }
		lootPool { item(Items.BLACK_DYE) { setCount(64) } }
	}

	lootTables.tile(MBlocks.COBBLESTONE_GENERATOR)
	lootTables.tile(MBlocks.ESSENCE_STORAGE)
	lootTables.tile(MBlocks.MATTER_RECONSTRUCTOR)
	lootTables.tile(MBlocks.FLUID_TANK)

	lootTables.tile(MBlocks.ENERGY_SERVO)
	lootTables.tile(MBlocks.ENERGY_COUNTER)
	lootTables.tile(MBlocks.CHEMICAL_GENERATOR)
	lootTables.tile(MBlocks.HOLO_SIGN, "isLocked")
	lootTables.tile(MBlocks.STORAGE_CABLE)
	lootTables.tile(MBlocks.ANDROID_STATION)
	lootTables.tile(MBlocks.ANDROID_CHARGER)
	lootTables.tile(MBlocks.BATTERY_BANK)
	lootTables.tile(MBlocks.DRIVE_VIEWER)

	lootTables.tile(MBlocks.STORAGE_BUS)
	lootTables.tile(MBlocks.STORAGE_IMPORTER)
	lootTables.tile(MBlocks.STORAGE_EXPORTER)
	lootTables.tile(MBlocks.STORAGE_POWER_SUPPLIER)
	lootTables.tile(MBlocks.DRIVE_RACK)

	lootTables.tile(MBlocks.MATTER_DECOMPOSER)
	lootTables.tile(MBlocks.MATTER_REPLICATOR)
	lootTables.tile(MBlocks.MATTER_RECYCLER)
	lootTables.tile(MBlocks.MATTER_SCANNER)
	lootTables.tile(MBlocks.PLATE_PRESS)
	lootTables.tile(MBlocks.TWIN_PLATE_PRESS)

	lootTables.tile(MBlocks.POWERED_FURNACE)
	lootTables.tile(MBlocks.POWERED_SMOKER)
	lootTables.tile(MBlocks.POWERED_BLAST_FURNACE)

	lootTables.tile(MBlocks.MATTER_PANEL)
	lootTables.tile(MBlocks.PATTERN_STORAGE)
	lootTables.tile(MBlocks.MATTER_CAPACITOR_BANK)
	lootTables.tile(MBlocks.MATTER_BOTTLER)
}
