package net.minecraft.client.gui.screens.inventory;

import net.minecraft.world.item.BannerItem;
import net.minecraft.util.Mth;
import net.minecraft.client.resources.sounds.SoundInstance;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.client.Minecraft;
import net.minecraft.world.entity.player.Player;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.block.entity.BannerBlockEntity;
import net.minecraft.nbt.Tag;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.item.Items;
import net.minecraft.world.inventory.Slot;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.resources.model.ModelBakery;
import net.minecraft.client.renderer.texture.OverlayTexture;
import com.mojang.blaze3d.platform.Lighting;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.blockentity.BannerRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;
import javax.annotation.Nullable;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.level.block.entity.BannerPattern;
import com.mojang.datafixers.util.Pair;
import java.util.List;
import net.minecraft.client.model.geom.ModelPart;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.LoomMenu;

public class LoomScreen extends AbstractContainerScreen<LoomMenu> {
    private static final ResourceLocation BG_LOCATION;
    private static final int TOTAL_PATTERN_ROWS;
    private final ModelPart flag;
    @Nullable
    private List<Pair<BannerPattern, DyeColor>> resultBannerPatterns;
    private ItemStack bannerStack;
    private ItemStack dyeStack;
    private ItemStack patternStack;
    private boolean displayPatterns;
    private boolean displaySpecialPattern;
    private boolean hasMaxPatterns;
    private float scrollOffs;
    private boolean scrolling;
    private int startIndex;
    
    public LoomScreen(final LoomMenu bhi, final Inventory beb, final Component mr) {
        super(bhi, beb, mr);
        this.bannerStack = ItemStack.EMPTY;
        this.dyeStack = ItemStack.EMPTY;
        this.patternStack = ItemStack.EMPTY;
        this.startIndex = 1;
        this.flag = BannerRenderer.makeFlag();
        bhi.registerUpdateListener(this::containerChanged);
        this.titleLabelY -= 2;
    }
    
    @Override
    public void render(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
        super.render(dhl, integer2, integer3, float4);
        this.renderTooltip(dhl, integer2, integer3);
    }
    
    @Override
    protected void renderBg(final PoseStack dhl, final float float2, final int integer3, final int integer4) {
        this.renderBackground(dhl);
        this.minecraft.getTextureManager().bind(LoomScreen.BG_LOCATION);
        final int integer5 = this.leftPos;
        final int integer6 = this.topPos;
        this.blit(dhl, integer5, integer6, 0, 0, this.imageWidth, this.imageHeight);
        final Slot bhw8 = ((LoomMenu)this.menu).getBannerSlot();
        final Slot bhw9 = ((LoomMenu)this.menu).getDyeSlot();
        final Slot bhw10 = ((LoomMenu)this.menu).getPatternSlot();
        final Slot bhw11 = ((LoomMenu)this.menu).getResultSlot();
        if (!bhw8.hasItem()) {
            this.blit(dhl, integer5 + bhw8.x, integer6 + bhw8.y, this.imageWidth, 0, 16, 16);
        }
        if (!bhw9.hasItem()) {
            this.blit(dhl, integer5 + bhw9.x, integer6 + bhw9.y, this.imageWidth + 16, 0, 16, 16);
        }
        if (!bhw10.hasItem()) {
            this.blit(dhl, integer5 + bhw10.x, integer6 + bhw10.y, this.imageWidth + 32, 0, 16, 16);
        }
        final int integer7 = (int)(41.0f * this.scrollOffs);
        this.blit(dhl, integer5 + 119, integer6 + 13 + integer7, 232 + (this.displayPatterns ? 0 : 12), 0, 12, 15);
        Lighting.setupForFlatItems();
        if (this.resultBannerPatterns != null && !this.hasMaxPatterns) {
            final MultiBufferSource.BufferSource a13 = this.minecraft.renderBuffers().bufferSource();
            dhl.pushPose();
            dhl.translate(integer5 + 139, integer6 + 52, 0.0);
            dhl.scale(24.0f, -24.0f, 1.0f);
            dhl.translate(0.5, 0.5, 0.5);
            final float float3 = 0.6666667f;
            dhl.scale(0.6666667f, -0.6666667f, -0.6666667f);
            this.flag.xRot = 0.0f;
            this.flag.y = -32.0f;
            BannerRenderer.renderPatterns(dhl, a13, 15728880, OverlayTexture.NO_OVERLAY, this.flag, ModelBakery.BANNER_BASE, true, this.resultBannerPatterns);
            dhl.popPose();
            a13.endBatch();
        }
        else if (this.hasMaxPatterns) {
            this.blit(dhl, integer5 + bhw11.x - 2, integer6 + bhw11.y - 2, this.imageWidth, 17, 17, 16);
        }
        if (this.displayPatterns) {
            final int integer8 = integer5 + 60;
            final int integer9 = integer6 + 13;
            for (int integer10 = this.startIndex + 16, integer11 = this.startIndex; integer11 < integer10 && integer11 < BannerPattern.COUNT - BannerPattern.PATTERN_ITEM_COUNT; ++integer11) {
                final int integer12 = integer11 - this.startIndex;
                final int integer13 = integer8 + integer12 % 4 * 14;
                final int integer14 = integer9 + integer12 / 4 * 14;
                this.minecraft.getTextureManager().bind(LoomScreen.BG_LOCATION);
                int integer15 = this.imageHeight;
                if (integer11 == ((LoomMenu)this.menu).getSelectedBannerPatternIndex()) {
                    integer15 += 14;
                }
                else if (integer3 >= integer13 && integer4 >= integer14 && integer3 < integer13 + 14 && integer4 < integer14 + 14) {
                    integer15 += 28;
                }
                this.blit(dhl, integer13, integer14, 0, integer15, 14, 14);
                this.renderPattern(integer11, integer13, integer14);
            }
        }
        else if (this.displaySpecialPattern) {
            final int integer8 = integer5 + 60;
            final int integer9 = integer6 + 13;
            this.minecraft.getTextureManager().bind(LoomScreen.BG_LOCATION);
            this.blit(dhl, integer8, integer9, 0, this.imageHeight, 14, 14);
            final int integer10 = ((LoomMenu)this.menu).getSelectedBannerPatternIndex();
            this.renderPattern(integer10, integer8, integer9);
        }
        Lighting.setupFor3DItems();
    }
    
