package ru.dbotthepony.mc.otm.container

import net.minecraft.world.entity.player.Player
import net.minecraft.world.entity.player.StackedContents
import net.minecraft.world.inventory.CraftingContainer
import net.minecraft.world.item.ItemStack
import ru.dbotthepony.mc.otm.container.util.iterator
import ru.dbotthepony.mc.otm.core.collect.toList

class ShadowCraftingContainer private constructor(private val parent: CraftingContainer, private val shadow: ShadowContainer) : CraftingContainer(DummyMenu, parent.width, parent.height), IContainer by shadow {
	constructor(parent: CraftingContainer) : this(parent, ShadowContainer(parent))

	override fun fillStackedContents(contents: StackedContents) {
		for (item in iterator()) {
			contents.accountStack(item)
		}
	}

	override fun getWidth(): Int {
		return parent.width
	}

	override fun getHeight(): Int {
		return parent.height
	}

	override fun clearContent() {
		shadow.clearContent()
	}

	override fun getContainerSize(): Int {
		return shadow.getContainerSize()
	}

	override fun isEmpty(): Boolean {
		return shadow.isEmpty()
	}

	override fun getItem(p_39332_: Int): ItemStack {
		return shadow.getItem(p_39332_)
	}

	override fun removeItem(p_39334_: Int, p_39335_: Int): ItemStack {
		return shadow.removeItem(p_39334_, p_39335_)
	}

	override fun removeItemNoUpdate(p_39344_: Int): ItemStack {
		return shadow.removeItemNoUpdate(p_39344_)
	}

	override fun setItem(p_39337_: Int, p_39338_: ItemStack) {
		shadow.setItem(p_39337_, p_39338_)
	}

	override fun setChanged() {
		shadow.setChanged()
	}

	override fun stillValid(p_39340_: Player): Boolean {
		return shadow.stillValid(p_39340_)
	}

	companion object {
		fun shadow(container: CraftingContainer, slot: Int, itemStack: ItemStack): CraftingContainer {
			return ShadowCraftingContainer(container).also { it[slot] = itemStack }
		}
	}
}
