package net.minecraft.client.gui.components;

import com.google.common.collect.ComparisonChain;
import java.util.Comparator;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.player.Player;
import com.mojang.authlib.GameProfile;
import java.util.Iterator;
import java.util.List;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.world.entity.player.PlayerModelPart;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.world.scores.criteria.ObjectiveCriteria;
import net.minecraft.network.chat.FormattedText;
import javax.annotation.Nullable;
import net.minecraft.world.scores.Objective;
import net.minecraft.world.scores.Scoreboard;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.Util;
import net.minecraft.ChatFormatting;
import net.minecraft.world.level.GameType;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.world.scores.Team;
import net.minecraft.world.scores.PlayerTeam;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.PlayerInfo;
import com.google.common.collect.Ordering;
import net.minecraft.client.gui.GuiComponent;

public class PlayerTabOverlay extends GuiComponent {
    private static final Ordering<PlayerInfo> PLAYER_ORDERING;
    private final Minecraft minecraft;
    private final Gui gui;
    private Component footer;
    private Component header;
    private long visibilityId;
    private boolean visible;
    
    public PlayerTabOverlay(final Minecraft dlx, final Gui dmu) {
        this.minecraft = dlx;
        this.gui = dmu;
    }
    
    public Component getNameForDisplay(final PlayerInfo dyq) {
        if (dyq.getTabListDisplayName() != null) {
            return this.decorateName(dyq, dyq.getTabListDisplayName().copy());
        }
        return this.decorateName(dyq, PlayerTeam.formatNameForTeam(dyq.getTeam(), new TextComponent(dyq.getProfile().getName())));
    }
    
    private Component decorateName(final PlayerInfo dyq, final MutableComponent mx) {
        return (dyq.getGameMode() == GameType.SPECTATOR) ? mx.withStyle(ChatFormatting.ITALIC) : mx;
    }
    
    public void setVisible(final boolean boolean1) {
        if (boolean1 && !this.visible) {
            this.visibilityId = Util.getMillis();
        }
        this.visible = boolean1;
    }
    
