package net.minecraft.network.protocol.game;

import net.minecraft.network.PacketListener;
import java.util.Iterator;
import net.minecraft.network.chat.ComponentUtils;
import java.io.IOException;
import net.minecraft.network.chat.Component;
import java.util.List;
import com.mojang.brigadier.Message;
import com.mojang.brigadier.suggestion.Suggestion;
import com.google.common.collect.Lists;
import com.mojang.brigadier.context.StringRange;
import net.minecraft.network.FriendlyByteBuf;
import com.mojang.brigadier.suggestion.Suggestions;
import net.minecraft.network.protocol.Packet;

public class ClientboundCommandSuggestionsPacket implements Packet<ClientGamePacketListener> {
    private int id;
    private Suggestions suggestions;
    
    public ClientboundCommandSuggestionsPacket() {
    }
    
    public ClientboundCommandSuggestionsPacket(final int integer, final Suggestions suggestions) {
        this.id = integer;
        this.suggestions = suggestions;
    }
    
    @Override
    public void read(final FriendlyByteBuf kv) throws IOException {
        this.id = kv.readVarInt();
        final int integer3 = kv.readVarInt();
        final int integer4 = kv.readVarInt();
        final StringRange stringRange5 = StringRange.between(integer3, integer3 + integer4);
        final int integer5 = kv.readVarInt();
        final List<Suggestion> list7 = Lists.newArrayListWithCapacity(integer5);
        for (int integer6 = 0; integer6 < integer5; ++integer6) {
            final String string9 = kv.readUtf(32767);
            final Component lf10 = kv.readBoolean() ? kv.readComponent() : null;
            list7.add(new Suggestion(stringRange5, string9, (Message)lf10));
        }
        this.suggestions = new Suggestions(stringRange5, (List)list7);
    }
    
    @Override
    public void write(final FriendlyByteBuf kv) throws IOException {
        kv.writeVarInt(this.id);
        kv.writeVarInt(this.suggestions.getRange().getStart());
        kv.writeVarInt(this.suggestions.getRange().getLength());
        kv.writeVarInt(this.suggestions.getList().size());
        for (final Suggestion suggestion4 : this.suggestions.getList()) {
            kv.writeUtf(suggestion4.getText());
            kv.writeBoolean(suggestion4.getTooltip() != null);
            if (suggestion4.getTooltip() != null) {
                kv.writeComponent(ComponentUtils.fromMessage(suggestion4.getTooltip()));
            }
        }
    }
    
    @Override
    public void handle(final ClientGamePacketListener lw) {
        lw.handleCommandSuggestions(this);
    }
    
    public int getId() {
        return this.id;
    }
    
    public Suggestions getSuggestions() {
        return this.suggestions;
    }
}
