package net.minecraft.world.inventory;

import javax.annotation.Nullable;
import net.minecraft.core.NonNullList;
import net.minecraft.world.Container;
import net.minecraft.world.ContainerHelper;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeHolder;

public class ResultContainer implements Container, RecipeCraftingHolder {
    private final NonNullList<ItemStack> itemStacks = NonNullList.withSize(1, ItemStack.EMPTY);
    @Nullable
    private RecipeHolder<?> recipeUsed;
    // CraftBukkit start
    private int maxStack = MAX_STACK;

    @Override
    public java.util.List<ItemStack> getContents() {
        return this.itemStacks;
    }

    @Override
    public @Nullable org.bukkit.inventory.InventoryHolder getOwner() {
        // Paper start - Add missing InventoryHolders
        if (this.holder == null && this.holderCreator != null) {
            this.holder = this.holderCreator.get();
        }
        return this.holder; // Result slots don't get an owner
        // Paper end - Add missing InventoryHolders
    }

    // Don't need a transaction; the InventoryCrafting keeps track of it for us
    public void onOpen(org.bukkit.craftbukkit.entity.CraftHumanEntity player) {}
    public void onClose(org.bukkit.craftbukkit.entity.CraftHumanEntity player) {}
    public java.util.List<org.bukkit.entity.HumanEntity> getViewers() {
        return new java.util.ArrayList<>();
    }

    @Override
    public int getMaxStackSize() {
        return this.maxStack;
    }

    public void setMaxStackSize(int size) {
        this.maxStack = size;
    }

    @Override
    public @Nullable org.bukkit.Location getLocation() {
        return null;
    }
    // CraftBukkit end
    // Paper start - Add missing InventoryHolders
    private @Nullable java.util.function.Supplier<? extends org.bukkit.inventory.InventoryHolder> holderCreator;
    private @Nullable org.bukkit.inventory.InventoryHolder holder;
    public ResultContainer(java.util.function.Supplier<? extends org.bukkit.inventory.InventoryHolder> holderCreator) {
        this.holderCreator = holderCreator;
    }

    public ResultContainer() {
    }
    // Paper end - Add missing InventoryHolders

    @Override
    public int getContainerSize() {
        return 1;
    }

    @Override
    public boolean isEmpty() {
        for (ItemStack itemStack : this.itemStacks) {
            if (!itemStack.isEmpty()) {
                return false;
            }
        }

        return true;
    }

    @Override
    public ItemStack getItem(int index) {
        return this.itemStacks.get(0);
    }

    @Override
    public ItemStack removeItem(int index, int count) {
        return ContainerHelper.takeItem(this.itemStacks, 0);
    }

    @Override
    public ItemStack removeItemNoUpdate(int index) {
        return ContainerHelper.takeItem(this.itemStacks, 0);
    }

    @Override
    public void setItem(int index, ItemStack stack) {
        this.itemStacks.set(0, stack);
    }

    @Override
    public void setChanged() {
    }

    @Override
    public boolean stillValid(Player player) {
        return true;
    }

    @Override
    public void clearContent() {
        this.itemStacks.clear();
    }

    @Override
    public void setRecipeUsed(@Nullable RecipeHolder<?> recipe) {
        this.recipeUsed = recipe;
    }

    @Nullable
    @Override
    public RecipeHolder<?> getRecipeUsed() {
        return this.recipeUsed;
    }
}
