package net.minecraft.world.inventory;

import net.minecraft.world.level.block.entity.AbstractFurnaceBlockEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Player;
import net.minecraft.recipebook.ServerPlaceSmeltingRecipe;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.entity.player.StackedContents;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.crafting.AbstractCookingRecipe;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.Level;
import net.minecraft.world.Container;

public abstract class AbstractFurnaceMenu extends RecipeBookMenu<Container> {
    private final Container container;
    private final ContainerData data;
    protected final Level level;
    private final RecipeType<? extends AbstractCookingRecipe> recipeType;
    
    protected AbstractFurnaceMenu(final MenuType<?> bbo, final RecipeType<? extends AbstractCookingRecipe> bgz, final int integer, final Inventory ayi) {
        this(bbo, bgz, integer, ayi, new SimpleContainer(3), new SimpleContainerData(4));
    }
    
    protected AbstractFurnaceMenu(final MenuType<?> bbo, final RecipeType<? extends AbstractCookingRecipe> bgz, final int integer, final Inventory ayi, final Container aje, final ContainerData baw) {
        super(bbo, integer);
        this.recipeType = bgz;
        AbstractContainerMenu.checkContainerSize(aje, 3);
        AbstractContainerMenu.checkContainerDataCount(baw, 4);
        this.container = aje;
        this.data = baw;
        this.level = ayi.player.level;
        this.addSlot(new Slot(aje, 0, 56, 17));
        this.addSlot(new FurnaceFuelSlot(this, aje, 1, 56, 53));
        this.addSlot(new FurnaceResultSlot(ayi.player, aje, 2, 116, 35));
        for (int integer2 = 0; integer2 < 3; ++integer2) {
            for (int integer3 = 0; integer3 < 9; ++integer3) {
                this.addSlot(new Slot(ayi, integer3 + integer2 * 9 + 9, 8 + integer3 * 18, 84 + integer2 * 18));
            }
        }
        for (int integer2 = 0; integer2 < 9; ++integer2) {
            this.addSlot(new Slot(ayi, integer2, 8 + integer2 * 18, 142));
        }
        this.addDataSlots(baw);
    }
    
    @Override
    public void fillCraftSlotsStackedContents(final StackedContents ayl) {
        if (this.container instanceof StackedContentsCompatible) {
            ((StackedContentsCompatible)this.container).fillStackedContents(ayl);
        }
    }
    
    @Override
    public void clearCraftingContent() {
        this.container.clearContent();
    }
    
    @Override
    public void handlePlacement(final boolean boolean1, final Recipe<?> bgw, final ServerPlayer xe) {
        new ServerPlaceSmeltingRecipe<>(this).recipeClicked(xe, bgw, boolean1);
    }
    
    @Override
    public boolean recipeMatches(final Recipe<? super Container> bgw) {
        return bgw.matches(this.container, this.level);
    }
    
    @Override
    public int getResultSlotIndex() {
        return 2;
    }
    
    @Override
    public int getGridWidth() {
        return 1;
    }
    
    @Override
    public int getGridHeight() {
        return 1;
    }
    
    @Override
    public int getSize() {
        return 3;
    }
    
    @Override
    public boolean stillValid(final Player ayj) {
        return this.container.stillValid(ayj);
    }
    
    @Override
    public ItemStack quickMoveStack(final Player ayj, final int integer) {
        ItemStack ben4 = ItemStack.EMPTY;
        final Slot bca5 = this.slots.get(integer);
        if (bca5 != null && bca5.hasItem()) {
            final ItemStack ben5 = bca5.getItem();
            ben4 = ben5.copy();
            if (integer == 2) {
                if (!this.moveItemStackTo(ben5, 3, 39, true)) {
                    return ItemStack.EMPTY;
                }
                bca5.onQuickCraft(ben5, ben4);
            }
            else if (integer == 1 || integer == 0) {
                if (!this.moveItemStackTo(ben5, 3, 39, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (this.canSmelt(ben5)) {
                if (!this.moveItemStackTo(ben5, 0, 1, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (this.isFuel(ben5)) {
                if (!this.moveItemStackTo(ben5, 1, 2, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 3 && integer < 30) {
                if (!this.moveItemStackTo(ben5, 30, 39, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 30 && integer < 39 && !this.moveItemStackTo(ben5, 3, 30, false)) {
                return ItemStack.EMPTY;
            }
            if (ben5.isEmpty()) {
                bca5.set(ItemStack.EMPTY);
            }
            else {
                bca5.setChanged();
            }
            if (ben5.getCount() == ben4.getCount()) {
                return ItemStack.EMPTY;
            }
            bca5.onTake(ayj, ben5);
        }
        return ben4;
    }
    
    protected boolean canSmelt(final ItemStack ben) {
        return this.level.getRecipeManager().<SimpleContainer, Recipe>getRecipeFor((RecipeType<Recipe>)this.recipeType, new SimpleContainer(new ItemStack[] { ben }), this.level).isPresent();
    }
    
    protected boolean isFuel(final ItemStack ben) {
        return AbstractFurnaceBlockEntity.isFuel(ben);
    }
    
    public int getBurnProgress() {
        final int integer2 = this.data.get(2);
        final int integer3 = this.data.get(3);
        if (integer3 == 0 || integer2 == 0) {
            return 0;
        }
        return integer2 * 24 / integer3;
    }
    
    public int getLitProgress() {
        int integer2 = this.data.get(1);
        if (integer2 == 0) {
            integer2 = 200;
        }
        return this.data.get(0) * 13 / integer2;
    }
    
    public boolean isLit() {
        return this.data.get(0) > 0;
    }
}
