package net.minecraft.world.inventory;

import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.MapItem;
import net.minecraft.world.item.Item;
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 net.minecraft.world.Container;

public class CartographyTableMenu extends AbstractContainerMenu {
    private final ContainerLevelAccess access;
    private boolean quickMoved;
    private long lastSoundTime;
    public final Container container;
    private final ResultContainer resultContainer;
    
    public CartographyTableMenu(final int integer, final Inventory beb) {
        this(integer, beb, ContainerLevelAccess.NULL);
    }
    
    public CartographyTableMenu(final int integer, final Inventory beb, final ContainerLevelAccess bgs) {
        super(MenuType.CARTOGRAPHY_TABLE, integer);
        this.container = new SimpleContainer(2) {
            @Override
            public void setChanged() {
                CartographyTableMenu.this.slotsChanged(this);
                super.setChanged();
            }
        };
        this.resultContainer = new ResultContainer() {
            @Override
            public void setChanged() {
                CartographyTableMenu.this.slotsChanged(this);
                super.setChanged();
            }
        };
        this.access = bgs;
        this.addSlot(new Slot(this.container, 0, 15, 15) {
            @Override
            public boolean mayPlace(final ItemStack bki) {
                return bki.getItem() == Items.FILLED_MAP;
            }
        });
        this.addSlot(new Slot(this.container, 1, 15, 52) {
            @Override
            public boolean mayPlace(final ItemStack bki) {
                final Item bke3 = bki.getItem();
                return bke3 == Items.PAPER || bke3 == Items.MAP || bke3 == Items.GLASS_PANE;
            }
        });
        this.addSlot(new Slot(this.resultContainer, 2, 145, 39) {
            @Override
            public boolean mayPlace(final ItemStack bki) {
                return false;
            }
            
            @Override
            public ItemStack remove(final int integer) {
                final ItemStack bki3 = super.remove(integer);
                ItemStack bki5;
                final ItemStack itemStack;
                final ItemStack bki4 = bgs.<ItemStack>evaluate((bqb, fu) -> {
                    if (!CartographyTableMenu.this.quickMoved && CartographyTableMenu.this.container.getItem(1).getItem() == Items.GLASS_PANE) {
                        bki5 = MapItem.lockMap(bqb, CartographyTableMenu.this.container.getItem(0));
                        if (bki5 != null) {
                            bki5.setCount(1);
                            return bki5;
                        }
                    }
                    return itemStack;
                }).orElse(bki3);
                CartographyTableMenu.this.container.removeItem(0, 1);
                CartographyTableMenu.this.container.removeItem(1, 1);
                return bki4;
            }
            
            @Override
            protected void onQuickCraft(final ItemStack bki, final int integer) {
                this.remove(integer);
                super.onQuickCraft(bki, integer);
            }
            
            @Override
            public ItemStack onTake(final Player bec, final ItemStack bki) {
                bki.getItem().onCraftedBy(bki, bec.level, bec);
                final long long4;
                bgs.execute((bqb, fu) -> {
                    long4 = bqb.getGameTime();
                    if (CartographyTableMenu.this.lastSoundTime != long4) {
                        bqb.playSound(null, fu, SoundEvents.UI_CARTOGRAPHY_TABLE_TAKE_RESULT, SoundSource.BLOCKS, 1.0f, 1.0f);
                        CartographyTableMenu.this.lastSoundTime = long4;
                    }
                    return;
                });
                return super.onTake(bec, bki);
            }
        });
        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 AbstractContainerMenu.stillValid(this.access, bec, Blocks.CARTOGRAPHY_TABLE);
    }
    
    @Override
    public void slotsChanged(final Container amz) {
        final ItemStack bki3 = this.container.getItem(0);
        final ItemStack bki4 = this.container.getItem(1);
        final ItemStack bki5 = this.resultContainer.getItem(2);
        if (!bki5.isEmpty() && (bki3.isEmpty() || bki4.isEmpty())) {
            this.resultContainer.removeItemNoUpdate(2);
        }
        else if (!bki3.isEmpty() && !bki4.isEmpty()) {
            this.setupResultSlot(bki3, bki4, bki5);
        }
    }
    
