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

import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.BlockItem;
import net.minecraft.client.resources.sounds.SoundInstance;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.sounds.SoundEvents;
import com.mojang.blaze3d.vertex.Tesselator;
import java.util.Set;
import net.minecraft.world.item.Items;
import net.minecraft.core.Registry;
import com.google.common.collect.Sets;
import com.google.common.collect.Lists;
import net.minecraft.world.level.block.Block;
import net.minecraft.stats.StatType;
import java.util.List;
import net.minecraft.ChatFormatting;
import it.unimi.dsi.fastutil.objects.ObjectListIterator;
import java.util.Comparator;
import java.util.Iterator;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.stats.Stats;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiComponent;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.world.item.Item;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.stats.Stat;
import net.minecraft.client.gui.Font;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.Util;
import net.minecraft.client.resources.language.I18n;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.client.gui.components.Button;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ServerboundClientCommandPacket;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import javax.annotation.Nullable;
import net.minecraft.client.gui.components.ObjectSelectionList;
import net.minecraft.stats.StatsCounter;
import net.minecraft.client.gui.screens.Screen;

public class StatsScreen extends Screen implements StatsUpdateListener {
    protected final Screen lastScreen;
    private GeneralStatisticsList statsList;
    private ItemStatisticsList itemStatsList;
    private MobsStatisticsList mobsStatsList;
    private final StatsCounter stats;
    @Nullable
    private ObjectSelectionList<?> activeList;
    private boolean isLoading;
    
    public StatsScreen(final Screen dqs, final StatsCounter acv) {
        super(new TranslatableComponent("gui.stats"));
        this.isLoading = true;
        this.lastScreen = dqs;
        this.stats = acv;
    }
    
    @Override
    protected void init() {
        this.isLoading = true;
        this.minecraft.getConnection().send(new ServerboundClientCommandPacket(ServerboundClientCommandPacket.Action.REQUEST_STATS));
    }
    
    public void initLists() {
        this.statsList = new GeneralStatisticsList(this.minecraft);
        this.itemStatsList = new ItemStatisticsList(this.minecraft);
        this.mobsStatsList = new MobsStatisticsList(this.minecraft);
    }
    
    public void initButtons() {
        this.<Button>addButton(new Button(this.width / 2 - 120, this.height - 52, 80, 20, new TranslatableComponent("stat.generalButton"), dni -> this.setActiveList(this.statsList)));
        final Button dni2 = this.<Button>addButton(new Button(this.width / 2 - 40, this.height - 52, 80, 20, new TranslatableComponent("stat.itemsButton"), dni -> this.setActiveList(this.itemStatsList)));
        final Button dni3 = this.<Button>addButton(new Button(this.width / 2 + 40, this.height - 52, 80, 20, new TranslatableComponent("stat.mobsButton"), dni -> this.setActiveList(this.mobsStatsList)));
        this.<Button>addButton(new Button(this.width / 2 - 100, this.height - 28, 200, 20, CommonComponents.GUI_DONE, dni -> this.minecraft.setScreen(this.lastScreen)));
        if (this.itemStatsList.children().isEmpty()) {
            dni2.active = false;
        }
        if (this.mobsStatsList.children().isEmpty()) {
            dni3.active = false;
        }
    }
    
    @Override
    public void render(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
        if (this.isLoading) {
            this.renderBackground(dhl);
            this.drawCenteredString(dhl, this.font, I18n.get("multiplayer.downloadingStats"), this.width / 2, this.height / 2, 16777215);
            final Font font = this.font;
            final String string = StatsScreen.LOADING_SYMBOLS[(int)(Util.getMillis() / 150L % StatsScreen.LOADING_SYMBOLS.length)];
            final int integer4 = this.width / 2;
            final int n = this.height / 2;
            this.font.getClass();
            this.drawCenteredString(dhl, font, string, integer4, n + 9 * 2, 16777215);
        }
        else {
            this.getActiveList().render(dhl, integer2, integer3, float4);
            this.drawCenteredString(dhl, this.font, this.title, this.width / 2, 20, 16777215);
            super.render(dhl, integer2, integer3, float4);
        }
    }
    
    @Override
    public void onStatsUpdated() {
        if (this.isLoading) {
            this.initLists();
            this.initButtons();
            this.setActiveList(this.statsList);
            this.isLoading = false;
        }
    }
    
    @Override
    public boolean isPauseScreen() {
        return !this.isLoading;
    }
    
