package ru.dbotthepony.mc.otm.container

import net.minecraft.world.Container
import net.minecraft.world.item.Item
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Items
import ru.dbotthepony.kommons.util.Delegate
import ru.dbotthepony.mc.otm.core.isNotEmpty

/**
 * While this somewhat similar to [net.minecraft.world.inventory.Slot], this slot is not meant
 * for Player interaction.
 */
interface IContainerSlot : Delegate<ItemStack> {
	val slot: Int
	val container: Container

	operator fun component1() = slot
	operator fun component2() = item

	fun getMaxStackSize(item: ItemStack = this.item): Int {
		return container.maxStackSize
	}

	val isForbiddenForAutomation: Boolean get() {
		return getFilter() === Items.AIR
	}

	fun getFilter(): Item?

	/**
	 * @return whenever the filter was set. Returns false only if container can't be filtered.
	 */
	fun setFilter(filter: Item? = null): Boolean

	val hasFilter: Boolean
		get() = getFilter() != null

	fun remove() {
		container[slot] = ItemStack.EMPTY
	}

	fun remove(count: Int): ItemStack {
		return container.removeItem(slot, count)
	}

	override fun get(): ItemStack {
		return container[slot]
	}

	override fun accept(t: ItemStack) {
		container[slot] = t
	}

	fun setChanged() {
		container.setChanged()
	}

	var item: ItemStack
		get() = container[slot]
		set(value) { container[slot] = value }

	val isEmpty: Boolean
		get() = container[slot].isEmpty

	val isNotEmpty: Boolean
		get() = container[slot].isNotEmpty
}
