package net.minecraft.world.inventory;

import javax.annotation.Nullable;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.tags.Tag;
import net.minecraft.tags.ItemTags;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.Container;

public class BeaconMenu extends AbstractContainerMenu {
    private final Container beacon;
    private final PaymentSlot paymentSlot;
    private final ContainerLevelAccess access;
    private final ContainerData beaconData;
    
    public BeaconMenu(final int integer, final Container amz) {
        this(integer, amz, new SimpleContainerData(3), ContainerLevelAccess.NULL);
    }
    
    public BeaconMenu(final int integer, final Container amz, final ContainerData bgr, final ContainerLevelAccess bgs) {
        super(MenuType.BEACON, integer);
        this.beacon = new SimpleContainer(1) {
            @Override
            public boolean canPlaceItem(final int integer, final ItemStack bki) {
                return bki.getItem().is(ItemTags.BEACON_PAYMENT_ITEMS);
            }
            
            @Override
            public int getMaxStackSize() {
                return 1;
            }
        };
        AbstractContainerMenu.checkContainerDataCount(bgr, 3);
        this.beaconData = bgr;
        this.access = bgs;
        this.addSlot(this.paymentSlot = new PaymentSlot(this.beacon, 0, 136, 110));
        this.addDataSlots(bgr);
        final int integer2 = 36;
        final int integer3 = 137;
        for (int integer4 = 0; integer4 < 3; ++integer4) {
            for (int integer5 = 0; integer5 < 9; ++integer5) {
                this.addSlot(new Slot(amz, integer5 + integer4 * 9 + 9, 36 + integer5 * 18, 137 + integer4 * 18));
            }
        }
        for (int integer4 = 0; integer4 < 9; ++integer4) {
            this.addSlot(new Slot(amz, integer4, 36 + integer4 * 18, 195));
        }
    }
    
    @Override
    public void removed(final Player bec) {
        super.removed(bec);
        if (bec.level.isClientSide) {
            return;
        }
        final ItemStack bki3 = this.paymentSlot.remove(this.paymentSlot.getMaxStackSize());
        if (!bki3.isEmpty()) {
            bec.drop(bki3, false);
        }
    }
    
    @Override
    public boolean stillValid(final Player bec) {
        return AbstractContainerMenu.stillValid(this.access, bec, Blocks.BEACON);
    }
    
    @Override
    public void setData(final int integer1, final int integer2) {
        super.setData(integer1, integer2);
        this.broadcastChanges();
    }
    
    @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) {
                if (!this.moveItemStackTo(bki5, 1, 37, true)) {
                    return ItemStack.EMPTY;
                }
                bhw5.onQuickCraft(bki5, bki4);
            }
            else if (!this.paymentSlot.hasItem() && this.paymentSlot.mayPlace(bki5) && bki5.getCount() == 1) {
                if (!this.moveItemStackTo(bki5, 0, 1, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 1 && integer < 28) {
                if (!this.moveItemStackTo(bki5, 28, 37, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 28 && integer < 37) {
                if (!this.moveItemStackTo(bki5, 1, 28, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (!this.moveItemStackTo(bki5, 1, 37, 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 getLevels() {
        return this.beaconData.get(0);
    }
    
    @Nullable
    public MobEffect getPrimaryEffect() {
        return MobEffect.byId(this.beaconData.get(1));
    }
    
    @Nullable
    public MobEffect getSecondaryEffect() {
        return MobEffect.byId(this.beaconData.get(2));
    }
    
    public void updateEffects(final int integer1, final int integer2) {
        if (this.paymentSlot.hasItem()) {
            this.beaconData.set(1, integer1);
            this.beaconData.set(2, integer2);
            this.paymentSlot.remove(1);
        }
    }
    
    public boolean hasPayment() {
        return !this.beacon.getItem(0).isEmpty();
    }
    
    class PaymentSlot extends Slot {
        public PaymentSlot(final Container amz, final int integer3, final int integer4, final int integer5) {
            super(amz, integer3, integer4, integer5);
        }
        
        @Override
        public boolean mayPlace(final ItemStack bki) {
            return bki.getItem().is(ItemTags.BEACON_PAYMENT_ITEMS);
        }
        
        @Override
        public int getMaxStackSize() {
            return 1;
        }
    }
}