    @Nullable
    public ObjectSelectionList<?> getActiveList() {
        return this.activeList;
    }
    
    public void setActiveList(@Nullable final ObjectSelectionList<?> dnt) {
        this.children.remove(this.statsList);
        this.children.remove(this.itemStatsList);
        this.children.remove(this.mobsStatsList);
        if (dnt != null) {
            this.children.add(0, dnt);
            this.activeList = dnt;
        }
    }
    
    private static String getTranslationKey(final Stat<ResourceLocation> acr) {
        return "stat." + acr.getValue().toString().replace(':', '.');
    }
    
    private int getColumnX(final int integer) {
        return 115 + 40 * integer;
    }
    
    private void blitSlot(final PoseStack dhl, final int integer2, final int integer3, final Item bke) {
        this.blitSlotIcon(dhl, integer2 + 1, integer3 + 1, 0, 0);
        RenderSystem.enableRescaleNormal();
        this.itemRenderer.renderGuiItem(bke.getDefaultInstance(), integer2 + 2, integer3 + 2);
        RenderSystem.disableRescaleNormal();
    }
    
    private void blitSlotIcon(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);
        this.minecraft.getTextureManager().bind(StatsScreen.STATS_ICON_LOCATION);
        GuiComponent.blit(dhl, integer2, integer3, this.getBlitOffset(), (float)integer4, (float)integer5, 18, 18, 128, 128);
    }
    
    class GeneralStatisticsList extends ObjectSelectionList<Entry> {
        public GeneralStatisticsList(final Minecraft dlx) {
            super(dlx, StatsScreen.this.width, StatsScreen.this.height, 32, StatsScreen.this.height - 64, 10);
            final ObjectArrayList<Stat<ResourceLocation>> objectArrayList4 = (ObjectArrayList<Stat<ResourceLocation>>)new ObjectArrayList((Iterator)Stats.CUSTOM.iterator());
            objectArrayList4.sort((Comparator)Comparator.<Object, Comparable>comparing(acr -> I18n.get(getTranslationKey(acr))));
            for (final Stat<ResourceLocation> acr2 : objectArrayList4) {
                this.addEntry(new Entry((Stat)acr2));
            }
        }
        
        @Override
        protected void renderBackground(final PoseStack dhl) {
            StatsScreen.this.renderBackground(dhl);
        }
        
        class Entry extends ObjectSelectionList.Entry<Entry> {
            private final Stat<ResourceLocation> stat;
            
            private Entry(final Stat<ResourceLocation> acr) {
                this.stat = acr;
            }
            
            @Override
            public void render(final PoseStack dhl, final int integer2, final int integer3, final int integer4, final int integer5, final int integer6, final int integer7, final int integer8, final boolean boolean9, final float float10) {
                final Component mr12 = new TranslatableComponent(getTranslationKey(this.stat)).withStyle(ChatFormatting.GRAY);
                GeneralStatisticsList.this.drawString(dhl, StatsScreen.this.font, mr12.getString(), integer4 + 2, integer3 + 1, (integer2 % 2 == 0) ? 16777215 : 9474192);
                final String string13 = this.stat.format(StatsScreen.this.stats.getValue(this.stat));
                GeneralStatisticsList.this.drawString(dhl, StatsScreen.this.font, string13, integer4 + 2 + 213 - StatsScreen.this.font.width(string13), integer3 + 1, (integer2 % 2 == 0) ? 16777215 : 9474192);
            }
        }
    }
    
    class ItemStatisticsList extends ObjectSelectionList<ItemRow> {
        protected final List<StatType<Block>> blockColumns;
        protected final List<StatType<Item>> itemColumns;
        private final int[] iconOffsets;
        protected int headerPressed;
        protected final List<Item> statItemList;
        protected final Comparator<Item> itemStatSorter;
        @Nullable
        protected StatType<?> sortColumn;
        protected int sortOrder;
        
        public ItemStatisticsList(final Minecraft dlx) {
            super(dlx, StatsScreen.this.width, StatsScreen.this.height, 32, StatsScreen.this.height - 64, 20);
            this.iconOffsets = new int[] { 3, 4, 1, 2, 5, 6 };
            this.headerPressed = -1;
            this.itemStatSorter = new ItemComparator();
            (this.blockColumns = Lists.newArrayList()).add(Stats.BLOCK_MINED);
            this.itemColumns = Lists.newArrayList(Stats.ITEM_BROKEN, Stats.ITEM_CRAFTED, Stats.ITEM_USED, Stats.ITEM_PICKED_UP, Stats.ITEM_DROPPED);
            this.setRenderHeader(true, 20);
            final Set<Item> set4 = Sets.<Item>newIdentityHashSet();
            for (final Item bke6 : Registry.ITEM) {
                boolean boolean7 = false;
                for (final StatType<Item> act9 : this.itemColumns) {
                    if (act9.contains(bke6) && StatsScreen.this.stats.getValue(act9.get(bke6)) > 0) {
                        boolean7 = true;
                    }
                }
                if (boolean7) {
                    set4.add(bke6);
                }
            }
            for (final Block bvr6 : Registry.BLOCK) {
                boolean boolean7 = false;
                for (final StatType<Block> act10 : this.blockColumns) {
                    if (act10.contains(bvr6) && StatsScreen.this.stats.getValue(act10.get(bvr6)) > 0) {
                        boolean7 = true;
                    }
                }
                if (boolean7) {
                    set4.add(bvr6.asItem());
                }
            }
            set4.remove(Items.AIR);
            this.statItemList = Lists.newArrayList(set4);
            for (int integer5 = 0; integer5 < this.statItemList.size(); ++integer5) {
                this.addEntry(new ItemRow());
            }
        }
        
        @Override
        protected void renderHeader(final PoseStack dhl, final int integer2, final int integer3, final Tesselator dhn) {
            if (!this.minecraft.mouseHandler.isLeftPressed()) {
                this.headerPressed = -1;
            }
            for (int integer4 = 0; integer4 < this.iconOffsets.length; ++integer4) {
                StatsScreen.this.blitSlotIcon(dhl, integer2 + StatsScreen.this.getColumnX(integer4) - 18, integer3 + 1, 0, (this.headerPressed == integer4) ? 0 : 18);
            }
            if (this.sortColumn != null) {
                final int integer4 = StatsScreen.this.getColumnX(this.getColumnIndex(this.sortColumn)) - 36;
                final int integer5 = (this.sortOrder == 1) ? 2 : 1;
                StatsScreen.this.blitSlotIcon(dhl, integer2 + integer4, integer3 + 1, 18 * integer5, 0);
            }
            for (int integer4 = 0; integer4 < this.iconOffsets.length; ++integer4) {
                final int integer5 = (this.headerPressed == integer4) ? 1 : 0;
                StatsScreen.this.blitSlotIcon(dhl, integer2 + StatsScreen.this.getColumnX(integer4) - 18 + integer5, integer3 + 1 + integer5, 18 * this.iconOffsets[integer4], 18);
            }
        }
        
        @Override
        public int getRowWidth() {
            return 375;
        }
        
        @Override
        protected int getScrollbarPosition() {
            return this.width / 2 + 140;
        }
        
        @Override
        protected void renderBackground(final PoseStack dhl) {
            StatsScreen.this.renderBackground(dhl);
        }
        
        @Override
        protected void clickedHeader(final int integer1, final int integer2) {
            this.headerPressed = -1;
            for (int integer3 = 0; integer3 < this.iconOffsets.length; ++integer3) {
                final int integer4 = integer1 - StatsScreen.this.getColumnX(integer3);
                if (integer4 >= -36 && integer4 <= 0) {
                    this.headerPressed = integer3;
                    break;
                }
            }
            if (this.headerPressed >= 0) {
                this.sortByColumn(this.getColumn(this.headerPressed));
                this.minecraft.getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0f));
            }
        }
        
        private StatType<?> getColumn(final int integer) {
            return ((integer < this.blockColumns.size()) ? ((StatType<Block>)this.blockColumns.get(integer)) : ((StatType<Item>)this.itemColumns.get(integer - this.blockColumns.size())));
        }
        
        private int getColumnIndex(final StatType<?> act) {
            final int integer3 = this.blockColumns.indexOf(act);
            if (integer3 >= 0) {
                return integer3;
            }
            final int integer4 = this.itemColumns.indexOf(act);
            if (integer4 >= 0) {
                return integer4 + this.blockColumns.size();
            }
            return -1;
        }
        
        @Override
        protected void renderDecorations(final PoseStack dhl, final int integer2, final int integer3) {
            if (integer3 < this.y0 || integer3 > this.y1) {
                return;
            }
            final ItemRow b5 = this.getEntryAtPosition(integer2, integer3);
            final int integer4 = (this.width - this.getRowWidth()) / 2;
            if (b5 != null) {
                if (integer2 < integer4 + 40 || integer2 > integer4 + 40 + 20) {
                    return;
                }
                final Item bke7 = this.statItemList.get(this.children().indexOf(b5));
                this.renderMousehoverTooltip(dhl, this.getString(bke7), integer2, integer3);
            }
            else {
                Component mr7 = null;
                final int integer5 = integer2 - integer4;
                for (int integer6 = 0; integer6 < this.iconOffsets.length; ++integer6) {
                    final int integer7 = StatsScreen.this.getColumnX(integer6);
                    if (integer5 >= integer7 - 18 && integer5 <= integer7) {
                        mr7 = new TranslatableComponent(this.getColumn(integer6).getTranslationKey());
                        break;
                    }
                }
                this.renderMousehoverTooltip(dhl, mr7, integer2, integer3);
            }
        }
        
        protected void renderMousehoverTooltip(final PoseStack dhl, @Nullable final Component mr, final int integer3, final int integer4) {
            if (mr == null) {
                return;
            }
            final int integer5 = integer3 + 12;
            final int integer6 = integer4 - 12;
            final int integer7 = StatsScreen.this.font.width(mr);
            this.fillGradient(dhl, integer5 - 3, integer6 - 3, integer5 + integer7 + 3, integer6 + 8 + 3, -1073741824, -1073741824);
            RenderSystem.pushMatrix();
            RenderSystem.translatef(0.0f, 0.0f, 400.0f);
            StatsScreen.this.font.drawShadow(dhl, mr, (float)integer5, (float)integer6, -1);
            RenderSystem.popMatrix();
        }
        
        protected Component getString(final Item bke) {
            return bke.getDescription();
        }
        
        protected void sortByColumn(final StatType<?> act) {
            if (act != this.sortColumn) {
                this.sortColumn = act;
                this.sortOrder = -1;
            }
            else if (this.sortOrder == -1) {
                this.sortOrder = 1;
            }
            else {
                this.sortColumn = null;
                this.sortOrder = 0;
            }
            this.statItemList.sort(this.itemStatSorter);
        }
        
        class ItemComparator implements Comparator<Item> {
            private ItemComparator() {
            }
            
            @Override
            public int compare(final Item bke1, final Item bke2) {
                int integer4;
                int integer5;
                if (ItemStatisticsList.this.sortColumn == null) {
                    integer4 = 0;
                    integer5 = 0;
                }
                else if (ItemStatisticsList.this.blockColumns.contains(ItemStatisticsList.this.sortColumn)) {
                    final StatType<Block> act6 = (StatType<Block>)ItemStatisticsList.this.sortColumn;
                    integer4 = ((bke1 instanceof BlockItem) ? StatsScreen.this.stats.<Block>getValue(act6, ((BlockItem)bke1).getBlock()) : -1);
                    integer5 = ((bke2 instanceof BlockItem) ? StatsScreen.this.stats.<Block>getValue(act6, ((BlockItem)bke2).getBlock()) : -1);
                }
                else {
                    final StatType<Item> act7 = (StatType<Item>)ItemStatisticsList.this.sortColumn;
                    integer4 = StatsScreen.this.stats.<Item>getValue(act7, bke1);
                    integer5 = StatsScreen.this.stats.<Item>getValue(act7, bke2);
                }
                if (integer4 == integer5) {
                    return ItemStatisticsList.this.sortOrder * Integer.compare(Item.getId(bke1), Item.getId(bke2));
                }
                return ItemStatisticsList.this.sortOrder * Integer.compare(integer4, integer5);
            }
        }
        
        class ItemRow extends Entry<ItemRow> {
            private ItemRow() {
            }
            
            @Override
            public void render(final PoseStack dhl, final int integer2, final int integer3, final int integer4, final int integer5, final int integer6, final int integer7, final int integer8, final boolean boolean9, final float float10) {
                final Item bke12 = StatsScreen.this.itemStatsList.statItemList.get(integer2);
                StatsScreen.this.blitSlot(dhl, integer4 + 40, integer3, bke12);
                for (int integer9 = 0; integer9 < StatsScreen.this.itemStatsList.blockColumns.size(); ++integer9) {
                    Stat<Block> acr14;
                    if (bke12 instanceof BlockItem) {
                        acr14 = StatsScreen.this.itemStatsList.blockColumns.get(integer9).get(((BlockItem)bke12).getBlock());
                    }
                    else {
                        acr14 = null;
                    }
                    this.renderStat(dhl, acr14, integer4 + StatsScreen.this.getColumnX(integer9), integer3, integer2 % 2 == 0);
                }
                for (int integer9 = 0; integer9 < StatsScreen.this.itemStatsList.itemColumns.size(); ++integer9) {
                    this.renderStat(dhl, StatsScreen.this.itemStatsList.itemColumns.get(integer9).get(bke12), integer4 + StatsScreen.this.getColumnX(integer9 + StatsScreen.this.itemStatsList.blockColumns.size()), integer3, integer2 % 2 == 0);
                }
            }
            
            protected void renderStat(final PoseStack dhl, @Nullable final Stat<?> acr, final int integer3, final int integer4, final boolean boolean5) {
                final String string7 = (acr == null) ? "-" : acr.format(StatsScreen.this.stats.getValue(acr));
                ItemStatisticsList.this.drawString(dhl, StatsScreen.this.font, string7, integer3 - StatsScreen.this.font.width(string7), integer4 + 5, boolean5 ? 16777215 : 9474192);
            }
        }
    }
    
    class MobsStatisticsList extends ObjectSelectionList<MobRow> {
        public MobsStatisticsList(final Minecraft dlx) {
            final int width = StatsScreen.this.width;
            final int height = StatsScreen.this.height;
            final int integer4 = 32;
            final int integer5 = StatsScreen.this.height - 64;
            StatsScreen.this.font.getClass();
            super(dlx, width, height, integer4, integer5, 9 * 4);
            for (final EntityType<?> aoq5 : Registry.ENTITY_TYPE) {
                if (StatsScreen.this.stats.getValue(Stats.ENTITY_KILLED.get(aoq5)) > 0 || StatsScreen.this.stats.getValue(Stats.ENTITY_KILLED_BY.get(aoq5)) > 0) {
                    this.addEntry(new MobRow(aoq5));
                }
            }
        }
        
        @Override
        protected void renderBackground(final PoseStack dhl) {
            StatsScreen.this.renderBackground(dhl);
        }
        
        class MobRow extends Entry<MobRow> {
            private final EntityType<?> type;
            
            public MobRow(final EntityType<?> aoq) {
                this.type = aoq;
            }
            
            @Override
            public void render(final PoseStack dhl, final int integer2, final int integer3, final int integer4, final int integer5, final int integer6, final int integer7, final int integer8, final boolean boolean9, final float float10) {
                final String string12 = I18n.get(Util.makeDescriptionId("entity", EntityType.getKey(this.type)));
                final int integer9 = StatsScreen.this.stats.getValue(Stats.ENTITY_KILLED.get(this.type));
                final int integer10 = StatsScreen.this.stats.getValue(Stats.ENTITY_KILLED_BY.get(this.type));
                MobsStatisticsList.this.drawString(dhl, StatsScreen.this.font, string12, integer4 + 2, integer3 + 1, 16777215);
                final MobsStatisticsList this$1 = MobsStatisticsList.this;
                final Font access$1800 = StatsScreen.this.font;
                final String killsMessage = this.killsMessage(string12, integer9);
                final int integer11 = integer4 + 2 + 10;
                final int n = integer3 + 1;
                StatsScreen.this.font.getClass();
                this$1.drawString(dhl, access$1800, killsMessage, integer11, n + 9, (integer9 == 0) ? 6316128 : 9474192);
                final MobsStatisticsList this$2 = MobsStatisticsList.this;
                final Font access$1801 = StatsScreen.this.font;
                final String killedByMessage = this.killedByMessage(string12, integer10);
                final int integer12 = integer4 + 2 + 10;
                final int n2 = integer3 + 1;
                StatsScreen.this.font.getClass();
                this$2.drawString(dhl, access$1801, killedByMessage, integer12, n2 + 9 * 2, (integer10 == 0) ? 6316128 : 9474192);
            }
            
            private String killsMessage(final String string, final int integer) {
                final String string2 = Stats.ENTITY_KILLED.getTranslationKey();
                if (integer == 0) {
                    return I18n.get(string2 + ".none", string);
                }
                return I18n.get(string2, integer, string);
            }
            
            private String killedByMessage(final String string, final int integer) {
                final String string2 = Stats.ENTITY_KILLED_BY.getTranslationKey();
                if (integer == 0) {
                    return I18n.get(string2 + ".none", string);
                }
                return I18n.get(string2, string, integer);
            }
        }
    }
}
