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

import javax.annotation.Nullable;
import net.minecraft.advancements.AdvancementProgress;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.client.gui.Font;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.client.gui.GuiComponent;
import com.mojang.blaze3d.vertex.PoseStack;
import java.util.Iterator;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ServerboundSeenAdvancementsPacket;
import com.google.common.collect.Maps;
import net.minecraft.client.gui.chat.NarratorChatListener;
import net.minecraft.advancements.Advancement;
import java.util.Map;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.client.multiplayer.ClientAdvancements;
import net.minecraft.client.gui.screens.Screen;

public class AdvancementsScreen extends Screen implements ClientAdvancements.Listener {
    private static final ResourceLocation WINDOW_LOCATION;
    private static final ResourceLocation TABS_LOCATION;
    private final ClientAdvancements advancements;
    private final Map<Advancement, AdvancementTab> tabs;
    private AdvancementTab selectedTab;
    private boolean isScrolling;
    
    public AdvancementsScreen(final ClientAdvancements dyj) {
        super(NarratorChatListener.NO_TITLE);
        this.tabs = Maps.newLinkedHashMap();
        this.advancements = dyj;
    }
    
    @Override
    protected void init() {
        this.tabs.clear();
        this.selectedTab = null;
        this.advancements.setListener(this);
        if (this.selectedTab == null && !this.tabs.isEmpty()) {
            this.advancements.setSelectedTab(this.tabs.values().iterator().next().getAdvancement(), true);
        }
        else {
            this.advancements.setSelectedTab((this.selectedTab == null) ? null : this.selectedTab.getAdvancement(), true);
        }
    }
    
    @Override
    public void removed() {
        this.advancements.setListener(null);
        final ClientPacketListener dyn2 = this.minecraft.getConnection();
        if (dyn2 != null) {
            dyn2.send(ServerboundSeenAdvancementsPacket.closedScreen());
        }
    }
    
    @Override
    public boolean mouseClicked(final double double1, final double double2, final int integer) {
        if (integer == 0) {
            final int integer2 = (this.width - 252) / 2;
            final int integer3 = (this.height - 140) / 2;
            for (final AdvancementTab drc10 : this.tabs.values()) {
                if (drc10.isMouseOver(integer2, integer3, double1, double2)) {
                    this.advancements.setSelectedTab(drc10.getAdvancement(), true);
                    break;
                }
            }
        }
        return super.mouseClicked(double1, double2, integer);
    }
    
    @Override
    public boolean keyPressed(final int integer1, final int integer2, final int integer3) {
        if (this.minecraft.options.keyAdvancements.matches(integer1, integer2)) {
            this.minecraft.setScreen(null);
            this.minecraft.mouseHandler.grabMouse();
            return true;
        }
        return super.keyPressed(integer1, integer2, integer3);
    }
    
    @Override
    public void render(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
        final int integer4 = (this.width - 252) / 2;
        final int integer5 = (this.height - 140) / 2;
        this.renderBackground(dhl);
        this.renderInside(dhl, integer2, integer3, integer4, integer5);
        this.renderWindow(dhl, integer4, integer5);
        this.renderTooltips(dhl, integer2, integer3, integer4, integer5);
    }
    
    @Override
    public boolean mouseDragged(final double double1, final double double2, final int integer, final double double4, final double double5) {
        if (integer != 0) {
            return this.isScrolling = false;
        }
        if (!this.isScrolling) {
            this.isScrolling = true;
        }
        else if (this.selectedTab != null) {
            this.selectedTab.scroll(double4, double5);
        }
        return true;
    }
    
    private void renderInside(final PoseStack dhl, final int integer2, final int integer3, final int integer4, final int integer5) {
        final AdvancementTab drc7 = this.selectedTab;
        if (drc7 == null) {
            GuiComponent.fill(dhl, integer4 + 9, integer5 + 18, integer4 + 9 + 234, integer5 + 18 + 113, -16777216);
            final String string8 = I18n.get("advancements.empty");
            final int integer6 = this.font.width(string8);
            final Font font = this.font;
            final String string9 = string8;
            final float float3 = (float)(integer4 + 9 + 117 - integer6 / 2);
            final int n = integer5 + 18 + 56;
            this.font.getClass();
            font.draw(dhl, string9, float3, (float)(n - 9 / 2), -1);
            final Font font2 = this.font;
            final String string10 = ":(";
            final float float4 = (float)(integer4 + 9 + 117 - this.font.width(":(") / 2);
            final int n2 = integer5 + 18 + 113;
            this.font.getClass();
            font2.draw(dhl, string10, float4, (float)(n2 - 9), -1);
            return;
        }
        RenderSystem.pushMatrix();
        RenderSystem.translatef((float)(integer4 + 9), (float)(integer5 + 18), 0.0f);
        drc7.drawContents(dhl);
        RenderSystem.popMatrix();
        RenderSystem.depthFunc(515);
        RenderSystem.disableDepthTest();
    }
    
