package ru.dbotthepony.mc.otm.menu.matter

import net.minecraft.server.level.ServerPlayer
import kotlin.jvm.JvmOverloads
import net.minecraft.world.entity.player.Inventory
import ru.dbotthepony.mc.otm.block.entity.matter.MatterReplicatorBlockEntity
import ru.dbotthepony.mc.otm.menu.widget.LevelGaugeWidget
import ru.dbotthepony.mc.otm.menu.widget.ProgressGaugeWidget
import net.minecraft.world.SimpleContainer
import ru.dbotthepony.mc.otm.core.immutableList
import ru.dbotthepony.mc.otm.core.isNotEmpty
import ru.dbotthepony.mc.otm.item.matter.MatterDustItem
import ru.dbotthepony.mc.otm.menu.MachineOutputSlot
import ru.dbotthepony.mc.otm.menu.MatteryPoweredMenu
import ru.dbotthepony.mc.otm.menu.input.EnergyConfigPlayerInput
import ru.dbotthepony.mc.otm.menu.input.ItemConfigPlayerInput
import ru.dbotthepony.mc.otm.menu.widget.ProfiledLevelGaugeWidget
import ru.dbotthepony.mc.otm.registry.MMenus
import ru.dbotthepony.mc.otm.triggers.TakeItemOutOfReplicatorTrigger

class MatterReplicatorMenu @JvmOverloads constructor(
	p_38852_: Int,
	inventory: Inventory,
	tile: MatterReplicatorBlockEntity? = null
) : MatteryPoweredMenu(MMenus.MATTER_REPLICATOR, p_38852_, inventory, tile) {
	val matter = ProfiledLevelGaugeWidget(this, tile?.matter, LevelGaugeWidget(this, tile?.matter))
	val progress = ProgressGaugeWidget(this, tile)
	val storageSlots: List<MachineOutputSlot>
	val itemConfig = ItemConfigPlayerInput(this, tile?.itemConfig)
	val energyConfig = EnergyConfigPlayerInput(this, tile?.energyConfig)
	val profiledEnergy = ProfiledLevelGaugeWidget(this, tile?.energy, energyWidget)

	val upgrades = makeUpgradeSlots(3, tile?.upgrades)

	init {
		val container = tile?.container ?: SimpleContainer(5)

		storageSlots = immutableList(5) {
			addStorageSlot(MachineOutputSlot(container, it, onTake = {
				if (inventory.player is ServerPlayer && it.isNotEmpty)
					TakeItemOutOfReplicatorTrigger.trigger(inventory.player as ServerPlayer, it) }))
		}

		addInventorySlots()
	}
}