    public void render(final PoseStack dhl, final int integer, final Scoreboard dfm, @Nullable final Objective dfj) {
        final ClientPacketListener dyn6 = this.minecraft.player.connection;
        List<PlayerInfo> list7 = PlayerTabOverlay.PLAYER_ORDERING.<PlayerInfo>sortedCopy(dyn6.getOnlinePlayers());
        int integer2 = 0;
        int integer3 = 0;
        for (final PlayerInfo dyq11 : list7) {
            int integer4 = this.minecraft.font.width(this.getNameForDisplay(dyq11));
            integer2 = Math.max(integer2, integer4);
            if (dfj != null && dfj.getRenderType() != ObjectiveCriteria.RenderType.HEARTS) {
                integer4 = this.minecraft.font.width(" " + dfm.getOrCreatePlayerScore(dyq11.getProfile().getName(), dfj).getScore());
                integer3 = Math.max(integer3, integer4);
            }
        }
        list7 = list7.subList(0, Math.min(list7.size(), 80));
        int integer4;
        int integer6;
        int integer5;
        for (integer5 = (integer6 = list7.size()), integer4 = 1; integer6 > 20; integer6 = (integer5 + integer4 - 1) / integer4) {
            ++integer4;
        }
        final boolean boolean13 = this.minecraft.isLocalServer() || this.minecraft.getConnection().getConnection().isEncrypted();
        int integer7;
        if (dfj != null) {
            if (dfj.getRenderType() == ObjectiveCriteria.RenderType.HEARTS) {
                integer7 = 90;
            }
            else {
                integer7 = integer3;
            }
        }
        else {
            integer7 = 0;
        }
        final int integer8 = Math.min(integer4 * ((boolean13 ? 9 : 0) + integer2 + integer7 + 13), integer - 50) / integer4;
        final int integer9 = integer / 2 - (integer8 * integer4 + (integer4 - 1) * 5) / 2;
        int integer10 = 10;
        int integer11 = integer8 * integer4 + (integer4 - 1) * 5;
        List<FormattedText> list8 = null;
        if (this.header != null) {
            list8 = this.minecraft.font.split(this.header, integer - 50);
            for (final FormattedText mu21 : list8) {
                integer11 = Math.max(integer11, this.minecraft.font.width(mu21));
            }
        }
        List<FormattedText> list9 = null;
        if (this.footer != null) {
            list9 = this.minecraft.font.split(this.footer, integer - 50);
            for (final FormattedText mu22 : list9) {
                integer11 = Math.max(integer11, this.minecraft.font.width(mu22));
            }
        }
        if (list8 != null) {
            final int integer24 = integer / 2 - integer11 / 2 - 1;
            final int integer25 = integer10 - 1;
            final int integer26 = integer / 2 + integer11 / 2 + 1;
            final int n = integer10;
            final int size = list8.size();
            this.minecraft.font.getClass();
            GuiComponent.fill(dhl, integer24, integer25, integer26, n + size * 9, Integer.MIN_VALUE);
            for (final FormattedText mu22 : list8) {
                final int integer12 = this.minecraft.font.width(mu22);
                this.minecraft.font.drawShadow(dhl, mu22, (float)(integer / 2 - integer12 / 2), (float)integer10, -1);
                final int n2 = integer10;
                this.minecraft.font.getClass();
                integer10 = n2 + 9;
            }
            ++integer10;
        }
        GuiComponent.fill(dhl, integer / 2 - integer11 / 2 - 1, integer10 - 1, integer / 2 + integer11 / 2 + 1, integer10 + integer6 * 9, Integer.MIN_VALUE);
        final int integer13 = this.minecraft.options.getBackgroundColor(553648127);
        for (int integer14 = 0; integer14 < integer5; ++integer14) {
            final int integer12 = integer14 / integer6;
            final int integer15 = integer14 % integer6;
            int integer16 = integer9 + integer12 * integer8 + integer12 * 5;
            final int integer17 = integer10 + integer15 * 9;
            GuiComponent.fill(dhl, integer16, integer17, integer16 + integer8, integer17 + 8, integer13);
            RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
            RenderSystem.enableAlphaTest();
            RenderSystem.enableBlend();
            RenderSystem.defaultBlendFunc();
            if (integer14 < list7.size()) {
                final PlayerInfo dyq12 = list7.get(integer14);
                final GameProfile gameProfile28 = dyq12.getProfile();
                if (boolean13) {
                    final Player bec29 = this.minecraft.level.getPlayerByUUID(gameProfile28.getId());
                    final boolean boolean14 = bec29 != null && bec29.isModelPartShown(PlayerModelPart.CAPE) && ("Dinnerbone".equals(gameProfile28.getName()) || "Grumm".equals(gameProfile28.getName()));
                    this.minecraft.getTextureManager().bind(dyq12.getSkinLocation());
                    final int integer18 = 8 + (boolean14 ? 8 : 0);
                    final int integer19 = 8 * (boolean14 ? -1 : 1);
                    GuiComponent.blit(dhl, integer16, integer17, 8, 8, 8.0f, (float)integer18, 8, integer19, 64, 64);
                    if (bec29 != null && bec29.isModelPartShown(PlayerModelPart.HAT)) {
                        final int integer20 = 8 + (boolean14 ? 8 : 0);
                        final int integer21 = 8 * (boolean14 ? -1 : 1);
                        GuiComponent.blit(dhl, integer16, integer17, 8, 8, 40.0f, (float)integer20, 8, integer21, 64, 64);
                    }
                    integer16 += 9;
                }
                this.minecraft.font.drawShadow(dhl, this.getNameForDisplay(dyq12), (float)integer16, (float)integer17, (dyq12.getGameMode() == GameType.SPECTATOR) ? -1862270977 : -1);
                if (dfj != null && dyq12.getGameMode() != GameType.SPECTATOR) {
                    final int integer22 = integer16 + integer2 + 1;
                    final int integer23 = integer22 + integer7;
                    if (integer23 - integer22 > 5) {
                        this.renderTablistScore(dfj, integer17, gameProfile28.getName(), integer22, integer23, dyq12, dhl);
                    }
                }
                this.renderPingIcon(dhl, integer8, integer16 - (boolean13 ? 9 : 0), integer17, dyq12);
            }
        }
        if (list9 != null) {
            integer10 += integer6 * 9 + 1;
            final int integer27 = integer / 2 - integer11 / 2 - 1;
            final int integer28 = integer10 - 1;
            final int integer29 = integer / 2 + integer11 / 2 + 1;
            final int n3 = integer10;
            final int size2 = list9.size();
            this.minecraft.font.getClass();
            GuiComponent.fill(dhl, integer27, integer28, integer29, n3 + size2 * 9, Integer.MIN_VALUE);
            for (final FormattedText mu23 : list9) {
                final int integer15 = this.minecraft.font.width(mu23);
                this.minecraft.font.drawShadow(dhl, mu23, (float)(integer / 2 - integer15 / 2), (float)integer10, -1);
                final int n4 = integer10;
                this.minecraft.font.getClass();
                integer10 = n4 + 9;
            }
        }
    }
    
