package net.minecraft.world.inventory;

import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import javax.annotation.Nullable;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.Container;

public abstract class ItemCombinerMenu extends AbstractContainerMenu {
    protected final Container resultSlots;
    protected final Container inputSlots;
    protected final ContainerLevelAccess access;
    protected final Player player;
    
    protected abstract boolean mayPickup(final Player bec, final boolean boolean2);
    
    protected abstract ItemStack onTake(final Player bec, final ItemStack bki);
    
    protected abstract boolean isValidBlock(final BlockState cfj);
    
    public ItemCombinerMenu(@Nullable final MenuType<?> bhk, final int integer, final Inventory beb, final ContainerLevelAccess bgs) {
        super(bhk, integer);
        this.resultSlots = new ResultContainer();
        this.inputSlots = new SimpleContainer(2) {
            @Override
            public void setChanged() {
                super.setChanged();
                ItemCombinerMenu.this.slotsChanged(this);
            }
        };
        this.access = bgs;
        this.player = beb.player;
        this.addSlot(new Slot(this.inputSlots, 0, 27, 47));
        this.addSlot(new Slot(this.inputSlots, 1, 76, 47));
        this.addSlot(new Slot(this.resultSlots, 2, 134, 47) {
            @Override
            public boolean mayPlace(final ItemStack bki) {
                return false;
            }
            
            @Override
            public boolean mayPickup(final Player bec) {
                return ItemCombinerMenu.this.mayPickup(bec, this.hasItem());
            }
            
            @Override
            public ItemStack onTake(final Player bec, final ItemStack bki) {
                return ItemCombinerMenu.this.onTake(bec, bki);
            }
        });
        for (int integer2 = 0; integer2 < 3; ++integer2) {
            for (int integer3 = 0; integer3 < 9; ++integer3) {
                this.addSlot(new Slot(beb, integer3 + integer2 * 9 + 9, 8 + integer3 * 18, 84 + integer2 * 18));
            }
        }
        for (int integer2 = 0; integer2 < 9; ++integer2) {
            this.addSlot(new Slot(beb, integer2, 8 + integer2 * 18, 142));
        }
    }
    
    public abstract void createResult();
    
    @Override
    public void slotsChanged(final Container amz) {
        super.slotsChanged(amz);
        if (amz == this.inputSlots) {
            this.createResult();
        }
    }
    
    @Override
    public void removed(final Player bec) {
        super.removed(bec);
        this.access.execute((bqb, fu) -> this.clearContainer(bec, bqb, this.inputSlots));
    }
    
    @Override
    public boolean stillValid(final Player bec) {
        return this.access.<Boolean>evaluate((bqb, fu) -> {
            if (!this.isValidBlock(bqb.getBlockState(fu))) {
                return false;
            }
            else {
                return bec.distanceToSqr(fu.getX() + 0.5, fu.getY() + 0.5, fu.getZ() + 0.5) <= 64.0;
            }
        }, Boolean.valueOf(true));
    }
    
    protected boolean shouldQuickMoveToAdditionalSlot(final ItemStack bki) {
        return false;
    }
    
    @Override
    public ItemStack quickMoveStack(final Player bec, final int integer) {
        ItemStack bki4 = ItemStack.EMPTY;
        final Slot bhw5 = this.slots.get(integer);
        if (bhw5 != null && bhw5.hasItem()) {
            final ItemStack bki5 = bhw5.getItem();
            bki4 = bki5.copy();
            if (integer == 2) {
                if (!this.moveItemStackTo(bki5, 3, 39, true)) {
                    return ItemStack.EMPTY;
                }
                bhw5.onQuickCraft(bki5, bki4);
            }
            else if (integer == 0 || integer == 1) {
                if (!this.moveItemStackTo(bki5, 3, 39, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 3 && integer < 39) {
                final int integer2 = this.shouldQuickMoveToAdditionalSlot(bki4) ? 1 : 0;
                if (!this.moveItemStackTo(bki5, integer2, 2, false)) {
                    return ItemStack.EMPTY;
                }
            }
            if (bki5.isEmpty()) {
                bhw5.set(ItemStack.EMPTY);
            }
            else {
                bhw5.setChanged();
            }
            if (bki5.getCount() == bki4.getCount()) {
                return ItemStack.EMPTY;
            }
            bhw5.onTake(bec, bki5);
        }
        return bki4;
    }
}
