package net.minecraft.world.inventory;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.stats.Stats;
import net.minecraft.world.item.EnchantedBookItem;
import net.minecraft.world.level.ItemLike;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.player.Player;
import java.util.List;
import net.minecraft.core.Registry;
import net.minecraft.world.item.enchantment.EnchantmentInstance;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import java.util.Random;
import net.minecraft.world.Container;

public class EnchantmentMenu extends AbstractContainerMenu {
    private final Container enchantSlots;
    private final ContainerLevelAccess access;
    private final Random random;
    private final DataSlot enchantmentSeed;
    public final int[] costs;
    public final int[] enchantClue;
    public final int[] levelClue;
    
    public EnchantmentMenu(final int integer, final Inventory beb) {
        this(integer, beb, ContainerLevelAccess.NULL);
    }
    
    public EnchantmentMenu(final int integer, final Inventory beb, final ContainerLevelAccess bgs) {
        super(MenuType.ENCHANTMENT, integer);
        this.enchantSlots = new SimpleContainer(2) {
            @Override
            public void setChanged() {
                super.setChanged();
                EnchantmentMenu.this.slotsChanged(this);
            }
        };
        this.random = new Random();
        this.enchantmentSeed = DataSlot.standalone();
        this.costs = new int[3];
        this.enchantClue = new int[] { -1, -1, -1 };
        this.levelClue = new int[] { -1, -1, -1 };
        this.access = bgs;
        this.addSlot(new Slot(this.enchantSlots, 0, 15, 47) {
            @Override
            public boolean mayPlace(final ItemStack bki) {
                return true;
            }
            
            @Override
            public int getMaxStackSize() {
                return 1;
            }
        });
        this.addSlot(new Slot(this.enchantSlots, 1, 35, 47) {
            @Override
            public boolean mayPlace(final ItemStack bki) {
                return bki.getItem() == Items.LAPIS_LAZULI;
            }
        });
        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.addDataSlot(DataSlot.shared(this.costs, 0));
        this.addDataSlot(DataSlot.shared(this.costs, 1));
        this.addDataSlot(DataSlot.shared(this.costs, 2));
        this.addDataSlot(this.enchantmentSeed).set(beb.player.getEnchantmentSeed());
        this.addDataSlot(DataSlot.shared(this.enchantClue, 0));
        this.addDataSlot(DataSlot.shared(this.enchantClue, 1));
        this.addDataSlot(DataSlot.shared(this.enchantClue, 2));
        this.addDataSlot(DataSlot.shared(this.levelClue, 0));
        this.addDataSlot(DataSlot.shared(this.levelClue, 1));
        this.addDataSlot(DataSlot.shared(this.levelClue, 2));
    }
    
