package ru.dbotthepony.mc.otm.menu.tech

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.RedstoneSetting
import ru.dbotthepony.mc.otm.block.entity.tech.EnergyServoBlockEntity
import ru.dbotthepony.mc.otm.capability.energy
import ru.dbotthepony.mc.otm.menu.MatteryMenu
import ru.dbotthepony.mc.otm.menu.MatterySlot
import ru.dbotthepony.mc.otm.menu.input.EnergyConfigPlayerInput
import ru.dbotthepony.mc.otm.menu.input.EnumInputWithFeedback
import ru.dbotthepony.mc.otm.menu.input.ItemConfigPlayerInput
import ru.dbotthepony.mc.otm.menu.widget.ProfiledLevelGaugeWidget
import ru.dbotthepony.mc.otm.registry.MMenus

class EnergyServoMenu @JvmOverloads constructor(
	p_38852_: Int,
	inventory: Inventory,
	tile: EnergyServoBlockEntity? = null
) : MatteryMenu(MMenus.ENERGY_SERVO, p_38852_, inventory, tile) {
	val dischargeSlot = object : MatterySlot(tile?.discharge ?: SimpleContainer(1), 0) {
		override fun mayPlace(itemStack: ItemStack): Boolean {
			return super.mayPlace(itemStack) && (itemStack.energy?.canExtract() ?: false)
		}
	}

	val chargeSlot = object : MatterySlot(tile?.charge ?: SimpleContainer(1), 0) {
		override fun mayPlace(itemStack: ItemStack): Boolean {
			return super.mayPlace(itemStack) && (itemStack.energy?.canReceive() ?: false)
		}
	}

	val equipment = makeEquipmentSlots(mapMoveToExternal = true)

	val powerGauge = ProfiledLevelGaugeWidget(this, tile?.energy)
	val itemConfig = ItemConfigPlayerInput(this, tile?.itemConfig)
	val energyConfig = EnergyConfigPlayerInput(this, tile?.energyConfig, allowPull = true, allowPush = true)
	val redstoneConfig = EnumInputWithFeedback<RedstoneSetting>(this)

	init {
		if (tile != null) {
			redstoneConfig.with(tile.redstoneControl::redstoneSetting)
		}
	}

	init {
		// charge then discharge for charging to take priority over discharging
		addStorageSlot(chargeSlot)
		addStorageSlot(dischargeSlot)

		addInventorySlots()
	}
}
