package net.minecraft.commands.arguments.item;

import net.minecraft.nbt.NbtUtils;
import javax.annotation.Nullable;
import net.minecraft.nbt.CompoundTag;
import java.util.Arrays;
import net.minecraft.network.chat.TranslatableComponent;
import com.mojang.brigadier.Message;
import com.mojang.brigadier.suggestion.Suggestions;
import java.util.concurrent.CompletableFuture;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.world.item.ItemStack;
import java.util.function.Predicate;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.tags.Tag;
import net.minecraft.commands.CommandSourceStack;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import java.util.Collection;
import com.mojang.brigadier.arguments.ArgumentType;

public class ItemPredicateArgument implements ArgumentType<Result> {
    private static final Collection<String> EXAMPLES;
    private static final DynamicCommandExceptionType ERROR_UNKNOWN_TAG;
    
    public static ItemPredicateArgument itemPredicate() {
        return new ItemPredicateArgument();
    }
    
    public Result parse(final StringReader stringReader) throws CommandSyntaxException {
        final ItemParser el3 = new ItemParser(stringReader, true).parse();
        if (el3.getItem() != null) {
            final ItemPredicate a4 = new ItemPredicate(el3.getItem(), el3.getNbt());
            return commandContext -> a4;
        }
        final ResourceLocation sm4 = el3.getTag();
        final ResourceLocation sm5;
        final Tag<Object> aaz4;
        final ItemParser itemParser;
        return commandContext -> {
            aaz4 = (Tag<Object>)((CommandSourceStack)commandContext.getSource()).getServer().getTags().getItems().getTag(sm5);
            if (aaz4 == null) {
                throw ItemPredicateArgument.ERROR_UNKNOWN_TAG.create(sm5.toString());
            }
            else {
                return new TagPredicate((Tag<Item>)aaz4, itemParser.getNbt());
            }
        };
    }
    
    public static Predicate<ItemStack> getItemPredicate(final CommandContext<CommandSourceStack> commandContext, final String string) throws CommandSyntaxException {
        return ((Result)commandContext.getArgument(string, (Class)Result.class)).create(commandContext);
    }
    
    public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> commandContext, final SuggestionsBuilder suggestionsBuilder) {
        final StringReader stringReader4 = new StringReader(suggestionsBuilder.getInput());
        stringReader4.setCursor(suggestionsBuilder.getStart());
        final ItemParser el5 = new ItemParser(stringReader4, true);
        try {
            el5.parse();
        }
        catch (CommandSyntaxException ex) {}
        return el5.fillSuggestions(suggestionsBuilder);
    }
    
    public Collection<String> getExamples() {
        return ItemPredicateArgument.EXAMPLES;
    }
    
    static {
        EXAMPLES = Arrays.<String>asList("stick", "minecraft:stick", "#stick", "#stick{foo=bar}");
        final TranslatableComponent translatableComponent;
        ERROR_UNKNOWN_TAG = new DynamicCommandExceptionType(object -> {
            new TranslatableComponent("arguments.item.tag.unknown", new Object[] { object });
            return translatableComponent;
        });
    }
    
    static class ItemPredicate implements Predicate<ItemStack> {
        private final Item item;
        @Nullable
        private final CompoundTag nbt;
        
        public ItemPredicate(final Item bei, @Nullable final CompoundTag jt) {
            this.item = bei;
            this.nbt = jt;
        }
        
        @Override
        public boolean test(final ItemStack ben) {
            return ben.getItem() == this.item && NbtUtils.compareNbt(this.nbt, ben.getTag(), true);
        }
    }
    
    static class TagPredicate implements Predicate<ItemStack> {
        private final Tag<Item> tag;
        @Nullable
        private final CompoundTag nbt;
        
        public TagPredicate(final Tag<Item> aaz, @Nullable final CompoundTag jt) {
            this.tag = aaz;
            this.nbt = jt;
        }
        
        @Override
        public boolean test(final ItemStack ben) {
            return this.tag.contains(ben.getItem()) && NbtUtils.compareNbt(this.nbt, ben.getTag(), true);
        }
    }
    
    public interface Result {
        Predicate<ItemStack> create(final CommandContext<CommandSourceStack> commandContext) throws CommandSyntaxException;
    }
}
