package net.minecraft.network.protocol.game;

import javax.annotation.Nullable;
import net.minecraft.network.PacketListener;
import com.google.common.base.MoreObjects;
import java.io.IOException;
import net.minecraft.network.chat.Component;
import net.minecraft.world.level.GameType;
import com.mojang.authlib.properties.Property;
import com.mojang.authlib.GameProfile;
import net.minecraft.network.FriendlyByteBuf;
import java.util.Iterator;
import net.minecraft.server.level.ServerPlayer;
import com.google.common.collect.Lists;
import java.util.List;
import net.minecraft.network.protocol.Packet;

public class ClientboundPlayerInfoPacket implements Packet<ClientGamePacketListener> {
    private Action action;
    private final List<PlayerUpdate> entries;
    
    public ClientboundPlayerInfoPacket() {
        this.entries = Lists.newArrayList();
    }
    
    public ClientboundPlayerInfoPacket(final Action a, final ServerPlayer... arr) {
        this.entries = Lists.newArrayList();
        this.action = a;
        for (final ServerPlayer xe7 : arr) {
            this.entries.add(new PlayerUpdate(xe7.getGameProfile(), xe7.latency, xe7.gameMode.getGameModeForPlayer(), xe7.getTabListDisplayName()));
        }
    }
    
    public ClientboundPlayerInfoPacket(final Action a, final Iterable<ServerPlayer> iterable) {
        this.entries = Lists.newArrayList();
        this.action = a;
        for (final ServerPlayer xe5 : iterable) {
            this.entries.add(new PlayerUpdate(xe5.getGameProfile(), xe5.latency, xe5.gameMode.getGameModeForPlayer(), xe5.getTabListDisplayName()));
        }
    }
    
    @Override
    public void read(final FriendlyByteBuf kv) throws IOException {
        this.action = kv.<Action>readEnum(Action.class);
        for (int integer3 = kv.readVarInt(), integer4 = 0; integer4 < integer3; ++integer4) {
            GameProfile gameProfile5 = null;
            int integer5 = 0;
            GameType bjt7 = null;
            Component lf8 = null;
            switch (this.action) {
                case ADD_PLAYER: {
                    gameProfile5 = new GameProfile(kv.readUUID(), kv.readUtf(16));
                    for (int integer6 = kv.readVarInt(), integer7 = 0; integer7 < integer6; ++integer7) {
                        final String string11 = kv.readUtf(32767);
                        final String string12 = kv.readUtf(32767);
                        if (kv.readBoolean()) {
                            gameProfile5.getProperties().put(string11, new Property(string11, string12, kv.readUtf(32767)));
                        }
                        else {
                            gameProfile5.getProperties().put(string11, new Property(string11, string12));
                        }
                    }
                    bjt7 = GameType.byId(kv.readVarInt());
                    integer5 = kv.readVarInt();
                    if (kv.readBoolean()) {
                        lf8 = kv.readComponent();
                        break;
                    }
                    break;
                }
                case UPDATE_GAME_MODE: {
                    gameProfile5 = new GameProfile(kv.readUUID(), (String)null);
                    bjt7 = GameType.byId(kv.readVarInt());
                    break;
                }
                case UPDATE_LATENCY: {
                    gameProfile5 = new GameProfile(kv.readUUID(), (String)null);
                    integer5 = kv.readVarInt();
                    break;
                }
                case UPDATE_DISPLAY_NAME: {
                    gameProfile5 = new GameProfile(kv.readUUID(), (String)null);
                    if (kv.readBoolean()) {
                        lf8 = kv.readComponent();
                        break;
                    }
                    break;
                }
                case REMOVE_PLAYER: {
                    gameProfile5 = new GameProfile(kv.readUUID(), (String)null);
                    break;
                }
            }
            this.entries.add(new PlayerUpdate(gameProfile5, integer5, bjt7, lf8));
        }
    }
    
    @Override
    public void write(final FriendlyByteBuf kv) throws IOException {
        kv.writeEnum(this.action);
        kv.writeVarInt(this.entries.size());
        for (final PlayerUpdate b4 : this.entries) {
            switch (this.action) {
                case ADD_PLAYER: {
                    kv.writeUUID(b4.getProfile().getId());
                    kv.writeUtf(b4.getProfile().getName());
                    kv.writeVarInt(b4.getProfile().getProperties().size());
                    for (final Property property6 : b4.getProfile().getProperties().values()) {
                        kv.writeUtf(property6.getName());
                        kv.writeUtf(property6.getValue());
                        if (property6.hasSignature()) {
                            kv.writeBoolean(true);
                            kv.writeUtf(property6.getSignature());
                        }
                        else {
                            kv.writeBoolean(false);
                        }
                    }
                    kv.writeVarInt(b4.getGameMode().getId());
                    kv.writeVarInt(b4.getLatency());
                    if (b4.getDisplayName() == null) {
                        kv.writeBoolean(false);
                        continue;
                    }
                    kv.writeBoolean(true);
                    kv.writeComponent(b4.getDisplayName());
                    continue;
                }
                case UPDATE_GAME_MODE: {
                    kv.writeUUID(b4.getProfile().getId());
                    kv.writeVarInt(b4.getGameMode().getId());
                    continue;
                }
                case UPDATE_LATENCY: {
                    kv.writeUUID(b4.getProfile().getId());
                    kv.writeVarInt(b4.getLatency());
                    continue;
                }
                case UPDATE_DISPLAY_NAME: {
                    kv.writeUUID(b4.getProfile().getId());
                    if (b4.getDisplayName() == null) {
                        kv.writeBoolean(false);
                        continue;
                    }
                    kv.writeBoolean(true);
                    kv.writeComponent(b4.getDisplayName());
                    continue;
                }
                case REMOVE_PLAYER: {
                    kv.writeUUID(b4.getProfile().getId());
                    continue;
                }
            }
        }
    }
    
    @Override
    public void handle(final ClientGamePacketListener lw) {
        lw.handlePlayerInfo(this);
    }
    
    public List<PlayerUpdate> getEntries() {
        return this.entries;
    }
    
    public Action getAction() {
        return this.action;
    }
    
    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this).add("action", this.action).add("entries", this.entries).toString();
    }
    
    public enum Action {
        ADD_PLAYER, 
        UPDATE_GAME_MODE, 
        UPDATE_LATENCY, 
        UPDATE_DISPLAY_NAME, 
        REMOVE_PLAYER;
    }
    
    public class PlayerUpdate {
        private final int latency;
        private final GameType gameMode;
        private final GameProfile profile;
        private final Component displayName;
        
        public PlayerUpdate(final GameProfile gameProfile, final int integer, @Nullable final GameType bjt, @Nullable final Component lf) {
            this.profile = gameProfile;
            this.latency = integer;
            this.gameMode = bjt;
            this.displayName = lf;
        }
        
        public GameProfile getProfile() {
            return this.profile;
        }
        
        public int getLatency() {
            return this.latency;
        }
        
        public GameType getGameMode() {
            return this.gameMode;
        }
        
        @Nullable
        public Component getDisplayName() {
            return this.displayName;
        }
        
        @Override
        public String toString() {
            return MoreObjects.toStringHelper(this).add("latency", this.latency).add("gameMode", this.gameMode).add("profile", this.profile).add("displayName", (this.displayName == null) ? null : Component.Serializer.toJson(this.displayName)).toString();
        }
    }
}
