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 beb) {
        this(integer, beb, ContainerLevelAccess.NULL);
    }
    
    public LoomMenu(final int integer, final Inventory beb, final ContainerLevelAccess bgs) {
        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 = bgs;
        this.bannerSlot = this.addSlot(new Slot(this.inputContainer, 0, 13, 26) {
            @Override
            public boolean mayPlace(final ItemStack bki) {
                return bki.getItem() instanceof BannerItem;
            }
        });
        this.dyeSlot = this.addSlot(new Slot(this.inputContainer, 1, 33, 26) {
            @Override
            public boolean mayPlace(final ItemStack bki) {
                return bki.getItem() instanceof DyeItem;
            }
        });
        this.patternSlot = this.addSlot(new Slot(this.inputContainer, 2, 23, 45) {
            @Override
            public boolean mayPlace(final ItemStack bki) {
                return bki.getItem() instanceof BannerPatternItem;
            }
        });
        this.resultSlot = this.addSlot(new Slot(this.outputContainer, 0, 143, 58) {
            @Override
            public boolean mayPlace(final ItemStack bki) {
                return false;
            }
            
            @Override
            public ItemStack onTake(final Player bec, final ItemStack bki) {
                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;
                bgs.execute((bqb, fu) -> {
                    long4 = bqb.getGameTime();
                    if (LoomMenu.this.lastSoundTime != long4) {
                        bqb.playSound(null, fu, SoundEvents.UI_LOOM_TAKE_RESULT, SoundSource.BLOCKS, 1.0f, 1.0f);
                        LoomMenu.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));
        }
        this.addDataSlot(this.selectedBannerPatternIndex);
    }
    
    public int getSelectedBannerPatternIndex() {
        return this.selectedBannerPatternIndex.get();
    }
    
    @Override
    public boolean stillValid(final Player bec) {
        return AbstractContainerMenu.stillValid(this.access, bec, Blocks.LOOM);
    }
    
    @Override
    public boolean clickMenuButton(final Player bec, 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 amz) {
        final ItemStack bki3 = this.bannerSlot.getItem();
        final ItemStack bki4 = this.dyeSlot.getItem();
        final ItemStack bki5 = this.patternSlot.getItem();
        final ItemStack bki6 = this.resultSlot.getItem();
        if (!bki6.isEmpty() && (bki3.isEmpty() || bki4.isEmpty() || this.selectedBannerPatternIndex.get() <= 0 || (this.selectedBannerPatternIndex.get() >= BannerPattern.COUNT - BannerPattern.PATTERN_ITEM_COUNT && bki5.isEmpty()))) {
            this.resultSlot.set(ItemStack.EMPTY);
            this.selectedBannerPatternIndex.set(0);
        }
        else if (!bki5.isEmpty() && bki5.getItem() instanceof BannerPatternItem) {
            final CompoundTag le7 = bki3.getOrCreateTagElement("BlockEntityTag");
            final boolean boolean8 = le7.contains("Patterns", 9) && !bki3.isEmpty() && le7.getList("Patterns", 10).size() >= 6;
            if (boolean8) {
                this.selectedBannerPatternIndex.set(0);
            }
            else {
                this.selectedBannerPatternIndex.set(((BannerPatternItem)bki5.getItem()).getBannerPattern().ordinal());
            }
        }
        this.setupResultSlot();
        this.broadcastChanges();
    }
    
    public void registerUpdateListener(final Runnable runnable) {
        this.slotUpdateListener = runnable;
    }
    
    @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 == this.resultSlot.index) {
                if (!this.moveItemStackTo(bki5, 4, 40, true)) {
                    return ItemStack.EMPTY;
                }
                bhw5.onQuickCraft(bki5, bki4);
            }
            else if (integer == this.dyeSlot.index || integer == this.bannerSlot.index || integer == this.patternSlot.index) {
                if (!this.moveItemStackTo(bki5, 4, 40, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (bki5.getItem() instanceof BannerItem) {
                if (!this.moveItemStackTo(bki5, this.bannerSlot.index, this.bannerSlot.index + 1, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (bki5.getItem() instanceof DyeItem) {
                if (!this.moveItemStackTo(bki5, this.dyeSlot.index, this.dyeSlot.index + 1, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (bki5.getItem() instanceof BannerPatternItem) {
                if (!this.moveItemStackTo(bki5, this.patternSlot.index, this.patternSlot.index + 1, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 4 && integer < 31) {
                if (!this.moveItemStackTo(bki5, 31, 40, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 31 && integer < 40 && !this.moveItemStackTo(bki5, 4, 31, false)) {
                return ItemStack.EMPTY;
            }
            if (bki5.isEmpty()) {
                bhw5.set(ItemStack.EMPTY);
            }
            else {
                bhw5.setChanged();
            }
            if (bki5.getCount() == bki4.getCount()) {
                return ItemStack.EMPTY;
            }
            bhw5.onTake(bec, bki5);
        }
        return bki4;
    }
    
    @Override
    public void removed(final Player bec) {
        super.removed(bec);
        this.access.execute((bqb, fu) -> this.clearContainer(bec, bec.level, this.inputContainer));
    }
    
    private void setupResultSlot() {
        if (this.selectedBannerPatternIndex.get() > 0) {
            final ItemStack bki2 = this.bannerSlot.getItem();
            final ItemStack bki3 = this.dyeSlot.getItem();
            ItemStack bki4 = ItemStack.EMPTY;
            if (!bki2.isEmpty() && !bki3.isEmpty()) {
                bki4 = bki2.copy();
                bki4.setCount(1);
                final BannerPattern cdd5 = BannerPattern.values()[this.selectedBannerPatternIndex.get()];
                final DyeColor bje6 = ((DyeItem)bki3.getItem()).getDyeColor();
                final CompoundTag le7 = bki4.getOrCreateTagElement("BlockEntityTag");
                ListTag lk8;
                if (le7.contains("Patterns", 9)) {
                    lk8 = le7.getList("Patterns", 10);
                }
                else {
                    lk8 = new ListTag();
                    le7.put("Patterns", lk8);
                }
                final CompoundTag le8 = new CompoundTag();
                le8.putString("Pattern", cdd5.getHashname());
                le8.putInt("Color", bje6.getId());
                ((AbstractList<CompoundTag>)lk8).add(le8);
            }
            if (!ItemStack.matches(bki4, this.resultSlot.getItem())) {
                this.resultSlot.set(bki4);
            }
        }
    }
    
    public Slot getBannerSlot() {
        return this.bannerSlot;
    }
    
    public Slot getDyeSlot() {
        return this.dyeSlot;
    }
    
    public Slot getPatternSlot() {
        return this.patternSlot;
    }
    
    public Slot getResultSlot() {
        return this.resultSlot;
    }
}
