package net.minecraft.world.inventory;

import java.util.AbstractList;
import net.minecraft.world.item.DyeColor;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.block.entity.BannerPattern;
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.BannerPatternItem;
import net.minecraft.world.item.DyeItem;
import net.minecraft.world.item.BannerItem;
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 LoomMenu extends AbstractContainerMenu {
    private final ContainerLevelAccess access;
    private final DataSlot selectedBannerPatternIndex;
    private Runnable slotUpdateListener;
    private final Slot bannerSlot;
    private final Slot dyeSlot;
    private final Slot patternSlot;
    private final Slot resultSlot;
    private long lastSoundTime;
    private final Container inputContainer;
    private final Container outputContainer;
    
    public LoomMenu(final int integer, final Inventory ayi) {
        this(integer, ayi, ContainerLevelAccess.NULL);
    }
    
    public LoomMenu(final int integer, final Inventory ayi, final ContainerLevelAccess bax) {
        super(MenuType.LOOM, integer);
        this.selectedBannerPatternIndex = DataSlot.standalone();
        this.slotUpdateListener = (() -> {});
        this.inputContainer = new SimpleContainer(3) {
            @Override
            public void setChanged() {
                super.setChanged();
                LoomMenu.this.slotsChanged(this);
                LoomMenu.this.slotUpdateListener.run();
            }
        };
        this.outputContainer = new SimpleContainer(1) {
            @Override
            public void setChanged() {
                super.setChanged();
                LoomMenu.this.slotUpdateListener.run();
            }
        };
        this.access = bax;
        this.bannerSlot = this.addSlot(new Slot(this.inputContainer, 0, 13, 26) {
            @Override
            public boolean mayPlace(final ItemStack ben) {
                return ben.getItem() instanceof BannerItem;
            }
        });
        this.dyeSlot = this.addSlot(new Slot(this.inputContainer, 1, 33, 26) {
            @Override
            public boolean mayPlace(final ItemStack ben) {
                return ben.getItem() instanceof DyeItem;
            }
        });
        this.patternSlot = this.addSlot(new Slot(this.inputContainer, 2, 23, 45) {
            @Override
            public boolean mayPlace(final ItemStack ben) {
                return ben.getItem() instanceof BannerPatternItem;
            }
        });
        this.resultSlot = this.addSlot(new Slot(this.outputContainer, 0, 143, 58) {
            @Override
            public boolean mayPlace(final ItemStack ben) {
                return false;
            }
            
            @Override
            public ItemStack onTake(final Player ayj, final ItemStack ben) {
                LoomMenu.this.bannerSlot.remove(1);
                LoomMenu.this.dyeSlot.remove(1);
                if (!LoomMenu.this.bannerSlot.hasItem() || !LoomMenu.this.dyeSlot.hasItem()) {
                    LoomMenu.this.selectedBannerPatternIndex.set(0);
                }
                final long long4;
                bax.execute((bjw, fk) -> {
                    long4 = bjw.getGameTime();
                    if (LoomMenu.this.lastSoundTime != long4) {
                        bjw.playSound(null, fk, SoundEvents.UI_LOOM_TAKE_RESULT, SoundSource.BLOCKS, 1.0f, 1.0f);
                        LoomMenu.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));
        }
        this.addDataSlot(this.selectedBannerPatternIndex);
    }
    
    public int getSelectedBannerPatternIndex() {
        return this.selectedBannerPatternIndex.get();
    }
    
    @Override
    public boolean stillValid(final Player ayj) {
        return AbstractContainerMenu.stillValid(this.access, ayj, Blocks.LOOM);
    }
    
    @Override
    public boolean clickMenuButton(final Player ayj, final int integer) {
        if (integer > 0 && integer <= BannerPattern.AVAILABLE_PATTERNS) {
            this.selectedBannerPatternIndex.set(integer);
            this.setupResultSlot();
            return true;
        }
        return false;
    }
    
    @Override
    public void slotsChanged(final Container aje) {
        final ItemStack ben3 = this.bannerSlot.getItem();
        final ItemStack ben4 = this.dyeSlot.getItem();
        final ItemStack ben5 = this.patternSlot.getItem();
        final ItemStack ben6 = this.resultSlot.getItem();
        if (!ben6.isEmpty() && (ben3.isEmpty() || ben4.isEmpty() || this.selectedBannerPatternIndex.get() <= 0 || (this.selectedBannerPatternIndex.get() >= BannerPattern.COUNT - 5 && ben5.isEmpty()))) {
            this.resultSlot.set(ItemStack.EMPTY);
            this.selectedBannerPatternIndex.set(0);
        }
        else if (!ben5.isEmpty() && ben5.getItem() instanceof BannerPatternItem) {
            final CompoundTag jt7 = ben3.getOrCreateTagElement("BlockEntityTag");
            final boolean boolean8 = jt7.contains("Patterns", 9) && !ben3.isEmpty() && jt7.getList("Patterns", 10).size() >= 6;
            if (boolean8) {
                this.selectedBannerPatternIndex.set(0);
            }
            else {
                this.selectedBannerPatternIndex.set(((BannerPatternItem)ben5.getItem()).getBannerPattern().ordinal());
            }
        }
        this.setupResultSlot();
        this.broadcastChanges();
    }
    
    public void registerUpdateListener(final Runnable runnable) {
        this.slotUpdateListener = runnable;
    }
    
    @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 == this.resultSlot.index) {
                if (!this.moveItemStackTo(ben5, 4, 40, true)) {
                    return ItemStack.EMPTY;
                }
                bca5.onQuickCraft(ben5, ben4);
            }
            else if (integer == this.dyeSlot.index || integer == this.bannerSlot.index || integer == this.patternSlot.index) {
                if (!this.moveItemStackTo(ben5, 4, 40, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (ben5.getItem() instanceof BannerItem) {
                if (!this.moveItemStackTo(ben5, this.bannerSlot.index, this.bannerSlot.index + 1, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (ben5.getItem() instanceof DyeItem) {
                if (!this.moveItemStackTo(ben5, this.dyeSlot.index, this.dyeSlot.index + 1, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (ben5.getItem() instanceof BannerPatternItem) {
                if (!this.moveItemStackTo(ben5, this.patternSlot.index, this.patternSlot.index + 1, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 4 && integer < 31) {
                if (!this.moveItemStackTo(ben5, 31, 40, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 31 && integer < 40 && !this.moveItemStackTo(ben5, 4, 31, 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;
    }
    
    @Override
    public void removed(final Player ayj) {
        super.removed(ayj);
        this.access.execute((bjw, fk) -> this.clearContainer(ayj, ayj.level, this.inputContainer));
    }
    
    private void setupResultSlot() {
        if (this.selectedBannerPatternIndex.get() > 0) {
            final ItemStack ben2 = this.bannerSlot.getItem();
            final ItemStack ben3 = this.dyeSlot.getItem();
            ItemStack ben4 = ItemStack.EMPTY;
            if (!ben2.isEmpty() && !ben3.isEmpty()) {
                ben4 = ben2.copy();
                ben4.setCount(1);
                final BannerPattern bwd5 = BannerPattern.values()[this.selectedBannerPatternIndex.get()];
                final DyeColor bdj6 = ((DyeItem)ben3.getItem()).getDyeColor();
                final CompoundTag jt7 = ben4.getOrCreateTagElement("BlockEntityTag");
                ListTag jz8;
                if (jt7.contains("Patterns", 9)) {
                    jz8 = jt7.getList("Patterns", 10);
                }
                else {
                    jz8 = new ListTag();
                    jt7.put("Patterns", jz8);
                }
                final CompoundTag jt8 = new CompoundTag();
                jt8.putString("Pattern", bwd5.getHashname());
                jt8.putInt("Color", bdj6.getId());
                ((AbstractList<CompoundTag>)jz8).add(jt8);
            }
            if (!ItemStack.matches(ben4, this.resultSlot.getItem())) {
                this.resultSlot.set(ben4);
            }
        }
    }
    
    public Slot getBannerSlot() {
        return this.bannerSlot;
    }
    
    public Slot getDyeSlot() {
        return this.dyeSlot;
    }
    
    public Slot getPatternSlot() {
        return this.patternSlot;
    }
    
    public Slot getResultSlot() {
        return this.resultSlot;
    }
}
