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 ayi) {
        this(integer, ayi, ContainerLevelAccess.NULL);
    }
    
    public CartographyTableMenu(final int integer, final Inventory ayi, final ContainerLevelAccess bax) {
        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 = bax;
        this.addSlot(new Slot(this.container, 0, 15, 15) {
            @Override
            public boolean mayPlace(final ItemStack ben) {
                return ben.getItem() == Items.FILLED_MAP;
            }
        });
        this.addSlot(new Slot(this.container, 1, 15, 52) {
            @Override
            public boolean mayPlace(final ItemStack ben) {
                final Item bei3 = ben.getItem();
                return bei3 == Items.PAPER || bei3 == Items.MAP || bei3 == Items.GLASS_PANE;
            }
        });
        this.addSlot(new Slot(this.resultContainer, 2, 145, 39) {
            @Override
            public boolean mayPlace(final ItemStack ben) {
                return false;
            }
            
            @Override
            public ItemStack remove(final int integer) {
                final ItemStack ben3 = super.remove(integer);
                ItemStack ben5;
                final ItemStack itemStack;
                final ItemStack ben4 = bax.<ItemStack>evaluate((bjw, fk) -> {
                    if (!CartographyTableMenu.this.quickMoved && CartographyTableMenu.this.container.getItem(1).getItem() == Items.GLASS_PANE) {
                        ben5 = MapItem.lockMap(bjw, CartographyTableMenu.this.container.getItem(0));
                        if (ben5 != null) {
                            ben5.setCount(1);
                            return ben5;
                        }
                    }
                    return itemStack;
                }).orElse(ben3);
                CartographyTableMenu.this.container.removeItem(0, 1);
                CartographyTableMenu.this.container.removeItem(1, 1);
                return ben4;
            }
            
            @Override
            protected void onQuickCraft(final ItemStack ben, final int integer) {
                this.remove(integer);
                super.onQuickCraft(ben, integer);
            }
            
            @Override
            public ItemStack onTake(final Player ayj, final ItemStack ben) {
                ben.getItem().onCraftedBy(ben, ayj.level, ayj);
                final long long4;
                bax.execute((bjw, fk) -> {
                    long4 = bjw.getGameTime();
                    if (CartographyTableMenu.this.lastSoundTime != long4) {
                        bjw.playSound(null, fk, SoundEvents.UI_CARTOGRAPHY_TABLE_TAKE_RESULT, SoundSource.BLOCKS, 1.0f, 1.0f);
                        CartographyTableMenu.this.lastSoundTime = long4;
                    }
                    return;
                });
                return super.onTake(ayj, ben);
            }
        });
        for (int integer2 = 0; integer2 < 3; ++integer2) {
            for (int integer3 = 0; integer3 < 9; ++integer3) {
                this.addSlot(new Slot(ayi, integer3 + integer2 * 9 + 9, 8 + integer3 * 18, 84 + integer2 * 18));
            }
        }
        for (int integer2 = 0; integer2 < 9; ++integer2) {
            this.addSlot(new Slot(ayi, integer2, 8 + integer2 * 18, 142));
        }
    }
    
    @Override
    public boolean stillValid(final Player ayj) {
        return AbstractContainerMenu.stillValid(this.access, ayj, Blocks.CARTOGRAPHY_TABLE);
    }
    
    @Override
    public void slotsChanged(final Container aje) {
        final ItemStack ben3 = this.container.getItem(0);
        final ItemStack ben4 = this.container.getItem(1);
        final ItemStack ben5 = this.resultContainer.getItem(2);
        if (!ben5.isEmpty() && (ben3.isEmpty() || ben4.isEmpty())) {
            this.resultContainer.removeItemNoUpdate(2);
        }
        else if (!ben3.isEmpty() && !ben4.isEmpty()) {
            this.setupResultSlot(ben3, ben4, ben5);
        }
    }
    
    private void setupResultSlot(final ItemStack ben1, final ItemStack ben2, final ItemStack ben3) {
        final Item bei7;
        final MapItemSavedData crg8;
        ItemStack ben4;
        this.access.execute((bjw, fk) -> {
            bei7 = ben2.getItem();
            crg8 = MapItem.getSavedData(ben1, bjw);
            if (crg8 != null) {
                if (bei7 == Items.PAPER && !crg8.locked && crg8.scale < 4) {
                    ben4 = ben1.copy();
                    ben4.setCount(1);
                    ben4.getOrCreateTag().putInt("map_scale_direction", 1);
                    this.broadcastChanges();
                }
                else if (bei7 == Items.GLASS_PANE && !crg8.locked) {
                    ben4 = ben1.copy();
                    ben4.setCount(1);
                    this.broadcastChanges();
                }
                else if (bei7 == Items.MAP) {
                    ben4 = ben1.copy();
                    ben4.setCount(2);
                    this.broadcastChanges();
                }
                else {
                    this.resultContainer.removeItemNoUpdate(2);
                    this.broadcastChanges();
                    return;
                }
                if (!ItemStack.matches(ben4, ben3)) {
                    this.resultContainer.setItem(2, ben4);
                    this.broadcastChanges();
                }
            }
        });
    }
    
    @Override
    public boolean canTakeItemForPickAll(final ItemStack ben, final Slot bca) {
        return bca.container != this.resultContainer && super.canTakeItemForPickAll(ben, bca);
    }
    
    @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()) {
            ItemStack ben6;
            final ItemStack ben5 = ben6 = bca5.getItem();
            final Item bei8 = ben6.getItem();
            ben4 = ben6.copy();
            if (integer == 2) {
                if (this.container.getItem(1).getItem() == Items.GLASS_PANE) {
                    final ItemStack ben7;
                    final ItemStack itemStack;
                    ben6 = this.access.<ItemStack>evaluate((bjw, fk) -> {
                        ben7 = MapItem.lockMap(bjw, this.container.getItem(0));
                        if (ben7 != null) {
                            ben7.setCount(1);
                            return ben7;
                        }
                        else {
                            return itemStack;
                        }
                    }).orElse(ben6);
                }
                bei8.onCraftedBy(ben6, ayj.level, ayj);
                if (!this.moveItemStackTo(ben6, 3, 39, true)) {
                    return ItemStack.EMPTY;
                }
                bca5.onQuickCraft(ben6, ben4);
            }
            else if (integer == 1 || integer == 0) {
                if (!this.moveItemStackTo(ben6, 3, 39, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (bei8 == Items.FILLED_MAP) {
                if (!this.moveItemStackTo(ben6, 0, 1, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (bei8 == Items.PAPER || bei8 == Items.MAP || bei8 == Items.GLASS_PANE) {
                if (!this.moveItemStackTo(ben6, 1, 2, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 3 && integer < 30) {
                if (!this.moveItemStackTo(ben6, 30, 39, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 30 && integer < 39 && !this.moveItemStackTo(ben6, 3, 30, false)) {
                return ItemStack.EMPTY;
            }
            if (ben6.isEmpty()) {
                bca5.set(ItemStack.EMPTY);
            }
            bca5.setChanged();
            if (ben6.getCount() == ben4.getCount()) {
                return ItemStack.EMPTY;
            }
            this.quickMoved = true;
            bca5.onTake(ayj, ben6);
            this.quickMoved = false;
            this.broadcastChanges();
        }
        return ben4;
    }
    
    @Override
    public void removed(final Player ayj) {
        super.removed(ayj);
        this.resultContainer.removeItemNoUpdate(2);
        this.access.execute((bjw, fk) -> this.clearContainer(ayj, ayj.level, this.container));
    }
}