    public void renderWindow(final PoseStack dhl, final int integer2, final int integer3) {
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        RenderSystem.enableBlend();
        this.minecraft.getTextureManager().bind(AdvancementsScreen.WINDOW_LOCATION);
        this.blit(dhl, integer2, integer3, 0, 0, 252, 140);
        if (this.tabs.size() > 1) {
            this.minecraft.getTextureManager().bind(AdvancementsScreen.TABS_LOCATION);
            for (final AdvancementTab drc6 : this.tabs.values()) {
                drc6.drawTab(dhl, integer2, integer3, drc6 == this.selectedTab);
            }
            RenderSystem.enableRescaleNormal();
            RenderSystem.defaultBlendFunc();
            for (final AdvancementTab drc6 : this.tabs.values()) {
                drc6.drawIcon(integer2, integer3, this.itemRenderer);
            }
            RenderSystem.disableBlend();
        }
        this.font.draw(dhl, I18n.get("gui.advancements"), (float)(integer2 + 8), (float)(integer3 + 6), 4210752);
    }
    
    private void renderTooltips(final PoseStack dhl, final int integer2, final int integer3, final int integer4, final int integer5) {
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        if (this.selectedTab != null) {
            RenderSystem.pushMatrix();
            RenderSystem.enableDepthTest();
            RenderSystem.translatef((float)(integer4 + 9), (float)(integer5 + 18), 400.0f);
            this.selectedTab.drawTooltips(dhl, integer2 - integer4 - 9, integer3 - integer5 - 18, integer4, integer5);
            RenderSystem.disableDepthTest();
            RenderSystem.popMatrix();
        }
        if (this.tabs.size() > 1) {
            for (final AdvancementTab drc8 : this.tabs.values()) {
                if (drc8.isMouseOver(integer4, integer5, integer2, integer3)) {
                    this.renderTooltip(dhl, drc8.getTitle(), integer2, integer3);
                }
            }
        }
    }
    
    @Override
    public void onAddAdvancementRoot(final Advancement w) {
        final AdvancementTab drc3 = AdvancementTab.create(this.minecraft, this, this.tabs.size(), w);
        if (drc3 == null) {
            return;
        }
        this.tabs.put(w, drc3);
    }
    
    @Override
    public void onRemoveAdvancementRoot(final Advancement w) {
    }
    
    @Override
    public void onAddAdvancementTask(final Advancement w) {
        final AdvancementTab drc3 = this.getTab(w);
        if (drc3 != null) {
            drc3.addAdvancement(w);
        }
    }
    
    @Override
    public void onRemoveAdvancementTask(final Advancement w) {
    }
    
    @Override
    public void onUpdateAdvancementProgress(final Advancement w, final AdvancementProgress y) {
        final AdvancementWidget dre4 = this.getAdvancementWidget(w);
        if (dre4 != null) {
            dre4.setProgress(y);
        }
    }
    
    @Override
    public void onSelectedTabChanged(@Nullable final Advancement w) {
        this.selectedTab = this.tabs.get(w);
    }
    
    @Override
    public void onAdvancementsCleared() {
        this.tabs.clear();
        this.selectedTab = null;
    }
    
    @Nullable
    public AdvancementWidget getAdvancementWidget(final Advancement w) {
        final AdvancementTab drc3 = this.getTab(w);
        return (drc3 == null) ? null : drc3.getWidget(w);
    }
    
    @Nullable
    private AdvancementTab getTab(Advancement w) {
        while (w.getParent() != null) {
            w = w.getParent();
        }
        return this.tabs.get(w);
    }
    
    static {
        WINDOW_LOCATION = new ResourceLocation("textures/gui/advancements/window.png");
        TABS_LOCATION = new ResourceLocation("textures/gui/advancements/tabs.png");
    }
}
