package net.minecraft.world.inventory;

import net.minecraft.world.item.alchemy.PotionBrewing;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.alchemy.Potion;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.alchemy.PotionUtils;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.Container;

public class BrewingStandMenu extends AbstractContainerMenu {
    private final Container brewingStand;
    private final ContainerData brewingStandData;
    private final Slot ingredientSlot;
    
    public BrewingStandMenu(final int integer, final Inventory ayi) {
        this(integer, ayi, new SimpleContainer(5), new SimpleContainerData(2));
    }
    
    public BrewingStandMenu(final int integer, final Inventory ayi, final Container aje, final ContainerData baw) {
        super(MenuType.BREWING_STAND, integer);
        AbstractContainerMenu.checkContainerSize(aje, 5);
        AbstractContainerMenu.checkContainerDataCount(baw, 2);
        this.brewingStand = aje;
        this.brewingStandData = baw;
        this.addSlot(new PotionSlot(aje, 0, 56, 51));
        this.addSlot(new PotionSlot(aje, 1, 79, 58));
        this.addSlot(new PotionSlot(aje, 2, 102, 51));
        this.ingredientSlot = this.addSlot(new IngredientsSlot(aje, 3, 79, 17));
        this.addSlot(new FuelSlot(aje, 4, 17, 17));
        this.addDataSlots(baw);
        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));
        }
    }
    
    @Override
    public boolean stillValid(final Player ayj) {
        return this.brewingStand.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 >= 0 && integer <= 2) || integer == 3 || integer == 4) {
                if (!this.moveItemStackTo(ben5, 5, 41, true)) {
                    return ItemStack.EMPTY;
                }
                bca5.onQuickCraft(ben5, ben4);
            }
            else if (FuelSlot.mayPlaceItem(ben4)) {
                if (this.moveItemStackTo(ben5, 4, 5, false) || (this.ingredientSlot.mayPlace(ben5) && !this.moveItemStackTo(ben5, 3, 4, false))) {
                    return ItemStack.EMPTY;
                }
            }
            else if (this.ingredientSlot.mayPlace(ben5)) {
                if (!this.moveItemStackTo(ben5, 3, 4, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (PotionSlot.mayPlaceItem(ben4) && ben4.getCount() == 1) {
                if (!this.moveItemStackTo(ben5, 0, 3, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 5 && integer < 32) {
                if (!this.moveItemStackTo(ben5, 32, 41, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 32 && integer < 41) {
                if (!this.moveItemStackTo(ben5, 5, 32, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (!this.moveItemStackTo(ben5, 5, 41, 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;
    }
    
    public int getFuel() {
        return this.brewingStandData.get(1);
    }
    
    public int getBrewingTicks() {
        return this.brewingStandData.get(0);
    }
    
    static class PotionSlot extends Slot {
        public PotionSlot(final Container aje, final int integer2, final int integer3, final int integer4) {
            super(aje, integer2, integer3, integer4);
        }
        
        @Override
        public boolean mayPlace(final ItemStack ben) {
            return mayPlaceItem(ben);
        }
        
        @Override
        public int getMaxStackSize() {
            return 1;
        }
        
        @Override
        public ItemStack onTake(final Player ayj, final ItemStack ben) {
            final Potion bgd4 = PotionUtils.getPotion(ben);
            if (ayj instanceof ServerPlayer) {
                CriteriaTriggers.BREWED_POTION.trigger((ServerPlayer)ayj, bgd4);
            }
            super.onTake(ayj, ben);
            return ben;
        }
        
        public static boolean mayPlaceItem(final ItemStack ben) {
            final Item bei2 = ben.getItem();
            return bei2 == Items.POTION || bei2 == Items.SPLASH_POTION || bei2 == Items.LINGERING_POTION || bei2 == Items.GLASS_BOTTLE;
        }
    }
    
    static class IngredientsSlot extends Slot {
        public IngredientsSlot(final Container aje, final int integer2, final int integer3, final int integer4) {
            super(aje, integer2, integer3, integer4);
        }
        
        @Override
        public boolean mayPlace(final ItemStack ben) {
            return PotionBrewing.isIngredient(ben);
        }
        
        @Override
        public int getMaxStackSize() {
            return 64;
        }
    }
    
    static class FuelSlot extends Slot {
        public FuelSlot(final Container aje, final int integer2, final int integer3, final int integer4) {
            super(aje, integer2, integer3, integer4);
        }
        
        @Override
        public boolean mayPlace(final ItemStack ben) {
            return mayPlaceItem(ben);
        }
        
        public static boolean mayPlaceItem(final ItemStack ben) {
            return ben.getItem() == Items.BLAZE_POWDER;
        }
        
        @Override
        public int getMaxStackSize() {
            return 64;
        }
    }
}