    private void renderPattern(final int integer1, final int integer2, final int integer3) {
        final ItemStack bki5 = new ItemStack(Items.GRAY_BANNER);
        final CompoundTag le6 = bki5.getOrCreateTagElement("BlockEntityTag");
        final ListTag lk7 = new BannerPattern.Builder().addPattern(BannerPattern.BASE, DyeColor.GRAY).addPattern(BannerPattern.values()[integer1], DyeColor.WHITE).toListTag();
        le6.put("Patterns", lk7);
        final PoseStack dhl8 = new PoseStack();
        dhl8.pushPose();
        dhl8.translate(integer2 + 0.5f, integer3 + 16, 0.0);
        dhl8.scale(6.0f, -6.0f, 1.0f);
        dhl8.translate(0.5, 0.5, 0.0);
        dhl8.translate(0.5, 0.5, 0.5);
        final float float9 = 0.6666667f;
        dhl8.scale(0.6666667f, -0.6666667f, -0.6666667f);
        final MultiBufferSource.BufferSource a10 = this.minecraft.renderBuffers().bufferSource();
        this.flag.xRot = 0.0f;
        this.flag.y = -32.0f;
        final List<Pair<BannerPattern, DyeColor>> list11 = BannerBlockEntity.createPatterns(DyeColor.GRAY, BannerBlockEntity.getItemPatterns(bki5));
        BannerRenderer.renderPatterns(dhl8, a10, 15728880, OverlayTexture.NO_OVERLAY, this.flag, ModelBakery.BANNER_BASE, true, list11);
        dhl8.popPose();
        a10.endBatch();
    }
    
