package net.minecraft.world.inventory;

import net.minecraft.core.BlockPos;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.Container;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.entity.player.Inventory;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.world.item.crafting.UpgradeRecipe;
import net.minecraft.world.level.Level;

public class SmithingMenu extends ItemCombinerMenu {
    private final Level level;
    @Nullable
    private UpgradeRecipe selectedRecipe;
    private final List<UpgradeRecipe> recipes;
    
    public SmithingMenu(final int integer, final Inventory beb) {
        this(integer, beb, ContainerLevelAccess.NULL);
    }
    
    public SmithingMenu(final int integer, final Inventory beb, final ContainerLevelAccess bgs) {
        super(MenuType.SMITHING, integer, beb, bgs);
        this.level = beb.player.level;
        this.recipes = this.level.getRecipeManager().<Container, UpgradeRecipe>getAllRecipesFor(RecipeType.SMITHING);
    }
    
    @Override
    protected boolean isValidBlock(final BlockState cfj) {
        return cfj.is(Blocks.SMITHING_TABLE);
    }
    
    @Override
    protected boolean mayPickup(final Player bec, final boolean boolean2) {
        return this.selectedRecipe != null && this.selectedRecipe.matches(this.inputSlots, this.level);
    }
    
    @Override
    protected ItemStack onTake(final Player bec, final ItemStack bki) {
        this.shrinkStackInSlot(0);
        this.shrinkStackInSlot(1);
        this.access.execute((bqb, fu) -> bqb.levelEvent(1044, fu, 0));
        return bki;
    }
    
    private void shrinkStackInSlot(final int integer) {
        final ItemStack bki3 = this.inputSlots.getItem(integer);
        bki3.shrink(1);
        this.inputSlots.setItem(integer, bki3);
    }
    
    @Override
    public void createResult() {
        final List<UpgradeRecipe> list2 = this.level.getRecipeManager().<Container, UpgradeRecipe>getRecipesFor(RecipeType.SMITHING, this.inputSlots, this.level);
        if (list2.isEmpty()) {
            this.resultSlots.setItem(0, ItemStack.EMPTY);
        }
        else {
            this.selectedRecipe = list2.get(0);
            final ItemStack bki3 = this.selectedRecipe.assemble(this.inputSlots);
            this.resultSlots.setItem(0, bki3);
        }
    }
    
    @Override
    protected boolean shouldQuickMoveToAdditionalSlot(final ItemStack bki) {
        return this.recipes.stream().anyMatch(bnl -> bnl.isAdditionIngredient(bki));
    }
}
