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

import javax.annotation.Nullable;
import java.util.Iterator;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.client.renderer.texture.TextureManager;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.renderer.entity.ItemRenderer;
import com.mojang.blaze3d.vertex.PoseStack;
import com.google.common.collect.Maps;
import java.util.Map;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemStack;
import net.minecraft.advancements.DisplayInfo;
import net.minecraft.advancements.Advancement;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiComponent;

public class AdvancementTab extends GuiComponent {
    private final Minecraft minecraft;
    private final AdvancementsScreen screen;
    private final AdvancementTabType type;
    private final int index;
    private final Advancement advancement;
    private final DisplayInfo display;
    private final ItemStack icon;
    private final Component title;
    private final AdvancementWidget root;
    private final Map<Advancement, AdvancementWidget> widgets;
    private double scrollX;
    private double scrollY;
    private int minX;
    private int minY;
    private int maxX;
    private int maxY;
    private float fade;
    private boolean centered;
    
    public AdvancementTab(final Minecraft dlx, final AdvancementsScreen drg, final AdvancementTabType drd, final int integer, final Advancement w, final DisplayInfo af) {
        this.widgets = Maps.newLinkedHashMap();
        this.minX = Integer.MAX_VALUE;
        this.minY = Integer.MAX_VALUE;
        this.maxX = Integer.MIN_VALUE;
        this.maxY = Integer.MIN_VALUE;
        this.minecraft = dlx;
        this.screen = drg;
        this.type = drd;
        this.index = integer;
        this.advancement = w;
        this.display = af;
        this.icon = af.getIcon();
        this.title = af.getTitle();
        this.addWidget(this.root = new AdvancementWidget(this, dlx, w, af), w);
    }
    
    public Advancement getAdvancement() {
        return this.advancement;
    }
    
    public Component getTitle() {
        return this.title;
    }
    
    public void drawTab(final PoseStack dhl, final int integer2, final int integer3, final boolean boolean4) {
        this.type.draw(dhl, this, integer2, integer3, boolean4, this.index);
    }
    
    public void drawIcon(final int integer1, final int integer2, final ItemRenderer ehh) {
        this.type.drawIcon(integer1, integer2, this.index, ehh, this.icon);
    }
    
    public void drawContents(final PoseStack dhl) {
        if (!this.centered) {
            this.scrollX = 117 - (this.maxX + this.minX) / 2;
            this.scrollY = 56 - (this.maxY + this.minY) / 2;
            this.centered = true;
        }
        RenderSystem.pushMatrix();
        RenderSystem.enableDepthTest();
        RenderSystem.translatef(0.0f, 0.0f, 950.0f);
        RenderSystem.colorMask(false, false, false, false);
        GuiComponent.fill(dhl, 4680, 2260, -4680, -2260, -16777216);
        RenderSystem.colorMask(true, true, true, true);
        RenderSystem.translatef(0.0f, 0.0f, -950.0f);
        RenderSystem.depthFunc(518);
        GuiComponent.fill(dhl, 234, 113, 0, 0, -16777216);
        RenderSystem.depthFunc(515);
        final ResourceLocation uh3 = this.display.getBackground();
        if (uh3 != null) {
            this.minecraft.getTextureManager().bind(uh3);
        }
        else {
            this.minecraft.getTextureManager().bind(TextureManager.INTENTIONAL_MISSING_TEXTURE);
        }
        final int integer4 = Mth.floor(this.scrollX);
        final int integer5 = Mth.floor(this.scrollY);
        final int integer6 = integer4 % 16;
        final int integer7 = integer5 % 16;
        for (int integer8 = -1; integer8 <= 15; ++integer8) {
            for (int integer9 = -1; integer9 <= 8; ++integer9) {
                GuiComponent.blit(dhl, integer6 + 16 * integer8, integer7 + 16 * integer9, 0.0f, 0.0f, 16, 16, 16, 16);
            }
        }
        this.root.drawConnectivity(dhl, integer4, integer5, true);
        this.root.drawConnectivity(dhl, integer4, integer5, false);
        this.root.draw(dhl, integer4, integer5);
        RenderSystem.depthFunc(518);
        RenderSystem.translatef(0.0f, 0.0f, -950.0f);
        RenderSystem.colorMask(false, false, false, false);
        GuiComponent.fill(dhl, 4680, 2260, -4680, -2260, -16777216);
        RenderSystem.colorMask(true, true, true, true);
        RenderSystem.translatef(0.0f, 0.0f, 950.0f);
        RenderSystem.depthFunc(515);
        RenderSystem.popMatrix();
    }
    
