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

import net.minecraft.world.Container
import net.minecraft.world.SimpleContainer
import net.minecraft.world.entity.player.Inventory
import ru.dbotthepony.mc.otm.block.entity.RedstoneSetting
import ru.dbotthepony.mc.otm.block.entity.tech.EnergyHatchBlockEntity
import ru.dbotthepony.mc.otm.capability.FlowDirection
import ru.dbotthepony.mc.otm.menu.EnergyContainerInputSlot
import ru.dbotthepony.mc.otm.menu.MatteryMenu
import ru.dbotthepony.mc.otm.menu.input.EnumInputWithFeedback
import ru.dbotthepony.mc.otm.menu.makeSlots
import ru.dbotthepony.mc.otm.menu.widget.ProfiledLevelGaugeWidget
import ru.dbotthepony.mc.otm.registry.game.MMenus

class EnergyHatchMenu(
	val isInput: Boolean,
	containerId: Int,
	inventory: Inventory,
	tile: EnergyHatchBlockEntity? = null
) : MatteryMenu(if (isInput) MMenus.ENERGY_INPUT_HATCH else MMenus.ENERGY_OUTPUT_HATCH, containerId, inventory, tile) {
	val container: Container = tile?.container ?: SimpleContainer(EnergyHatchBlockEntity.CAPACITY)

	val inputSlots = makeSlots(container) { a, b ->
		EnergyContainerInputSlot(a, b, direction = FlowDirection.input(isInput))
	}

	val gauge = ProfiledLevelGaugeWidget(this, tile?.energy)
	val redstone = EnumInputWithFeedback<RedstoneSetting>(this)

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

		addStorageSlot(inputSlots)
		addInventorySlots()
	}

	companion object {
		fun input(containerId: Int, inventory: Inventory, tile: EnergyHatchBlockEntity? = null): EnergyHatchMenu {
			return EnergyHatchMenu(true, containerId, inventory, tile)
		}

		fun output(containerId: Int, inventory: Inventory, tile: EnergyHatchBlockEntity? = null): EnergyHatchMenu {
			return EnergyHatchMenu(false, containerId, inventory, tile)
		}
	}
}
