package net.minecraft.network.protocol.game;

import net.minecraft.network.PacketListener;
import java.util.Iterator;
import java.io.IOException;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.scores.PlayerTeam;
import com.google.common.collect.Lists;
import net.minecraft.world.scores.Team;
import net.minecraft.network.chat.TextComponent;
import java.util.Collection;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.Packet;

public class ClientboundSetPlayerTeamPacket implements Packet<ClientGamePacketListener> {
    private String name;
    private Component displayName;
    private Component playerPrefix;
    private Component playerSuffix;
    private String nametagVisibility;
    private String collisionRule;
    private ChatFormatting color;
    private final Collection<String> players;
    private int method;
    private int options;
    
    public ClientboundSetPlayerTeamPacket() {
        this.name = "";
        this.displayName = new TextComponent("");
        this.playerPrefix = new TextComponent("");
        this.playerSuffix = new TextComponent("");
        this.nametagVisibility = Team.Visibility.ALWAYS.name;
        this.collisionRule = Team.CollisionRule.ALWAYS.name;
        this.color = ChatFormatting.RESET;
        this.players = Lists.newArrayList();
    }
    
    public ClientboundSetPlayerTeamPacket(final PlayerTeam cwj, final int integer) {
        this.name = "";
        this.displayName = new TextComponent("");
        this.playerPrefix = new TextComponent("");
        this.playerSuffix = new TextComponent("");
        this.nametagVisibility = Team.Visibility.ALWAYS.name;
        this.collisionRule = Team.CollisionRule.ALWAYS.name;
        this.color = ChatFormatting.RESET;
        this.players = Lists.newArrayList();
        this.name = cwj.getName();
        this.method = integer;
        if (integer == 0 || integer == 2) {
            this.displayName = cwj.getDisplayName();
            this.options = cwj.packOptions();
            this.nametagVisibility = cwj.getNameTagVisibility().name;
            this.collisionRule = cwj.getCollisionRule().name;
            this.color = cwj.getColor();
            this.playerPrefix = cwj.getPlayerPrefix();
            this.playerSuffix = cwj.getPlayerSuffix();
        }
        if (integer == 0) {
            this.players.addAll(cwj.getPlayers());
        }
    }
    
    public ClientboundSetPlayerTeamPacket(final PlayerTeam cwj, final Collection<String> collection, final int integer) {
        this.name = "";
        this.displayName = new TextComponent("");
        this.playerPrefix = new TextComponent("");
        this.playerSuffix = new TextComponent("");
        this.nametagVisibility = Team.Visibility.ALWAYS.name;
        this.collisionRule = Team.CollisionRule.ALWAYS.name;
        this.color = ChatFormatting.RESET;
        this.players = Lists.newArrayList();
        if (integer != 3 && integer != 4) {
            throw new IllegalArgumentException("Method must be join or leave for player constructor");
        }
        if (collection == null || collection.isEmpty()) {
            throw new IllegalArgumentException("Players cannot be null/empty");
        }
        this.method = integer;
        this.name = cwj.getName();
        this.players.addAll(collection);
    }
    
    @Override
    public void read(final FriendlyByteBuf kv) throws IOException {
        this.name = kv.readUtf(16);
        this.method = kv.readByte();
        if (this.method == 0 || this.method == 2) {
            this.displayName = kv.readComponent();
            this.options = kv.readByte();
            this.nametagVisibility = kv.readUtf(40);
            this.collisionRule = kv.readUtf(40);
            this.color = kv.<ChatFormatting>readEnum(ChatFormatting.class);
            this.playerPrefix = kv.readComponent();
            this.playerSuffix = kv.readComponent();
        }
        if (this.method == 0 || this.method == 3 || this.method == 4) {
            for (int integer3 = kv.readVarInt(), integer4 = 0; integer4 < integer3; ++integer4) {
                this.players.add(kv.readUtf(40));
            }
        }
    }
    
    @Override
    public void write(final FriendlyByteBuf kv) throws IOException {
        kv.writeUtf(this.name);
        kv.writeByte(this.method);
        if (this.method == 0 || this.method == 2) {
            kv.writeComponent(this.displayName);
            kv.writeByte(this.options);
            kv.writeUtf(this.nametagVisibility);
            kv.writeUtf(this.collisionRule);
            kv.writeEnum(this.color);
            kv.writeComponent(this.playerPrefix);
            kv.writeComponent(this.playerSuffix);
        }
        if (this.method == 0 || this.method == 3 || this.method == 4) {
            kv.writeVarInt(this.players.size());
            for (final String string4 : this.players) {
                kv.writeUtf(string4);
            }
        }
    }
    
    @Override
    public void handle(final ClientGamePacketListener lw) {
        lw.handleSetPlayerTeamPacket(this);
    }
    
    public String getName() {
        return this.name;
    }
    
    public Component getDisplayName() {
        return this.displayName;
    }
    
    public Collection<String> getPlayers() {
        return this.players;
    }
    
    public int getMethod() {
        return this.method;
    }
    
    public int getOptions() {
        return this.options;
    }
    
    public ChatFormatting getColor() {
        return this.color;
    }
    
    public String getNametagVisibility() {
        return this.nametagVisibility;
    }
    
    public String getCollisionRule() {
        return this.collisionRule;
    }
    
    public Component getPlayerPrefix() {
        return this.playerPrefix;
    }
    
    public Component getPlayerSuffix() {
        return this.playerSuffix;
    }
}