    @Override
    public boolean mouseClicked(final double double1, final double double2, final int integer) {
        this.scrolling = false;
        if (this.displayPatterns) {
            int integer2 = this.leftPos + 60;
            int integer3 = this.topPos + 13;
            for (int integer4 = this.startIndex + 16, integer5 = this.startIndex; integer5 < integer4; ++integer5) {
                final int integer6 = integer5 - this.startIndex;
                final double double3 = double1 - (integer2 + integer6 % 4 * 14);
                final double double4 = double2 - (integer3 + integer6 / 4 * 14);
                if (double3 >= 0.0 && double4 >= 0.0 && double3 < 14.0 && double4 < 14.0 && ((LoomMenu)this.menu).clickMenuButton(this.minecraft.player, integer5)) {
                    Minecraft.getInstance().getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_LOOM_SELECT_PATTERN, 1.0f));
                    this.minecraft.gameMode.handleInventoryButtonClick(((LoomMenu)this.menu).containerId, integer5);
                    return true;
                }
            }
            integer2 = this.leftPos + 119;
            integer3 = this.topPos + 9;
            if (double1 >= integer2 && double1 < integer2 + 12 && double2 >= integer3 && double2 < integer3 + 56) {
                this.scrolling = true;
            }
        }
        return super.mouseClicked(double1, double2, integer);
    }
    
    @Override
    public boolean mouseDragged(final double double1, final double double2, final int integer, final double double4, final double double5) {
        if (this.scrolling && this.displayPatterns) {
            final int integer2 = this.topPos + 13;
            final int integer3 = integer2 + 56;
            this.scrollOffs = ((float)double2 - integer2 - 7.5f) / (integer3 - integer2 - 15.0f);
            this.scrollOffs = Mth.clamp(this.scrollOffs, 0.0f, 1.0f);
            final int integer4 = LoomScreen.TOTAL_PATTERN_ROWS - 4;
            int integer5 = (int)(this.scrollOffs * integer4 + 0.5);
            if (integer5 < 0) {
                integer5 = 0;
            }
            this.startIndex = 1 + integer5 * 4;
            return true;
        }
        return super.mouseDragged(double1, double2, integer, double4, double5);
    }
    
    @Override
    public boolean mouseScrolled(final double double1, final double double2, final double double3) {
        if (this.displayPatterns) {
            final int integer8 = LoomScreen.TOTAL_PATTERN_ROWS - 4;
            this.scrollOffs -= (float)(double3 / integer8);
            this.scrollOffs = Mth.clamp(this.scrollOffs, 0.0f, 1.0f);
            this.startIndex = 1 + (int)(this.scrollOffs * integer8 + 0.5) * 4;
        }
        return true;
    }
    
    @Override
    protected boolean hasClickedOutside(final double double1, final double double2, final int integer3, final int integer4, final int integer5) {
        return double1 < integer3 || double2 < integer4 || double1 >= integer3 + this.imageWidth || double2 >= integer4 + this.imageHeight;
    }
    
    private void containerChanged() {
        final ItemStack bki2 = ((LoomMenu)this.menu).getResultSlot().getItem();
        if (bki2.isEmpty()) {
            this.resultBannerPatterns = null;
        }
        else {
            this.resultBannerPatterns = BannerBlockEntity.createPatterns(((BannerItem)bki2.getItem()).getColor(), BannerBlockEntity.getItemPatterns(bki2));
        }
        final ItemStack bki3 = ((LoomMenu)this.menu).getBannerSlot().getItem();
        final ItemStack bki4 = ((LoomMenu)this.menu).getDyeSlot().getItem();
        final ItemStack bki5 = ((LoomMenu)this.menu).getPatternSlot().getItem();
        final CompoundTag le6 = bki3.getOrCreateTagElement("BlockEntityTag");
        this.hasMaxPatterns = (le6.contains("Patterns", 9) && !bki3.isEmpty() && le6.getList("Patterns", 10).size() >= 6);
        if (this.hasMaxPatterns) {
            this.resultBannerPatterns = null;
        }
        if (!ItemStack.matches(bki3, this.bannerStack) || !ItemStack.matches(bki4, this.dyeStack) || !ItemStack.matches(bki5, this.patternStack)) {
            this.displayPatterns = (!bki3.isEmpty() && !bki4.isEmpty() && bki5.isEmpty() && !this.hasMaxPatterns);
            this.displaySpecialPattern = (!this.hasMaxPatterns && !bki5.isEmpty() && !bki3.isEmpty() && !bki4.isEmpty());
        }
        this.bannerStack = bki3.copy();
        this.dyeStack = bki4.copy();
        this.patternStack = bki5.copy();
    }
    
    static {
        BG_LOCATION = new ResourceLocation("textures/gui/container/loom.png");
        TOTAL_PATTERN_ROWS = (BannerPattern.COUNT - BannerPattern.PATTERN_ITEM_COUNT - 1 + 4 - 1) / 4;
    }
}
