package net.md_5.bungee.protocol.packet;

import io.netty.buffer.ByteBuf;

import java.util.Arrays;

public class PacketD1Team extends DefinedPacket
{

    private String name;
    /**
     * 0 - create, 1 remove, 2 info update, 3 player add, 4 player remove.
     */
    private byte mode;
    private String displayName;
    private String prefix;
    private String suffix;
    private boolean friendlyFire;
    private short playerCount;
    private String[] players;

    private PacketD1Team()
    {
        super( 0xD1 );
    }

    /**
     * Packet to destroy a team.
     *
     * @param name
     */
    public PacketD1Team(String name)
    {
        this();
        this.name = name;
        this.mode = 1;
    }

    @Override
    public void read(ByteBuf buf)
    {
        name = readString( buf );
        mode = buf.readByte();
        if ( mode == 0 || mode == 2 )
        {
            displayName = readString( buf );
            prefix = readString( buf );
            suffix = readString( buf );
            friendlyFire = buf.readBoolean();
        }
        if ( mode == 0 || mode == 3 || mode == 4 )
        {
            players = new String[ buf.readShort() ];
            for ( int i = 0; i < getPlayers().length; i++ )
            {
                players[i] = readString( buf );
            }
        }
    }

    @Override
    public void write(ByteBuf buf)
    {
        writeString( name, buf );
        buf.writeByte( mode );
        if ( mode == 0 || mode == 2 )
        {
            writeString( displayName, buf );
            writeString( prefix, buf );
            writeString( suffix, buf );
            buf.writeBoolean( friendlyFire );
        }
        if ( mode == 0 || mode == 3 || mode == 4 )
        {
            buf.writeShort( players.length );
            for ( int i = 0; i < players.length; i++ )
            {
                writeString( players[i], buf );
            }
        }
    }

    @Override
    public void handle(AbstractPacketHandler handler) throws Exception
    {
        handler.handle( this );
    }

    public String getName() {
        return this.name;
    }

    public byte getMode() {
        return this.mode;
    }

    public String getDisplayName() {
        return this.displayName;
    }

    public String getPrefix() {
        return this.prefix;
    }

    public String getSuffix() {
        return this.suffix;
    }

    public boolean isFriendlyFire() {
        return this.friendlyFire;
    }

    public short getPlayerCount() {
        return this.playerCount;
    }

    public String[] getPlayers() {
        return this.players;
    }

    @Override
    public String toString() {
        return "PacketD1Team(name=" + this.getName() + ", mode=" + this.getMode() + ", displayName=" + this.getDisplayName() + ", prefix=" + this.getPrefix() + ", suffix=" + this.getSuffix() + ", friendlyFire=" + this.isFriendlyFire()
                + ", playerCount=" + this.getPlayerCount() + ", players=" + Arrays.deepToString(this.getPlayers()) + ")";
    }

    @Override
    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof PacketD1Team)) {
            return false;
        }
        final PacketD1Team other = (PacketD1Team) o;
        if (!other.canEqual(this)) {
            return false;
        }
        final Object this$name = this.getName();
        final Object other$name = other.getName();
        Label_0065: {
            if (this$name == null) {
                if (other$name == null) {
                    break Label_0065;
                }
            } else if (this$name.equals(other$name)) {
                break Label_0065;
            }
            return false;
        }
        if (this.getMode() != other.getMode()) {
            return false;
        }
        final Object this$displayName = this.getDisplayName();
        final Object other$displayName = other.getDisplayName();
        Label_0115: {
            if (this$displayName == null) {
                if (other$displayName == null) {
                    break Label_0115;
                }
            } else if (this$displayName.equals(other$displayName)) {
                break Label_0115;
            }
            return false;
        }
        final Object this$prefix = this.getPrefix();
        final Object other$prefix = other.getPrefix();
        Label_0152: {
            if (this$prefix == null) {
                if (other$prefix == null) {
                    break Label_0152;
                }
            } else if (this$prefix.equals(other$prefix)) {
                break Label_0152;
            }
            return false;
        }
        final Object this$suffix = this.getSuffix();
        final Object other$suffix = other.getSuffix();
        if (this$suffix == null) {
            if (other$suffix == null) {
                return this.isFriendlyFire() == other.isFriendlyFire() && this.getPlayerCount() == other.getPlayerCount() && Arrays.deepEquals(this.getPlayers(), other.getPlayers());
            }
        } else if (this$suffix.equals(other$suffix)) {
            return this.isFriendlyFire() == other.isFriendlyFire() && this.getPlayerCount() == other.getPlayerCount() && Arrays.deepEquals(this.getPlayers(), other.getPlayers());
        }
        return false;
    }

    public boolean canEqual(final Object other) {
        return other instanceof PacketD1Team;
    }

    @Override
    public int hashCode() {
        final int PRIME = 31;
        int result = 1;
        final Object $name = this.getName();
        result = result * 31 + (($name == null) ? 0 : $name.hashCode());
        result = result * 31 + this.getMode();
        final Object $displayName = this.getDisplayName();
        result = result * 31 + (($displayName == null) ? 0 : $displayName.hashCode());
        final Object $prefix = this.getPrefix();
        result = result * 31 + (($prefix == null) ? 0 : $prefix.hashCode());
        final Object $suffix = this.getSuffix();
        result = result * 31 + (($suffix == null) ? 0 : $suffix.hashCode());
        result = result * 31 + (this.isFriendlyFire() ? 1231 : 1237);
        result = result * 31 + this.getPlayerCount();
        result = result * 31 + Arrays.deepHashCode(this.getPlayers());
        return result;
    }
}
