package net.minecraft.world.inventory;

import net.minecraft.world.item.Item;
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.Items;
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 aje) {
        this(integer, aje, new SimpleContainerData(3), ContainerLevelAccess.NULL);
    }
    
    public BeaconMenu(final int integer, final Container aje, final ContainerData baw, final ContainerLevelAccess bax) {
        super(MenuType.BEACON, integer);
        this.beacon = new SimpleContainer(1) {
            @Override
            public boolean canPlaceItem(final int integer, final ItemStack ben) {
                return ben.getItem() == Items.EMERALD || ben.getItem() == Items.DIAMOND || ben.getItem() == Items.GOLD_INGOT || ben.getItem() == Items.IRON_INGOT;
            }
            
            @Override
            public int getMaxStackSize() {
                return 1;
            }
        };
        AbstractContainerMenu.checkContainerDataCount(baw, 3);
        this.beaconData = baw;
        this.access = bax;
        this.addSlot(this.paymentSlot = new PaymentSlot(this.beacon, 0, 136, 110));
        this.addDataSlots(baw);
        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(aje, integer5 + integer4 * 9 + 9, 36 + integer5 * 18, 137 + integer4 * 18));
            }
        }
        for (int integer4 = 0; integer4 < 9; ++integer4) {
            this.addSlot(new Slot(aje, integer4, 36 + integer4 * 18, 195));
        }
    }
    
    @Override
    public void removed(final Player ayj) {
        super.removed(ayj);
        if (ayj.level.isClientSide) {
            return;
        }
        final ItemStack ben3 = this.paymentSlot.remove(this.paymentSlot.getMaxStackSize());
        if (!ben3.isEmpty()) {
            ayj.drop(ben3, false);
        }
    }
    
    @Override
    public boolean stillValid(final Player ayj) {
        return AbstractContainerMenu.stillValid(this.access, ayj, Blocks.BEACON);
    }
    
    @Override
    public void setData(final int integer1, final int integer2) {
        super.setData(integer1, integer2);
        this.broadcastChanges();
    }
    
    @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) {
                if (!this.moveItemStackTo(ben5, 1, 37, true)) {
                    return ItemStack.EMPTY;
                }
                bca5.onQuickCraft(ben5, ben4);
            }
            else if (!this.paymentSlot.hasItem() && this.paymentSlot.mayPlace(ben5) && ben5.getCount() == 1) {
                if (!this.moveItemStackTo(ben5, 0, 1, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 1 && integer < 28) {
                if (!this.moveItemStackTo(ben5, 28, 37, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 28 && integer < 37) {
                if (!this.moveItemStackTo(ben5, 1, 28, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (!this.moveItemStackTo(ben5, 1, 37, 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 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 aje, final int integer3, final int integer4, final int integer5) {
            super(aje, integer3, integer4, integer5);
        }
        
        @Override
        public boolean mayPlace(final ItemStack ben) {
            final Item bei3 = ben.getItem();
            return bei3 == Items.EMERALD || bei3 == Items.DIAMOND || bei3 == Items.GOLD_INGOT || bei3 == Items.IRON_INGOT;
        }
        
        @Override
        public int getMaxStackSize() {
            return 1;
        }
    }
}
