package net.minecraft.client.multiplayer;

import net.minecraft.client.Minecraft;
import net.minecraft.world.scores.PlayerTeam;
import com.google.common.base.MoreObjects;
import net.minecraft.client.resources.DefaultPlayerSkin;
import com.google.common.collect.Maps;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoPacket;
import net.minecraft.network.chat.Component;
import javax.annotation.Nullable;
import net.minecraft.world.level.GameType;
import net.minecraft.resources.ResourceLocation;
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
import java.util.Map;
import com.mojang.authlib.GameProfile;

public class PlayerInfo {
    private final GameProfile profile;
    private final Map<MinecraftProfileTexture.Type, ResourceLocation> textureLocations;
    private GameType gameMode;
    private int latency;
    private boolean pendingTextures;
    @Nullable
    private String skinModel;
    @Nullable
    private Component tabListDisplayName;
    private int lastHealth;
    private int displayHealth;
    private long lastHealthTime;
    private long healthBlinkTime;
    private long renderVisibilityId;
    
    public PlayerInfo(final ClientboundPlayerInfoPacket.PlayerUpdate b) {
        this.textureLocations = Maps.newEnumMap(MinecraftProfileTexture.Type.class);
        this.profile = b.getProfile();
        this.gameMode = b.getGameMode();
        this.latency = b.getLatency();
        this.tabListDisplayName = b.getDisplayName();
    }
    
    public GameProfile getProfile() {
        return this.profile;
    }
    
    @Nullable
    public GameType getGameMode() {
        return this.gameMode;
    }
    
    protected void setGameMode(final GameType bpy) {
        this.gameMode = bpy;
    }
    
    public int getLatency() {
        return this.latency;
    }
    
    protected void setLatency(final int integer) {
        this.latency = integer;
    }
    
    public boolean isSkinLoaded() {
        return this.getSkinLocation() != null;
    }
    
    public String getModelName() {
        if (this.skinModel == null) {
            return DefaultPlayerSkin.getSkinModelName(this.profile.getId());
        }
        return this.skinModel;
    }
    
    public ResourceLocation getSkinLocation() {
        this.registerTextures();
        return MoreObjects.<ResourceLocation>firstNonNull(this.textureLocations.get(MinecraftProfileTexture.Type.SKIN), DefaultPlayerSkin.getDefaultSkin(this.profile.getId()));
    }
    
    @Nullable
    public ResourceLocation getCapeLocation() {
        this.registerTextures();
        return this.textureLocations.get(MinecraftProfileTexture.Type.CAPE);
    }
    
    @Nullable
    public ResourceLocation getElytraLocation() {
        this.registerTextures();
        return this.textureLocations.get(MinecraftProfileTexture.Type.ELYTRA);
    }
    
    @Nullable
    public PlayerTeam getTeam() {
        return Minecraft.getInstance().level.getScoreboard().getPlayersTeam(this.getProfile().getName());
    }
    
    protected void registerTextures() {
        synchronized (this) {
            if (!this.pendingTextures) {
                this.pendingTextures = true;
                Minecraft.getInstance().getSkinManager().registerSkins(this.profile, (type, uh, minecraftProfileTexture) -> {
                    this.textureLocations.put(type, uh);
                    if (type == MinecraftProfileTexture.Type.SKIN) {
                        this.skinModel = minecraftProfileTexture.getMetadata("model");
                        if (this.skinModel == null) {
                            this.skinModel = "default";
                        }
                    }
                }, true);
            }
        }
    }
    
    public void setTabListDisplayName(@Nullable final Component mr) {
        this.tabListDisplayName = mr;
    }
    
    @Nullable
    public Component getTabListDisplayName() {
        return this.tabListDisplayName;
    }
    
    public int getLastHealth() {
        return this.lastHealth;
    }
    
    public void setLastHealth(final int integer) {
        this.lastHealth = integer;
    }
    
    public int getDisplayHealth() {
        return this.displayHealth;
    }
    
    public void setDisplayHealth(final int integer) {
        this.displayHealth = integer;
    }
    
    public long getLastHealthTime() {
        return this.lastHealthTime;
    }
    
    public void setLastHealthTime(final long long1) {
        this.lastHealthTime = long1;
    }
    
    public long getHealthBlinkTime() {
        return this.healthBlinkTime;
    }
    
    public void setHealthBlinkTime(final long long1) {
        this.healthBlinkTime = long1;
    }
    
    public long getRenderVisibilityId() {
        return this.renderVisibilityId;
    }
    
    public void setRenderVisibilityId(final long long1) {
        this.renderVisibilityId = long1;
    }
}
