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.MatterScannerBlockEntity
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.core.math.Decimal
import ru.dbotthepony.mc.otm.matter.MatterManager
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 MatterScannerMenu @JvmOverloads constructor(
	p_38852_: Int,
	inventory: Inventory,
	tile: MatterScannerBlockEntity? = null
) : MatteryPoweredMenu(MMenus.MATTER_SCANNER, p_38852_, inventory, tile) {
	val input: MatterySlot
	val progress = ProgressGaugeWidget(this, tile)
	val patterns = LevelGaugeWidget(this)
	val itemConfig = ItemConfigPlayerInput(this, tile?.itemConfig)
	val energyConfig = EnergyConfigPlayerInput(this, tile?.energyConfig)
	val profiledEnergy = ProfiledLevelGaugeWidget(this, tile?.energy, energyWidget)

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

		input = object : MatterySlot(container, 0, 64, 38) {
			override fun mayPlace(itemStack: ItemStack) = MatterManager.canDecompose(itemStack)
		}

		addStorageSlot(input)

		if (tile != null) {
			patterns.with(
				{ Decimal(tile.matterNode.graph.patternCount) },
				{ Decimal(tile.matterNode.graph.patternCapacity) })
		}

		addInventorySlots()
	}
}
