package net.minecraft.client.gui.spectator.categories;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.util.Mth;
import java.util.Collection;
import net.minecraft.client.player.AbstractClientPlayer;
import java.util.Random;
import net.minecraft.client.resources.DefaultPlayerSkin;
import net.minecraft.client.multiplayer.PlayerInfo;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.client.gui.GuiComponent;
import net.minecraft.client.gui.components.spectator.SpectatorGui;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.gui.spectator.SpectatorMenu;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.network.chat.Component;
import java.util.Iterator;
import net.minecraft.world.scores.PlayerTeam;
import net.minecraft.client.Minecraft;
import com.google.common.collect.Lists;
import java.util.List;
import net.minecraft.client.gui.spectator.SpectatorMenuItem;
import net.minecraft.client.gui.spectator.SpectatorMenuCategory;

public class TeleportToTeamMenuCategory implements SpectatorMenuCategory, SpectatorMenuItem {
    private final List<SpectatorMenuItem> items;
    
    public TeleportToTeamMenuCategory() {
        this.items = Lists.newArrayList();
        final Minecraft dlx2 = Minecraft.getInstance();
        for (final PlayerTeam dfk4 : dlx2.level.getScoreboard().getPlayerTeams()) {
            this.items.add(new TeamSelectionItem(dfk4));
        }
    }
    
    @Override
    public List<SpectatorMenuItem> getItems() {
        return this.items;
    }
    
    @Override
    public Component getPrompt() {
        return new TranslatableComponent("spectatorMenu.team_teleport.prompt");
    }
    
    @Override
    public void selectItem(final SpectatorMenu duj) {
        duj.selectCategory(this);
    }
    
    @Override
    public Component getName() {
        return new TranslatableComponent("spectatorMenu.team_teleport");
    }
    
    @Override
    public void renderIcon(final PoseStack dhl, final float float2, final int integer) {
        Minecraft.getInstance().getTextureManager().bind(SpectatorGui.SPECTATOR_LOCATION);
        GuiComponent.blit(dhl, 0, 0, 16.0f, 0.0f, 16, 16, 256, 256);
    }
    
    @Override
    public boolean isEnabled() {
        for (final SpectatorMenuItem dul3 : this.items) {
            if (dul3.isEnabled()) {
                return true;
            }
        }
        return false;
    }
    
    class TeamSelectionItem implements SpectatorMenuItem {
        private final PlayerTeam team;
        private final ResourceLocation location;
        private final List<PlayerInfo> players;
        
        public TeamSelectionItem(final PlayerTeam dfk) {
            this.team = dfk;
            this.players = Lists.newArrayList();
            for (final String string5 : dfk.getPlayers()) {
                final PlayerInfo dyq6 = Minecraft.getInstance().getConnection().getPlayerInfo(string5);
                if (dyq6 != null) {
                    this.players.add(dyq6);
                }
            }
            if (this.players.isEmpty()) {
                this.location = DefaultPlayerSkin.getDefaultSkin();
            }
            else {
                final String string6 = this.players.get(new Random().nextInt(this.players.size())).getProfile().getName();
                AbstractClientPlayer.registerSkinTexture(this.location = AbstractClientPlayer.getSkinLocation(string6), string6);
            }
        }
        
        @Override
        public void selectItem(final SpectatorMenu duj) {
            duj.selectCategory(new TeleportToPlayerMenuCategory(this.players));
        }
        
        @Override
        public Component getName() {
            return this.team.getDisplayName();
        }
        
        @Override
        public void renderIcon(final PoseStack dhl, final float float2, final int integer) {
            final Integer integer2 = this.team.getColor().getColor();
            if (integer2 != null) {
                final float float3 = (integer2 >> 16 & 0xFF) / 255.0f;
                final float float4 = (integer2 >> 8 & 0xFF) / 255.0f;
                final float float5 = (integer2 & 0xFF) / 255.0f;
                GuiComponent.fill(dhl, 1, 1, 15, 15, Mth.color(float3 * float2, float4 * float2, float5 * float2) | integer << 24);
            }
            Minecraft.getInstance().getTextureManager().bind(this.location);
            RenderSystem.color4f(float2, float2, float2, integer / 255.0f);
            GuiComponent.blit(dhl, 2, 2, 12, 12, 8.0f, 8.0f, 8, 8, 64, 64);
            GuiComponent.blit(dhl, 2, 2, 12, 12, 40.0f, 8.0f, 8, 8, 64, 64);
        }
        
        @Override
        public boolean isEnabled() {
            return !this.players.isEmpty();
        }
    }
}