    @Override
    public void slotsChanged(final Container amz) {
        if (amz == this.enchantSlots) {
            final ItemStack bki3 = amz.getItem(0);
            if (bki3.isEmpty() || !bki3.isEnchantable()) {
                for (int integer4 = 0; integer4 < 3; ++integer4) {
                    this.costs[integer4] = 0;
                    this.enchantClue[integer4] = -1;
                    this.levelClue[integer4] = -1;
                }
            }
            else {
                int integer5;
                int integer6;
                int integer7;
                int integer8;
                final ItemStack itemStack;
                int integer9;
                List<EnchantmentInstance> list7;
                EnchantmentInstance bnz8;
                this.access.execute((bqb, fu) -> {
                    integer5 = 0;
                    for (integer6 = -1; integer6 <= 1; ++integer6) {
                        for (integer7 = -1; integer7 <= 1; ++integer7) {
                            if (integer6 != 0 || integer7 != 0) {
                                if (bqb.isEmptyBlock(fu.offset(integer7, 0, integer6)) && bqb.isEmptyBlock(fu.offset(integer7, 1, integer6))) {
                                    if (bqb.getBlockState(fu.offset(integer7 * 2, 0, integer6 * 2)).is(Blocks.BOOKSHELF)) {
                                        ++integer5;
                                    }
                                    if (bqb.getBlockState(fu.offset(integer7 * 2, 1, integer6 * 2)).is(Blocks.BOOKSHELF)) {
                                        ++integer5;
                                    }
                                    if (integer7 != 0 && integer6 != 0) {
                                        if (bqb.getBlockState(fu.offset(integer7 * 2, 0, integer6)).is(Blocks.BOOKSHELF)) {
                                            ++integer5;
                                        }
                                        if (bqb.getBlockState(fu.offset(integer7 * 2, 1, integer6)).is(Blocks.BOOKSHELF)) {
                                            ++integer5;
                                        }
                                        if (bqb.getBlockState(fu.offset(integer7, 0, integer6 * 2)).is(Blocks.BOOKSHELF)) {
                                            ++integer5;
                                        }
                                        if (bqb.getBlockState(fu.offset(integer7, 1, integer6 * 2)).is(Blocks.BOOKSHELF)) {
                                            ++integer5;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    this.random.setSeed(this.enchantmentSeed.get());
                    for (integer8 = 0; integer8 < 3; ++integer8) {
                        this.costs[integer8] = EnchantmentHelper.getEnchantmentCost(this.random, integer8, integer5, itemStack);
                        this.enchantClue[integer8] = -1;
                        this.levelClue[integer8] = -1;
                        if (this.costs[integer8] < integer8 + 1) {
                            this.costs[integer8] = 0;
                        }
                    }
                    for (integer9 = 0; integer9 < 3; ++integer9) {
                        if (this.costs[integer9] > 0) {
                            list7 = this.getEnchantmentList(itemStack, integer9, this.costs[integer9]);
                            if (list7 != null && !list7.isEmpty()) {
                                bnz8 = list7.get(this.random.nextInt(list7.size()));
                                this.enchantClue[integer9] = Registry.ENCHANTMENT.getId(bnz8.enchantment);
                                this.levelClue[integer9] = bnz8.level;
                            }
                        }
                    }
                    this.broadcastChanges();
                });
            }
        }
    }
    
    @Override
    public boolean clickMenuButton(final Player bec, final int integer) {
        // 
        // This method could not be decompiled.
        // 
        // Original Bytecode:
        // 
        //     1: getfield        net/minecraft/world/inventory/EnchantmentMenu.enchantSlots:Lnet/minecraft/world/Container;
        //     4: iconst_0       
        //     5: invokeinterface net/minecraft/world/Container.getItem:(I)Lnet/minecraft/world/item/ItemStack;
        //    10: astore_3        /* bki4 */
        //    11: aload_0         /* this */
        //    12: getfield        net/minecraft/world/inventory/EnchantmentMenu.enchantSlots:Lnet/minecraft/world/Container;
        //    15: iconst_1       
        //    16: invokeinterface net/minecraft/world/Container.getItem:(I)Lnet/minecraft/world/item/ItemStack;
        //    21: astore          bki5
        //    23: iload_2         /* integer */
        //    24: iconst_1       
        //    25: iadd           
        //    26: istore          integer6
        //    28: aload           bki5
        //    30: invokevirtual   net/minecraft/world/item/ItemStack.isEmpty:()Z
        //    33: ifne            46
        //    36: aload           bki5
        //    38: invokevirtual   net/minecraft/world/item/ItemStack.getCount:()I
        //    41: iload           integer6
        //    43: if_icmpge       58
        //    46: aload_1         /* bec */
        //    47: getfield        net/minecraft/world/entity/player/Player.abilities:Lnet/minecraft/world/entity/player/Abilities;
        //    50: getfield        net/minecraft/world/entity/player/Abilities.instabuild:Z
        //    53: ifne            58
        //    56: iconst_0       
        //    57: ireturn        
        //    58: aload_0         /* this */
        //    59: getfield        net/minecraft/world/inventory/EnchantmentMenu.costs:[I
        //    62: iload_2         /* integer */
        //    63: iaload         
        //    64: ifle            130
        //    67: aload_3         /* bki4 */
        //    68: invokevirtual   net/minecraft/world/item/ItemStack.isEmpty:()Z
        //    71: ifne            130
        //    74: aload_1         /* bec */
        //    75: getfield        net/minecraft/world/entity/player/Player.experienceLevel:I
        //    78: iload           integer6
        //    80: if_icmplt       96
        //    83: aload_1         /* bec */
        //    84: getfield        net/minecraft/world/entity/player/Player.experienceLevel:I
        //    87: aload_0         /* this */
        //    88: getfield        net/minecraft/world/inventory/EnchantmentMenu.costs:[I
        //    91: iload_2         /* integer */
        //    92: iaload         
        //    93: if_icmpge       106
        //    96: aload_1         /* bec */
        //    97: getfield        net/minecraft/world/entity/player/Player.abilities:Lnet/minecraft/world/entity/player/Abilities;
        //   100: getfield        net/minecraft/world/entity/player/Abilities.instabuild:Z
        //   103: ifeq            130
        //   106: aload_0         /* this */
        //   107: getfield        net/minecraft/world/inventory/EnchantmentMenu.access:Lnet/minecraft/world/inventory/ContainerLevelAccess;
        //   110: aload_0         /* this */
        //   111: aload_3         /* bki4 */
        //   112: iload_2         /* integer */
        //   113: aload_1         /* bec */
        //   114: iload           integer6
        //   116: aload           bki5
        //   118: invokedynamic   BootstrapMethod #1, accept:(Lnet/minecraft/world/inventory/EnchantmentMenu;Lnet/minecraft/world/item/ItemStack;ILnet/minecraft/world/entity/player/Player;ILnet/minecraft/world/item/ItemStack;)Ljava/util/function/BiConsumer;
        //   123: invokeinterface net/minecraft/world/inventory/ContainerLevelAccess.execute:(Ljava/util/function/BiConsumer;)V
        //   128: iconst_1       
        //   129: ireturn        
        //   130: iconst_0       
        //   131: ireturn        
        //    MethodParameters:
        //  Name     Flags  
        //  -------  -----
        //  bec      
        //  integer  
        //    StackMapTable: 00 05 FE 00 2E 07 00 84 07 00 84 01 0B 25 09 FF 00 17 00 00 00 00
        // 
        // The error that occurred was:
        // 
        // java.lang.IllegalStateException: Could not infer any expression.
        //     at com.strobel.decompiler.ast.TypeAnalysis.runInference(TypeAnalysis.java:374)
        //     at com.strobel.decompiler.ast.TypeAnalysis.run(TypeAnalysis.java:96)
        //     at com.strobel.decompiler.ast.AstOptimizer.optimize(AstOptimizer.java:344)
        //     at com.strobel.decompiler.ast.AstOptimizer.optimize(AstOptimizer.java:42)
        //     at com.strobel.decompiler.languages.java.ast.AstMethodBodyBuilder.createMethodBody(AstMethodBodyBuilder.java:214)
        //     at com.strobel.decompiler.languages.java.ast.AstMethodBodyBuilder.createMethodBody(AstMethodBodyBuilder.java:99)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createMethodBody(AstBuilder.java:782)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createMethod(AstBuilder.java:675)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.addTypeMembers(AstBuilder.java:552)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createTypeCore(AstBuilder.java:519)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createTypeNoCache(AstBuilder.java:161)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createType(AstBuilder.java:150)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.addType(AstBuilder.java:125)
        //     at cuchaz.enigma.SourceProvider.getSources(SourceProvider.java:85)
        //     at cuchaz.enigma.EnigmaProject$JarExport.decompileClass(EnigmaProject.java:241)
        //     at cuchaz.enigma.EnigmaProject$JarExport.lambda$decompile$1(EnigmaProject.java:230)
        //     at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
        //     at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1654)
        //     at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
        //     at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
        //     at java.base/java.util.stream.ReduceOps$ReduceTask.doLeaf(ReduceOps.java:952)
        //     at java.base/java.util.stream.ReduceOps$ReduceTask.doLeaf(ReduceOps.java:926)
        //     at java.base/java.util.stream.AbstractTask.compute(AbstractTask.java:327)
        //     at java.base/java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:746)
        //     at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
        //     at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
        //     at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
        //     at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
        //     at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:177)
        // 
        throw new IllegalStateException("An error occurred while decompiling this method.");
    }
    
    private List<EnchantmentInstance> getEnchantmentList(final ItemStack bki, final int integer2, final int integer3) {
        this.random.setSeed(this.enchantmentSeed.get() + integer2);
        final List<EnchantmentInstance> list5 = EnchantmentHelper.selectEnchantment(this.random, bki, integer3, false);
        if (bki.getItem() == Items.BOOK && list5.size() > 1) {
            list5.remove(this.random.nextInt(list5.size()));
        }
        return list5;
    }
    
    public int getGoldCount() {
        final ItemStack bki2 = this.enchantSlots.getItem(1);
        if (bki2.isEmpty()) {
            return 0;
        }
        return bki2.getCount();
    }
    
    public int getEnchantmentSeed() {
        return this.enchantmentSeed.get();
    }
    
    @Override
    public void removed(final Player bec) {
        super.removed(bec);
        this.access.execute((bqb, fu) -> this.clearContainer(bec, bec.level, this.enchantSlots));
    }
    
    @Override
    public boolean stillValid(final Player bec) {
        return AbstractContainerMenu.stillValid(this.access, bec, Blocks.ENCHANTING_TABLE);
    }
    
    @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, 2, 38, true)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer == 1) {
                if (!this.moveItemStackTo(bki5, 2, 38, true)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (bki5.getItem() == Items.LAPIS_LAZULI) {
                if (!this.moveItemStackTo(bki5, 1, 2, true)) {
                    return ItemStack.EMPTY;
                }
            }
            else {
                if (this.slots.get(0).hasItem() || !this.slots.get(0).mayPlace(bki5)) {
                    return ItemStack.EMPTY;
                }
                final ItemStack bki6 = bki5.copy();
                bki6.setCount(1);
                bki5.shrink(1);
                this.slots.get(0).set(bki6);
            }
            if (bki5.isEmpty()) {
                bhw5.set(ItemStack.EMPTY);
            }
            else {
                bhw5.setChanged();
            }
            if (bki5.getCount() == bki4.getCount()) {
                return ItemStack.EMPTY;
            }
            bhw5.onTake(bec, bki5);
        }
        return bki4;
    }
}