    public void drawTooltips(final PoseStack dhl, final int integer2, final int integer3, final int integer4, final int integer5) {
        RenderSystem.pushMatrix();
        RenderSystem.translatef(0.0f, 0.0f, 200.0f);
        GuiComponent.fill(dhl, 0, 0, 234, 113, Mth.floor(this.fade * 255.0f) << 24);
        boolean boolean7 = false;
        final int integer6 = Mth.floor(this.scrollX);
        final int integer7 = Mth.floor(this.scrollY);
        if (integer2 > 0 && integer2 < 234 && integer3 > 0 && integer3 < 113) {
            for (final AdvancementWidget dre11 : this.widgets.values()) {
                if (dre11.isMouseOver(integer6, integer7, integer2, integer3)) {
                    boolean7 = true;
                    dre11.drawHover(dhl, integer6, integer7, this.fade, integer4, integer5);
                    break;
                }
            }
        }
        RenderSystem.popMatrix();
        if (boolean7) {
            this.fade = Mth.clamp(this.fade + 0.02f, 0.0f, 0.3f);
        }
        else {
            this.fade = Mth.clamp(this.fade - 0.04f, 0.0f, 1.0f);
        }
    }
    
    public boolean isMouseOver(final int integer1, final int integer2, final double double3, final double double4) {
        return this.type.isMouseOver(integer1, integer2, this.index, double3, double4);
    }
    
    @Nullable
    public static AdvancementTab create(final Minecraft dlx, final AdvancementsScreen drg, int integer, final Advancement w) {
        if (w.getDisplay() == null) {
            return null;
        }
        for (final AdvancementTabType drd8 : AdvancementTabType.values()) {
            if (integer < drd8.getMax()) {
                return new AdvancementTab(dlx, drg, drd8, integer, w, w.getDisplay());
            }
            integer -= drd8.getMax();
        }
        return null;
    }
    
    public void scroll(final double double1, final double double2) {
        if (this.maxX - this.minX > 234) {
            this.scrollX = Mth.clamp(this.scrollX + double1, -(this.maxX - 234), 0.0);
        }
        if (this.maxY - this.minY > 113) {
            this.scrollY = Mth.clamp(this.scrollY + double2, -(this.maxY - 113), 0.0);
        }
    }
    
    public void addAdvancement(final Advancement w) {
        if (w.getDisplay() == null) {
            return;
        }
        final AdvancementWidget dre3 = new AdvancementWidget(this, this.minecraft, w, w.getDisplay());
        this.addWidget(dre3, w);
    }
    
    private void addWidget(final AdvancementWidget dre, final Advancement w) {
        this.widgets.put(w, dre);
        final int integer4 = dre.getX();
        final int integer5 = integer4 + 28;
        final int integer6 = dre.getY();
        final int integer7 = integer6 + 27;
        this.minX = Math.min(this.minX, integer4);
        this.maxX = Math.max(this.maxX, integer5);
        this.minY = Math.min(this.minY, integer6);
        this.maxY = Math.max(this.maxY, integer7);
        for (final AdvancementWidget dre2 : this.widgets.values()) {
            dre2.attachToParent();
        }
    }
    
    @Nullable
    public AdvancementWidget getWidget(final Advancement w) {
        return this.widgets.get(w);
    }
    
    public AdvancementsScreen getScreen() {
        return this.screen;
    }
}
