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

import com.google.common.collect.ImmutableList
import net.minecraft.world.Container
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.ItemHatchBlockEntity
import ru.dbotthepony.mc.otm.menu.MatteryMenu
import ru.dbotthepony.mc.otm.menu.UserFilteredSlot
import ru.dbotthepony.mc.otm.menu.input.EnumInputWithFeedback
import ru.dbotthepony.mc.otm.menu.makeSlots
import ru.dbotthepony.mc.otm.registry.game.MMenus

class ItemHatchMenu(
	val isInput: Boolean,
	containerId: Int,
	inventory: Inventory,
	tile: ItemHatchBlockEntity? = null
) : MatteryMenu(if (isInput) MMenus.ITEM_INPUT_HATCH else MMenus.ITEM_OUTPUT_HATCH, containerId, inventory, tile) {
	val actualContainer: Container = tile?.container ?: SimpleContainer(ItemHatchBlockEntity.CAPACITY)
	val storageSlots: ImmutableList<UserFilteredSlot> = makeSlots(actualContainer) { a, b ->
		object : UserFilteredSlot(a, b) {
			override fun mayPlace(itemStack: ItemStack): Boolean {
				return isInput && super.mayPlace(itemStack)
			}
		}
	}

	val sort = SortInput(actualContainer, playerSortSettings)
	val redstone = EnumInputWithFeedback<RedstoneSetting>(this)

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

		sort.input.filter { isInput }
		addStorageSlot(storageSlots)
		addInventorySlots()
	}

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

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