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

import net.minecraft.world.SimpleContainer
import net.minecraft.world.entity.player.Inventory
import net.minecraft.world.item.ItemStack
import ru.dbotthepony.mc.otm.block.entity.matter.MatterReconstructorBlockEntity
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.LevelGaugeWidget
import ru.dbotthepony.mc.otm.menu.widget.ProfiledLevelGaugeWidget
import ru.dbotthepony.mc.otm.menu.widget.ProgressGaugeWidget
import ru.dbotthepony.mc.otm.registry.MMenus

class MatterReconstructorMenu(
	containerId: Int,
	inventory: Inventory,
	tile: MatterReconstructorBlockEntity? = null
) : MatteryPoweredMenu(MMenus.ITEM_REPAIER, containerId, inventory, tile) {
	val matterWidget = ProfiledLevelGaugeWidget(this, tile?.matter, LevelGaugeWidget(this, tile?.matter))
	val slot = object : MatterySlot(tile?.repairContainer ?: SimpleContainer(1), 0) {
		override fun mayPlace(itemStack: ItemStack): Boolean {
			return itemStack.isRepairable && itemStack.isDamaged && super.mayPlace(itemStack)
		}
	}

	val equipment = makeEquipmentSlots(mapMoveToExternal = true)
	val progress = ProgressGaugeWidget(this)

	val energyConfig = EnergyConfigPlayerInput(this, tile?.energyConfig)
	val itemConfig = ItemConfigPlayerInput(this, tile?.itemConfig)
	val profiledEnergy = ProfiledLevelGaugeWidget(this, tile?.energy, energyWidget)

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

	init {
		addStorageSlot(slot)
		addInventorySlots()

		if (tile != null) {
			progress.with(tile::visualProgress, tile::isUnableToProcess)
		}
	}
}
