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 mg) throws IOException {
        this.id = mg.readVarInt();
        final int integer3 = mg.readVarInt();
        final int integer4 = mg.readVarInt();
        final StringRange stringRange5 = StringRange.between(integer3, integer3 + integer4);
        final int integer5 = mg.readVarInt();
        final List<Suggestion> list7 = Lists.newArrayListWithCapacity(integer5);
        for (int integer6 = 0; integer6 < integer5; ++integer6) {
            final String string9 = mg.readUtf(32767);
            final Component mr10 = mg.readBoolean() ? mg.readComponent() : null;
            list7.add(new Suggestion(stringRange5, string9, (Message)mr10));
        }
        this.suggestions = new Suggestions(stringRange5, (List)list7);
    }
    
    @Override
    public void write(final FriendlyByteBuf mg) throws IOException {
        mg.writeVarInt(this.id);
        mg.writeVarInt(this.suggestions.getRange().getStart());
        mg.writeVarInt(this.suggestions.getRange().getLength());
        mg.writeVarInt(this.suggestions.getList().size());
        for (final Suggestion suggestion4 : this.suggestions.getList()) {
            mg.writeUtf(suggestion4.getText());
            mg.writeBoolean(suggestion4.getTooltip() != null);
            if (suggestion4.getTooltip() != null) {
                mg.writeComponent(ComponentUtils.fromMessage(suggestion4.getTooltip()));
            }
        }
    }
    
    @Override
    public void handle(final ClientGamePacketListener nl) {
        nl.handleCommandSuggestions(this);
    }
    
    public int getId() {
        return this.id;
    }
    
    public Suggestions getSuggestions() {
        return this.suggestions;
    }
}
