package net.minecraft.client.multiplayer;

import net.minecraft.world.level.Level;
import net.minecraft.resources.ResourceKey;
import java.util.Set;
import net.minecraft.world.phys.Vec3;
import net.minecraft.core.BlockPos;
import net.minecraft.world.phys.BlockHitResult;
import java.util.Locale;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ServerboundCommandSuggestionPacket;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.client.player.LocalPlayer;
import java.util.stream.Stream;
import net.minecraft.resources.ResourceLocation;
import java.util.Collections;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;
import java.util.Iterator;
import java.util.List;
import com.google.common.collect.Lists;
import java.util.Collection;
import com.mojang.brigadier.suggestion.Suggestions;
import java.util.concurrent.CompletableFuture;
import net.minecraft.client.Minecraft;
import net.minecraft.commands.SharedSuggestionProvider;

public class ClientSuggestionProvider implements SharedSuggestionProvider {
    private final ClientPacketListener connection;
    private final Minecraft minecraft;
    private int pendingSuggestionsId;
    private CompletableFuture<Suggestions> pendingSuggestionsFuture;
    
    public ClientSuggestionProvider(final ClientPacketListener dyn, final Minecraft dlx) {
        this.pendingSuggestionsId = -1;
        this.connection = dyn;
        this.minecraft = dlx;
    }
    
    @Override
    public Collection<String> getOnlinePlayerNames() {
        final List<String> list2 = Lists.newArrayList();
        for (final PlayerInfo dyq4 : this.connection.getOnlinePlayers()) {
            list2.add(dyq4.getProfile().getName());
        }
        return list2;
    }
    
    @Override
    public Collection<String> getSelectedEntities() {
        if (this.minecraft.hitResult != null && this.minecraft.hitResult.getType() == HitResult.Type.ENTITY) {
            return Collections.<String>singleton(((EntityHitResult)this.minecraft.hitResult).getEntity().getStringUUID());
        }
        return Collections.emptyList();
    }
    
    @Override
    public Collection<String> getAllTeams() {
        return this.connection.getLevel().getScoreboard().getTeamNames();
    }
    
    @Override
    public Collection<ResourceLocation> getAvailableSoundEvents() {
        return this.minecraft.getSoundManager().getAvailableSounds();
    }
    
    @Override
    public Stream<ResourceLocation> getRecipeNames() {
        return this.connection.getRecipeManager().getRecipeIds();
    }
    
    @Override
    public boolean hasPermission(final int integer) {
        final LocalPlayer ebf3 = this.minecraft.player;
        return (ebf3 != null) ? ebf3.hasPermissions(integer) : (integer == 0);
    }
    
    @Override
    public CompletableFuture<Suggestions> customSuggestion(final CommandContext<SharedSuggestionProvider> commandContext, final SuggestionsBuilder suggestionsBuilder) {
        if (this.pendingSuggestionsFuture != null) {
            this.pendingSuggestionsFuture.cancel(false);
        }
        this.pendingSuggestionsFuture = new CompletableFuture<Suggestions>();
        final int integer4 = ++this.pendingSuggestionsId;
        this.connection.send(new ServerboundCommandSuggestionPacket(integer4, commandContext.getInput()));
        return this.pendingSuggestionsFuture;
    }
    
    private static String prettyPrint(final double double1) {
        return String.format(Locale.ROOT, "%.2f", double1);
    }
    
    private static String prettyPrint(final int integer) {
        return Integer.toString(integer);
    }
    
    @Override
    public Collection<TextCoordinates> getRelevantCoordinates() {
        final HitResult dej2 = this.minecraft.hitResult;
        if (dej2 == null || dej2.getType() != HitResult.Type.BLOCK) {
            return super.getRelevantCoordinates();
        }
        final BlockPos fu3 = ((BlockHitResult)dej2).getBlockPos();
        return Collections.<TextCoordinates>singleton(new TextCoordinates(prettyPrint(fu3.getX()), prettyPrint(fu3.getY()), prettyPrint(fu3.getZ())));
    }
    
    @Override
    public Collection<TextCoordinates> getAbsoluteCoordinates() {
        final HitResult dej2 = this.minecraft.hitResult;
        if (dej2 == null || dej2.getType() != HitResult.Type.BLOCK) {
            return super.getAbsoluteCoordinates();
        }
        final Vec3 dem3 = dej2.getLocation();
        return Collections.<TextCoordinates>singleton(new TextCoordinates(prettyPrint(dem3.x), prettyPrint(dem3.y), prettyPrint(dem3.z)));
    }
    
    @Override
    public Set<ResourceKey<Level>> levels() {
        return this.connection.levels();
    }
    
    public void completeCustomSuggestions(final int integer, final Suggestions suggestions) {
        if (integer == this.pendingSuggestionsId) {
            this.pendingSuggestionsFuture.complete(suggestions);
            this.pendingSuggestionsFuture = null;
            this.pendingSuggestionsId = -1;
        }
    }
}
