package net.minecraft.commands.arguments;

import net.minecraft.Util;
import com.google.common.collect.Maps;
import java.util.HashMap;
import javax.annotation.Nullable;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.Vec3;
import java.util.function.BiFunction;
import java.util.Map;
import java.util.Arrays;
import net.minecraft.network.chat.TranslatableComponent;
import com.mojang.brigadier.Message;
import net.minecraft.commands.SharedSuggestionProvider;
import com.mojang.brigadier.suggestion.Suggestions;
import java.util.concurrent.CompletableFuture;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.ImmutableStringReader;
import com.mojang.brigadier.StringReader;
import net.minecraft.commands.CommandSourceStack;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import java.util.Collection;
import com.mojang.brigadier.arguments.ArgumentType;

public class EntityAnchorArgument implements ArgumentType<Anchor> {
    private static final Collection<String> EXAMPLES;
    private static final DynamicCommandExceptionType ERROR_INVALID;
    
    public static Anchor getAnchor(final CommandContext<CommandSourceStack> commandContext, final String string) {
        return (Anchor)commandContext.getArgument(string, (Class)Anchor.class);
    }
    
    public static EntityAnchorArgument anchor() {
        return new EntityAnchorArgument();
    }
    
    public Anchor parse(final StringReader stringReader) throws CommandSyntaxException {
        final int integer3 = stringReader.getCursor();
        final String string4 = stringReader.readUnquotedString();
        final Anchor a5 = Anchor.getByName(string4);
        if (a5 == null) {
            stringReader.setCursor(integer3);
            throw EntityAnchorArgument.ERROR_INVALID.createWithContext((ImmutableStringReader)stringReader, string4);
        }
        return a5;
    }
    
    public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> commandContext, final SuggestionsBuilder suggestionsBuilder) {
        return SharedSuggestionProvider.suggest(Anchor.BY_NAME.keySet(), suggestionsBuilder);
    }
    
    public Collection<String> getExamples() {
        return EntityAnchorArgument.EXAMPLES;
    }
    
    static {
        EXAMPLES = Arrays.<String>asList("eyes", "feet");
        final TranslatableComponent translatableComponent;
        ERROR_INVALID = new DynamicCommandExceptionType(object -> {
            new TranslatableComponent("argument.anchor.invalid", new Object[] { object });
            return translatableComponent;
        });
    }
    
    public enum Anchor {
        FEET("feet", (cvl, akq) -> cvl), 
        EYES("eyes", (cvl, akq) -> new Vec3(cvl.x, cvl.y + akq.getEyeHeight(), cvl.z));
        
        private static final Map<String, Anchor> BY_NAME;
        private final String name;
        private final BiFunction<Vec3, Entity, Vec3> transform;
        
        private Anchor(final String string3, final BiFunction<Vec3, Entity, Vec3> biFunction) {
            this.name = string3;
            this.transform = biFunction;
        }
        
        @Nullable
        public static Anchor getByName(final String string) {
            return Anchor.BY_NAME.get(string);
        }
        
        public Vec3 apply(final Entity akq) {
            return this.transform.apply(akq.position(), akq);
        }
        
        public Vec3 apply(final CommandSourceStack cq) {
            final Entity akq3 = cq.getEntity();
            if (akq3 == null) {
                return cq.getPosition();
            }
            return this.transform.apply(cq.getPosition(), akq3);
        }
        
        static {
            final Anchor[] array;
            int length;
            int i = 0;
            Anchor a5;
            BY_NAME = Util.<Map<String, Anchor>>make(Maps.newHashMap(), hashMap -> {
                values();
                for (length = array.length; i < length; ++i) {
                    a5 = array[i];
                    hashMap.put(a5.name, a5);
                }
            });
        }
    }
}
