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

import kotlin.jvm.JvmOverloads
import net.minecraft.world.entity.player.Inventory
import ru.dbotthepony.mc.otm.block.entity.matter.MatterDecomposerBlockEntity
import ru.dbotthepony.mc.otm.menu.widget.ProgressGaugeWidget
import ru.dbotthepony.mc.otm.menu.widget.LevelGaugeWidget
import net.minecraft.world.SimpleContainer
import net.minecraft.world.item.ItemStack
import ru.dbotthepony.mc.otm.capability.MatteryCapability
import ru.dbotthepony.mc.otm.core.orNull
import ru.dbotthepony.mc.otm.matter.MatterManager
import ru.dbotthepony.mc.otm.menu.MachineOutputSlot
import ru.dbotthepony.mc.otm.menu.MatteryPoweredMenu
import ru.dbotthepony.mc.otm.menu.MatterySlot
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

class MatterDecomposerMenu @JvmOverloads constructor(
	containerID: Int,
	inventory: Inventory,
	tile: MatterDecomposerBlockEntity? = null
) : MatteryPoweredMenu(MMenus.MATTER_DECOMPOSER, containerID, inventory, tile) {
	val input = object : MatterySlot(tile?.inputContainer ?: SimpleContainer(1), 0) {
		override fun mayPlace(itemStack: ItemStack) = MatterManager.canDecompose(itemStack)
	}

	val outputMain: MachineOutputSlot
	val outputStacking: MachineOutputSlot
	val progressWidget = ProgressGaugeWidget(this, tile)
	val matterWidget = ProfiledLevelGaugeWidget(this, tile?.matter, LevelGaugeWidget(this, tile?.matter))
	val itemConfig = ItemConfigPlayerInput(this, tile?.itemConfig)
	val energyConfig = EnergyConfigPlayerInput(this, tile?.energyConfig)
	val profiledEnergy = ProfiledLevelGaugeWidget(this, tile?.energy, energyWidget)

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

		// Выход
		outputMain = MachineOutputSlot(container, 0)
		outputStacking = MachineOutputSlot(container, 1)

		addStorageSlot(outputMain)
		addStorageSlot(outputStacking)
		addStorageSlot(input)

		addInventorySlots()
	}
}