    protected void renderPingIcon(final PoseStack dhl, final int integer2, final int integer3, final int integer4, final PlayerInfo dyq) {
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        this.minecraft.getTextureManager().bind(PlayerTabOverlay.GUI_ICONS_LOCATION);
        final int integer5 = 0;
        int integer6;
        if (dyq.getLatency() < 0) {
            integer6 = 5;
        }
        else if (dyq.getLatency() < 150) {
            integer6 = 0;
        }
        else if (dyq.getLatency() < 300) {
            integer6 = 1;
        }
        else if (dyq.getLatency() < 600) {
            integer6 = 2;
        }
        else if (dyq.getLatency() < 1000) {
            integer6 = 3;
        }
        else {
            integer6 = 4;
        }
        this.setBlitOffset(this.getBlitOffset() + 100);
        this.blit(dhl, integer3 + integer2 - 11, integer4, 0, 176 + integer6 * 8, 10, 8);
        this.setBlitOffset(this.getBlitOffset() - 100);
    }
    
    private void renderTablistScore(final Objective dfj, final int integer2, final String string, final int integer4, final int integer5, final PlayerInfo dyq, final PoseStack dhl) {
        final int integer6 = dfj.getScoreboard().getOrCreatePlayerScore(string, dfj).getScore();
        if (dfj.getRenderType() == ObjectiveCriteria.RenderType.HEARTS) {
            this.minecraft.getTextureManager().bind(PlayerTabOverlay.GUI_ICONS_LOCATION);
            final long long10 = Util.getMillis();
            if (this.visibilityId == dyq.getRenderVisibilityId()) {
                if (integer6 < dyq.getLastHealth()) {
                    dyq.setLastHealthTime(long10);
                    dyq.setHealthBlinkTime(this.gui.getGuiTicks() + 20);
                }
                else if (integer6 > dyq.getLastHealth()) {
                    dyq.setLastHealthTime(long10);
                    dyq.setHealthBlinkTime(this.gui.getGuiTicks() + 10);
                }
            }
            if (long10 - dyq.getLastHealthTime() > 1000L || this.visibilityId != dyq.getRenderVisibilityId()) {
                dyq.setLastHealth(integer6);
                dyq.setDisplayHealth(integer6);
                dyq.setLastHealthTime(long10);
            }
            dyq.setRenderVisibilityId(this.visibilityId);
            dyq.setLastHealth(integer6);
            final int integer7 = Mth.ceil(Math.max(integer6, dyq.getDisplayHealth()) / 2.0f);
            final int integer8 = Math.max(Mth.ceil((float)(integer6 / 2)), Math.max(Mth.ceil((float)(dyq.getDisplayHealth() / 2)), 10));
            final boolean boolean14 = dyq.getHealthBlinkTime() > this.gui.getGuiTicks() && (dyq.getHealthBlinkTime() - this.gui.getGuiTicks()) / 3L % 2L == 1L;
            if (integer7 > 0) {
                final int integer9 = Mth.floor(Math.min((integer5 - integer4 - 4) / (float)integer8, 9.0f));
                if (integer9 > 3) {
                    for (int integer10 = integer7; integer10 < integer8; ++integer10) {
                        this.blit(dhl, integer4 + integer10 * integer9, integer2, boolean14 ? 25 : 16, 0, 9, 9);
                    }
                    for (int integer10 = 0; integer10 < integer7; ++integer10) {
                        this.blit(dhl, integer4 + integer10 * integer9, integer2, boolean14 ? 25 : 16, 0, 9, 9);
                        if (boolean14) {
                            if (integer10 * 2 + 1 < dyq.getDisplayHealth()) {
                                this.blit(dhl, integer4 + integer10 * integer9, integer2, 70, 0, 9, 9);
                            }
                            if (integer10 * 2 + 1 == dyq.getDisplayHealth()) {
                                this.blit(dhl, integer4 + integer10 * integer9, integer2, 79, 0, 9, 9);
                            }
                        }
                        if (integer10 * 2 + 1 < integer6) {
                            this.blit(dhl, integer4 + integer10 * integer9, integer2, (integer10 >= 10) ? 160 : 52, 0, 9, 9);
                        }
                        if (integer10 * 2 + 1 == integer6) {
                            this.blit(dhl, integer4 + integer10 * integer9, integer2, (integer10 >= 10) ? 169 : 61, 0, 9, 9);
                        }
                    }
                }
                else {
                    final float float16 = Mth.clamp(integer6 / 20.0f, 0.0f, 1.0f);
                    final int integer11 = (int)((1.0f - float16) * 255.0f) << 16 | (int)(float16 * 255.0f) << 8;
                    String string2 = "" + integer6 / 2.0f;
                    if (integer5 - this.minecraft.font.width(string2 + "hp") >= integer4) {
                        string2 += "hp";
                    }
                    this.minecraft.font.drawShadow(dhl, string2, (float)((integer5 + integer4) / 2 - this.minecraft.font.width(string2) / 2), (float)integer2, integer11);
                }
            }
        }
        else {
            final String string3 = ChatFormatting.YELLOW + "" + integer6;
            this.minecraft.font.drawShadow(dhl, string3, (float)(integer5 - this.minecraft.font.width(string3)), (float)integer2, 16777215);
        }
    }
    
    public void setFooter(@Nullable final Component mr) {
        this.footer = mr;
    }
    
    public void setHeader(@Nullable final Component mr) {
        this.header = mr;
    }
    
    public void reset() {
        this.header = null;
        this.footer = null;
    }
    
    static {
        PLAYER_ORDERING = Ordering.<PlayerInfo>from((Comparator<PlayerInfo>)new PlayerInfoComparator());
    }
    
    static class PlayerInfoComparator implements Comparator<PlayerInfo> {
        private PlayerInfoComparator() {
        }
        
        @Override
        public int compare(final PlayerInfo dyq1, final PlayerInfo dyq2) {
            final PlayerTeam dfk4 = dyq1.getTeam();
            final PlayerTeam dfk5 = dyq2.getTeam();
            return ComparisonChain.start().compareTrueFirst(dyq1.getGameMode() != GameType.SPECTATOR, dyq2.getGameMode() != GameType.SPECTATOR).compare((dfk4 != null) ? dfk4.getName() : "", (dfk5 != null) ? dfk5.getName() : "").<String>compare(dyq1.getProfile().getName(), dyq2.getProfile().getName(), String::compareToIgnoreCase).result();
        }
    }
}
