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 ze7 : arr) {
            this.entries.add(new PlayerUpdate(ze7.getGameProfile(), ze7.latency, ze7.gameMode.getGameModeForPlayer(), ze7.getTabListDisplayName()));
        }
    }
    
    public ClientboundPlayerInfoPacket(final Action a, final Iterable<ServerPlayer> iterable) {
        this.entries = Lists.newArrayList();
        this.action = a;
        for (final ServerPlayer ze5 : iterable) {
            this.entries.add(new PlayerUpdate(ze5.getGameProfile(), ze5.latency, ze5.gameMode.getGameModeForPlayer(), ze5.getTabListDisplayName()));
        }
    }
    
    @Override
    public void read(final FriendlyByteBuf mg) throws IOException {
        this.action = mg.<Action>readEnum(Action.class);
        for (int integer3 = mg.readVarInt(), integer4 = 0; integer4 < integer3; ++integer4) {
            GameProfile gameProfile5 = null;
            int integer5 = 0;
            GameType bpy7 = null;
            Component mr8 = null;
            switch (this.action) {
                case ADD_PLAYER: {
                    gameProfile5 = new GameProfile(mg.readUUID(), mg.readUtf(16));
                    for (int integer6 = mg.readVarInt(), integer7 = 0; integer7 < integer6; ++integer7) {
                        final String string11 = mg.readUtf(32767);
                        final String string12 = mg.readUtf(32767);
                        if (mg.readBoolean()) {
                            gameProfile5.getProperties().put(string11, new Property(string11, string12, mg.readUtf(32767)));
                        }
                        else {
                            gameProfile5.getProperties().put(string11, new Property(string11, string12));
                        }
                    }
                    bpy7 = GameType.byId(mg.readVarInt());
                    integer5 = mg.readVarInt();
                    if (mg.readBoolean()) {
                        mr8 = mg.readComponent();
                        break;
                    }
                    break;
                }
                case UPDATE_GAME_MODE: {
                    gameProfile5 = new GameProfile(mg.readUUID(), (String)null);
                    bpy7 = GameType.byId(mg.readVarInt());
                    break;
                }
                case UPDATE_LATENCY: {
                    gameProfile5 = new GameProfile(mg.readUUID(), (String)null);
                    integer5 = mg.readVarInt();
                    break;
                }
                case UPDATE_DISPLAY_NAME: {
                    gameProfile5 = new GameProfile(mg.readUUID(), (String)null);
                    if (mg.readBoolean()) {
                        mr8 = mg.readComponent();
                        break;
                    }
                    break;
                }
                case REMOVE_PLAYER: {
                    gameProfile5 = new GameProfile(mg.readUUID(), (String)null);
                    break;
                }
            }
            this.entries.add(new PlayerUpdate(gameProfile5, integer5, bpy7, mr8));
        }
    }
    
    @Override
    public void write(final FriendlyByteBuf mg) throws IOException {
        mg.writeEnum(this.action);
        mg.writeVarInt(this.entries.size());
        for (final PlayerUpdate b4 : this.entries) {
            switch (this.action) {
                case ADD_PLAYER: {
                    mg.writeUUID(b4.getProfile().getId());
                    mg.writeUtf(b4.getProfile().getName());
                    mg.writeVarInt(b4.getProfile().getProperties().size());
                    for (final Property property6 : b4.getProfile().getProperties().values()) {
                        mg.writeUtf(property6.getName());
                        mg.writeUtf(property6.getValue());
                        if (property6.hasSignature()) {
                            mg.writeBoolean(true);
                            mg.writeUtf(property6.getSignature());
                        }
                        else {
                            mg.writeBoolean(false);
                        }
                    }
                    mg.writeVarInt(b4.getGameMode().getId());
                    mg.writeVarInt(b4.getLatency());
                    if (b4.getDisplayName() == null) {
                        mg.writeBoolean(false);
                        continue;
                    }
                    mg.writeBoolean(true);
                    mg.writeComponent(b4.getDisplayName());
                    continue;
                }
                case UPDATE_GAME_MODE: {
                    mg.writeUUID(b4.getProfile().getId());
                    mg.writeVarInt(b4.getGameMode().getId());
                    continue;
                }
                case UPDATE_LATENCY: {
                    mg.writeUUID(b4.getProfile().getId());
                    mg.writeVarInt(b4.getLatency());
                    continue;
                }
                case UPDATE_DISPLAY_NAME: {
                    mg.writeUUID(b4.getProfile().getId());
                    if (b4.getDisplayName() == null) {
                        mg.writeBoolean(false);
                        continue;
                    }
                    mg.writeBoolean(true);
                    mg.writeComponent(b4.getDisplayName());
                    continue;
                }
                case REMOVE_PLAYER: {
                    mg.writeUUID(b4.getProfile().getId());
                    continue;
                }
            }
        }
    }
    
    @Override
    public void handle(final ClientGamePacketListener nl) {
        nl.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 bpy, @Nullable final Component mr) {
            this.profile = gameProfile;
            this.latency = integer;
            this.gameMode = bpy;
            this.displayName = mr;
        }
        
        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();
        }
    }
}
