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<?> bhk, final RecipeType<? extends AbstractCookingRecipe> bmx, final int integer, final Inventory beb) {
        this(bhk, bmx, integer, beb, new SimpleContainer(3), new SimpleContainerData(4));
    }
    
    protected AbstractFurnaceMenu(final MenuType<?> bhk, final RecipeType<? extends AbstractCookingRecipe> bmx, final int integer, final Inventory beb, final Container amz, final ContainerData bgr) {
        super(bhk, integer);
        this.recipeType = bmx;
        AbstractContainerMenu.checkContainerSize(amz, 3);
        AbstractContainerMenu.checkContainerDataCount(bgr, 4);
        this.container = amz;
        this.data = bgr;
        this.level = beb.player.level;
        this.addSlot(new Slot(amz, 0, 56, 17));
        this.addSlot(new FurnaceFuelSlot(this, amz, 1, 56, 53));
        this.addSlot(new FurnaceResultSlot(beb.player, amz, 2, 116, 35));
        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));
        }
        this.addDataSlots(bgr);
    }
    
    @Override
    public void fillCraftSlotsStackedContents(final StackedContents bee) {
        if (this.container instanceof StackedContentsCompatible) {
            ((StackedContentsCompatible)this.container).fillStackedContents(bee);
        }
    }
    
    @Override
    public void clearCraftingContent() {
        this.container.clearContent();
    }
    
    @Override
    public void handlePlacement(final boolean boolean1, final Recipe<?> bmu, final ServerPlayer ze) {
        new ServerPlaceSmeltingRecipe<>(this).recipeClicked(ze, bmu, boolean1);
    }
    
    @Override
    public boolean recipeMatches(final Recipe<? super Container> bmu) {
        return bmu.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 bec) {
        return this.container.stillValid(bec);
    }
    
    @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 == 1 || integer == 0) {
                if (!this.moveItemStackTo(bki5, 3, 39, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (this.canSmelt(bki5)) {
                if (!this.moveItemStackTo(bki5, 0, 1, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (this.isFuel(bki5)) {
                if (!this.moveItemStackTo(bki5, 1, 2, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 3 && integer < 30) {
                if (!this.moveItemStackTo(bki5, 30, 39, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 30 && integer < 39 && !this.moveItemStackTo(bki5, 3, 30, 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;
    }
    
    protected boolean canSmelt(final ItemStack bki) {
        return this.level.getRecipeManager().<SimpleContainer, Recipe>getRecipeFor((RecipeType<Recipe>)this.recipeType, new SimpleContainer(new ItemStack[] { bki }), this.level).isPresent();
    }
    
    protected boolean isFuel(final ItemStack bki) {
        return AbstractFurnaceBlockEntity.isFuel(bki);
    }
    
    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;
    }
}
