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