    private void setupResultSlot(final ItemStack bki1, final ItemStack bki2, final ItemStack bki3) {
        final Item bke7;
        final MapItemSavedData czv8;
        ItemStack bki4;
        this.access.execute((bqb, fu) -> {
            bke7 = bki2.getItem();
            czv8 = MapItem.getSavedData(bki1, bqb);
            if (czv8 != null) {
                if (bke7 == Items.PAPER && !czv8.locked && czv8.scale < 4) {
                    bki4 = bki1.copy();
                    bki4.setCount(1);
                    bki4.getOrCreateTag().putInt("map_scale_direction", 1);
                    this.broadcastChanges();
                }
                else if (bke7 == Items.GLASS_PANE && !czv8.locked) {
                    bki4 = bki1.copy();
                    bki4.setCount(1);
                    this.broadcastChanges();
                }
                else if (bke7 == Items.MAP) {
                    bki4 = bki1.copy();
                    bki4.setCount(2);
                    this.broadcastChanges();
                }
                else {
                    this.resultContainer.removeItemNoUpdate(2);
                    this.broadcastChanges();
                    return;
                }
                if (!ItemStack.matches(bki4, bki3)) {
                    this.resultContainer.setItem(2, bki4);
                    this.broadcastChanges();
                }
            }
        });
    }
    
    @Override
    public boolean canTakeItemForPickAll(final ItemStack bki, final Slot bhw) {
        return bhw.container != this.resultContainer && super.canTakeItemForPickAll(bki, bhw);
    }
    
    @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()) {
            ItemStack bki6;
            final ItemStack bki5 = bki6 = bhw5.getItem();
            final Item bke8 = bki6.getItem();
            bki4 = bki6.copy();
            if (integer == 2) {
                if (this.container.getItem(1).getItem() == Items.GLASS_PANE) {
                    final ItemStack bki7;
                    final ItemStack itemStack;
                    bki6 = this.access.<ItemStack>evaluate((bqb, fu) -> {
                        bki7 = MapItem.lockMap(bqb, this.container.getItem(0));
                        if (bki7 != null) {
                            bki7.setCount(1);
                            return bki7;
                        }
                        else {
                            return itemStack;
                        }
                    }).orElse(bki6);
                }
                bke8.onCraftedBy(bki6, bec.level, bec);
                if (!this.moveItemStackTo(bki6, 3, 39, true)) {
                    return ItemStack.EMPTY;
                }
                bhw5.onQuickCraft(bki6, bki4);
            }
            else if (integer == 1 || integer == 0) {
                if (!this.moveItemStackTo(bki6, 3, 39, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (bke8 == Items.FILLED_MAP) {
                if (!this.moveItemStackTo(bki6, 0, 1, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (bke8 == Items.PAPER || bke8 == Items.MAP || bke8 == Items.GLASS_PANE) {
                if (!this.moveItemStackTo(bki6, 1, 2, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 3 && integer < 30) {
                if (!this.moveItemStackTo(bki6, 30, 39, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 30 && integer < 39 && !this.moveItemStackTo(bki6, 3, 30, false)) {
                return ItemStack.EMPTY;
            }
            if (bki6.isEmpty()) {
                bhw5.set(ItemStack.EMPTY);
            }
            bhw5.setChanged();
            if (bki6.getCount() == bki4.getCount()) {
                return ItemStack.EMPTY;
            }
            this.quickMoved = true;
            bhw5.onTake(bec, bki6);
            this.quickMoved = false;
            this.broadcastChanges();
        }
        return bki4;
    }
    
    @Override
    public void removed(final Player bec) {
        super.removed(bec);
        this.resultContainer.removeItemNoUpdate(2);
        this.access.execute((bqb, fu) -> this.clearContainer(bec, bec.level, this.container));
    }
}
