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.MatterHatchBlockEntity
import ru.dbotthepony.mc.otm.capability.FlowDirection
import ru.dbotthepony.mc.otm.menu.MatterContainerInputSlot
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 MatterHatchMenu(
	val isInput: Boolean,
	containerId: Int,
	inventory: Inventory,
	tile: MatterHatchBlockEntity? = null
) : MatteryMenu(if (isInput) MMenus.MATTER_INPUT_HATCH else MMenus.MATTER_OUTPUT_HATCH, containerId, inventory, tile) {
	val container: Container = tile?.container ?: SimpleContainer(MatterHatchBlockEntity.CAPACITY)

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

	val gauge = ProfiledLevelGaugeWidget(this, tile?.matter)
	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: MatterHatchBlockEntity? = null): MatterHatchMenu {
			return MatterHatchMenu(true, containerId, inventory, tile)
		}

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