package ru.dbotthepony.mc.otm.menu.decorative

import net.minecraft.world.Container
import net.minecraft.world.SimpleContainer
import net.minecraft.world.entity.player.Inventory
import net.minecraft.world.entity.player.Player
import ru.dbotthepony.mc.otm.block.entity.decorative.CargoCrateBlockEntity
import ru.dbotthepony.mc.otm.menu.MatteryMenu
import ru.dbotthepony.mc.otm.menu.UserFilteredSlot
import ru.dbotthepony.mc.otm.menu.makeSlots
import ru.dbotthepony.mc.otm.registry.game.MMenus

class CargoCrateMenu(
	containerId: Int,
	inventory: Inventory,
	tile: CargoCrateBlockEntity? = null
) : MatteryMenu(MMenus.CARGO_CRATE, containerId, inventory, tile) {
	val actualContainer: Container = tile?.container ?: SimpleContainer(CargoCrateBlockEntity.CAPACITY)
	val storageSlots = makeSlots(actualContainer, ::UserFilteredSlot)
	private val trackedPlayerOpen = !inventory.player.isSpectator

	val sort = SortInput(actualContainer, playerSortSettings)

	init {
		if (trackedPlayerOpen) {
			tile?.onPlayerOpen()
		}

		addStorageSlot(storageSlots)
		addInventorySlots()
	}

	override fun removed(p_38940_: Player) {
		super.removed(p_38940_)

		if (trackedPlayerOpen) {
			(tile as? CargoCrateBlockEntity)?.onPlayerClose()
		}
	}
}
