package net.md_5.bungee.protocol.packet;

import io.netty.buffer.ByteBuf;
import java.util.Arrays;
import java.util.Objects;

public class PacketCBTabComplete extends DefinedPacket
{

    public String getCursor() {
        return cursor;
    }

    public String[] getCommands() {
        return commands;
    }

    private String cursor;
    private String[] commands;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PacketCBTabComplete that = (PacketCBTabComplete) o;
        return Objects.equals(cursor, that.cursor) && Objects.deepEquals(commands, that.commands);
    }

    @Override
    public int hashCode() {
        return Objects.hash(cursor, Arrays.hashCode(commands));
    }

    private PacketCBTabComplete()
    {
        super( 0xCB );
    }

    public PacketCBTabComplete(String[] alternatives)
    {
        this();
        commands = alternatives;
    }

    @Override
    public String toString() {
        return "PacketCBTabComplete{" +
                "cursor='" + cursor + '\'' +
                ", commands=" + Arrays.toString(commands) +
                '}';
    }

    @Override
    public void read(ByteBuf buf)
    {
        cursor = readString( buf );
    }

    @Override
    public void write(ByteBuf buf)
    {
        StringBuilder tab = new StringBuilder();
        for ( String alternative : commands )
        {
            tab.append( alternative );
            tab.append( "\00" );
        }
        writeString( tab.substring( 0, tab.length() - 1 ), buf );
    }

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