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

import javax.annotation.Nullable;
import net.minecraft.advancements.AdvancementProgress;
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 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 dnl) {
        super(NarratorChatListener.NO_TITLE);
        this.tabs = Maps.newLinkedHashMap();
        this.advancements = dnl;
    }
    
    @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 dnp2 = this.minecraft.getConnection();
        if (dnp2 != null) {
            dnp2.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 dgl10 : this.tabs.values()) {
                if (dgl10.isMouseOver(integer2, integer3, double1, double2)) {
                    this.advancements.setSelectedTab(dgl10.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 int integer1, final int integer2, final float float3) {
        final int integer3 = (this.width - 252) / 2;
        final int integer4 = (this.height - 140) / 2;
        this.renderBackground();
        this.renderInside(integer1, integer2, integer3, integer4);
        this.renderWindow(integer3, integer4);
        this.renderTooltips(integer1, integer2, integer3, integer4);
    }
    
    @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 int integer1, final int integer2, final int integer3, final int integer4) {
        final AdvancementTab dgl6 = this.selectedTab;
        if (dgl6 == null) {
            GuiComponent.fill(integer3 + 9, integer4 + 18, integer3 + 9 + 234, integer4 + 18 + 113, -16777216);
            final String string7 = I18n.get("advancements.empty");
            final int integer5 = this.font.width(string7);
            final Font font = this.font;
            final String string8 = string7;
            final float float2 = (float)(integer3 + 9 + 117 - integer5 / 2);
            final int n = integer4 + 18 + 56;
            this.font.getClass();
            font.draw(string8, float2, (float)(n - 9 / 2), -1);
            final Font font2 = this.font;
            final String string9 = ":(";
            final float float3 = (float)(integer3 + 9 + 117 - this.font.width(":(") / 2);
            final int n2 = integer4 + 18 + 113;
            this.font.getClass();
            font2.draw(string9, float3, (float)(n2 - 9), -1);
            return;
        }
        RenderSystem.pushMatrix();
        RenderSystem.translatef((float)(integer3 + 9), (float)(integer4 + 18), 0.0f);
        dgl6.drawContents();
        RenderSystem.popMatrix();
        RenderSystem.depthFunc(515);
        RenderSystem.disableDepthTest();
    }
    
    public void renderWindow(final int integer1, final int integer2) {
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        RenderSystem.enableBlend();
        this.minecraft.getTextureManager().bind(AdvancementsScreen.WINDOW_LOCATION);
        this.blit(integer1, integer2, 0, 0, 252, 140);
        if (this.tabs.size() > 1) {
            this.minecraft.getTextureManager().bind(AdvancementsScreen.TABS_LOCATION);
            for (final AdvancementTab dgl5 : this.tabs.values()) {
                dgl5.drawTab(integer1, integer2, dgl5 == this.selectedTab);
            }
            RenderSystem.enableRescaleNormal();
            RenderSystem.defaultBlendFunc();
            for (final AdvancementTab dgl5 : this.tabs.values()) {
                dgl5.drawIcon(integer1, integer2, this.itemRenderer);
            }
            RenderSystem.disableBlend();
        }
        this.font.draw(I18n.get("gui.advancements"), (float)(integer1 + 8), (float)(integer2 + 6), 4210752);
    }
    
    private void renderTooltips(final int integer1, final int integer2, final int integer3, final int integer4) {
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        if (this.selectedTab != null) {
            RenderSystem.pushMatrix();
            RenderSystem.enableDepthTest();
            RenderSystem.translatef((float)(integer3 + 9), (float)(integer4 + 18), 400.0f);
            this.selectedTab.drawTooltips(integer1 - integer3 - 9, integer2 - integer4 - 18, integer3, integer4);
            RenderSystem.disableDepthTest();
            RenderSystem.popMatrix();
        }
        if (this.tabs.size() > 1) {
            for (final AdvancementTab dgl7 : this.tabs.values()) {
                if (dgl7.isMouseOver(integer3, integer4, integer1, integer2)) {
                    this.renderTooltip(dgl7.getTitle(), integer1, integer2);
                }
            }
        }
    }
    
    @Override
    public void onAddAdvancementRoot(final Advancement u) {
        final AdvancementTab dgl3 = AdvancementTab.create(this.minecraft, this, this.tabs.size(), u);
        if (dgl3 == null) {
            return;
        }
        this.tabs.put(u, dgl3);
    }
    
    @Override
    public void onRemoveAdvancementRoot(final Advancement u) {
    }
    
    @Override
    public void onAddAdvancementTask(final Advancement u) {
        final AdvancementTab dgl3 = this.getTab(u);
        if (dgl3 != null) {
            dgl3.addAdvancement(u);
        }
    }
    
    @Override
    public void onRemoveAdvancementTask(final Advancement u) {
    }
    
    @Override
    public void onUpdateAdvancementProgress(final Advancement u, final AdvancementProgress w) {
        final AdvancementWidget dgn4 = this.getAdvancementWidget(u);
        if (dgn4 != null) {
            dgn4.setProgress(w);
        }
    }
    
    @Override
    public void onSelectedTabChanged(@Nullable final Advancement u) {
        this.selectedTab = this.tabs.get(u);
    }
    
    @Override
    public void onAdvancementsCleared() {
        this.tabs.clear();
        this.selectedTab = null;
    }
    
    @Nullable
    public AdvancementWidget getAdvancementWidget(final Advancement u) {
        final AdvancementTab dgl3 = this.getTab(u);
        return (dgl3 == null) ? null : dgl3.getWidget(u);
    }
    
    @Nullable
    private AdvancementTab getTab(Advancement u) {
        while (u.getParent() != null) {
            u = u.getParent();
        }
        return this.tabs.get(u);
    }
    
    static {
        WINDOW_LOCATION = new ResourceLocation("textures/gui/advancements/window.png");
        TABS_LOCATION = new ResourceLocation("textures/gui/advancements/tabs.png");
    }
}
