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 = TextComponent.EMPTY;
        this.playerPrefix = TextComponent.EMPTY;
        this.playerSuffix = TextComponent.EMPTY;
        this.nametagVisibility = Team.Visibility.ALWAYS.name;
        this.collisionRule = Team.CollisionRule.ALWAYS.name;
        this.color = ChatFormatting.RESET;
        this.players = Lists.newArrayList();
    }
    
    public ClientboundSetPlayerTeamPacket(final PlayerTeam dfk, final int integer) {
        this.name = "";
        this.displayName = TextComponent.EMPTY;
        this.playerPrefix = TextComponent.EMPTY;
        this.playerSuffix = TextComponent.EMPTY;
        this.nametagVisibility = Team.Visibility.ALWAYS.name;
        this.collisionRule = Team.CollisionRule.ALWAYS.name;
        this.color = ChatFormatting.RESET;
        this.players = Lists.newArrayList();
        this.name = dfk.getName();
        this.method = integer;
        if (integer == 0 || integer == 2) {
            this.displayName = dfk.getDisplayName();
            this.options = dfk.packOptions();
            this.nametagVisibility = dfk.getNameTagVisibility().name;
            this.collisionRule = dfk.getCollisionRule().name;
            this.color = dfk.getColor();
            this.playerPrefix = dfk.getPlayerPrefix();
            this.playerSuffix = dfk.getPlayerSuffix();
        }
        if (integer == 0) {
            this.players.addAll(dfk.getPlayers());
        }
    }
    
    public ClientboundSetPlayerTeamPacket(final PlayerTeam dfk, final Collection<String> collection, final int integer) {
        this.name = "";
        this.displayName = TextComponent.EMPTY;
        this.playerPrefix = TextComponent.EMPTY;
        this.playerSuffix = TextComponent.EMPTY;
        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 = dfk.getName();
        this.players.addAll(collection);
    }
    
    @Override
    public void read(final FriendlyByteBuf mg) throws IOException {
        this.name = mg.readUtf(16);
        this.method = mg.readByte();
        if (this.method == 0 || this.method == 2) {
            this.displayName = mg.readComponent();
            this.options = mg.readByte();
            this.nametagVisibility = mg.readUtf(40);
            this.collisionRule = mg.readUtf(40);
            this.color = mg.<ChatFormatting>readEnum(ChatFormatting.class);
            this.playerPrefix = mg.readComponent();
            this.playerSuffix = mg.readComponent();
        }
        if (this.method == 0 || this.method == 3 || this.method == 4) {
            for (int integer3 = mg.readVarInt(), integer4 = 0; integer4 < integer3; ++integer4) {
                this.players.add(mg.readUtf(40));
            }
        }
    }
    
    @Override
    public void write(final FriendlyByteBuf mg) throws IOException {
        mg.writeUtf(this.name);
        mg.writeByte(this.method);
        if (this.method == 0 || this.method == 2) {
            mg.writeComponent(this.displayName);
            mg.writeByte(this.options);
            mg.writeUtf(this.nametagVisibility);
            mg.writeUtf(this.collisionRule);
            mg.writeEnum(this.color);
            mg.writeComponent(this.playerPrefix);
            mg.writeComponent(this.playerSuffix);
        }
        if (this.method == 0 || this.method == 3 || this.method == 4) {
            mg.writeVarInt(this.players.size());
            for (final String string4 : this.players) {
                mg.writeUtf(string4);
            }
        }
    }
    
    @Override
    public void handle(final ClientGamePacketListener nl) {
        nl.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;
    }
}
