package net.minecraft.commands.arguments.blocks;

import net.minecraft.world.level.block.entity.BlockEntity;
import java.util.Iterator;
import net.minecraft.nbt.NbtUtils;
import javax.annotation.Nullable;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.block.state.properties.Property;
import java.util.Set;
import net.minecraft.world.level.block.state.BlockState;
import java.util.Arrays;
import net.minecraft.network.chat.TranslatableComponent;
import com.mojang.brigadier.Message;
import net.minecraft.world.level.block.Block;
import net.minecraft.tags.TagManager;
import net.minecraft.tags.BlockTags;
import com.mojang.brigadier.suggestion.Suggestions;
import java.util.concurrent.CompletableFuture;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.world.level.block.state.pattern.BlockInWorld;
import java.util.function.Predicate;
import net.minecraft.commands.CommandSourceStack;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.resources.ResourceLocation;
import java.util.Map;
import net.minecraft.tags.Tag;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import java.util.Collection;
import com.mojang.brigadier.arguments.ArgumentType;

public class BlockPredicateArgument implements ArgumentType<Result> {
    private static final Collection<String> EXAMPLES;
    private static final DynamicCommandExceptionType ERROR_UNKNOWN_TAG;
    
    public static BlockPredicateArgument blockPredicate() {
        return new BlockPredicateArgument();
    }
    
    public Result parse(final StringReader stringReader) throws CommandSyntaxException {
        final BlockStateParser ef3 = new BlockStateParser(stringReader, true).parse(true);
        if (ef3.getState() != null) {
            final BlockPredicate a4 = new BlockPredicate(ef3.getState(), ef3.getProperties().keySet(), ef3.getNbt());
            return adh -> a4;
        }
        final ResourceLocation uh4 = ef3.getTag();
        final ResourceLocation uh5;
        final Tag<Object> adf4;
        final BlockStateParser blockStateParser;
        return adh -> {
            adf4 = adh.getBlocks().getTag(uh5);
            if (adf4 == null) {
                throw BlockPredicateArgument.ERROR_UNKNOWN_TAG.create(uh5.toString());
            }
            else {
                return new TagPredicate((Tag)adf4, (Map)blockStateParser.getVagueProperties(), blockStateParser.getNbt());
            }
        };
    }
    
    public static Predicate<BlockInWorld> getBlockPredicate(final CommandContext<CommandSourceStack> commandContext, final String string) throws CommandSyntaxException {
        return ((Result)commandContext.getArgument(string, (Class)Result.class)).create(((CommandSourceStack)commandContext.getSource()).getServer().getTags());
    }
    
    public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> commandContext, final SuggestionsBuilder suggestionsBuilder) {
        final StringReader stringReader4 = new StringReader(suggestionsBuilder.getInput());
        stringReader4.setCursor(suggestionsBuilder.getStart());
        final BlockStateParser ef5 = new BlockStateParser(stringReader4, true);
        try {
            ef5.parse(true);
        }
        catch (CommandSyntaxException ex) {}
        return ef5.fillSuggestions(suggestionsBuilder, BlockTags.getAllTags());
    }
    
    public Collection<String> getExamples() {
        return BlockPredicateArgument.EXAMPLES;
    }
    
    static {
        EXAMPLES = Arrays.<String>asList("stone", "minecraft:stone", "stone[foo=bar]", "#stone", "#stone[foo=bar]{baz=nbt}");
        final TranslatableComponent translatableComponent;
        ERROR_UNKNOWN_TAG = new DynamicCommandExceptionType(object -> {
            new TranslatableComponent("arguments.block.tag.unknown", new Object[] { object });
            return translatableComponent;
        });
    }
    
    static class BlockPredicate implements Predicate<BlockInWorld> {
        private final BlockState state;
        private final Set<Property<?>> properties;
        @Nullable
        private final CompoundTag nbt;
        
        public BlockPredicate(final BlockState cfj, final Set<Property<?>> set, @Nullable final CompoundTag le) {
            this.state = cfj;
            this.properties = set;
            this.nbt = le;
        }
        
        @Override
        public boolean test(final BlockInWorld cfn) {
            final BlockState cfj3 = cfn.getState();
            if (!cfj3.is(this.state.getBlock())) {
                return false;
            }
            for (final Property<?> cgl5 : this.properties) {
                if (cfj3.getValue(cgl5) != this.state.getValue(cgl5)) {
                    return false;
                }
            }
            if (this.nbt != null) {
                final BlockEntity cdl4 = cfn.getEntity();
                return cdl4 != null && NbtUtils.compareNbt(this.nbt, cdl4.save(new CompoundTag()), true);
            }
            return true;
        }
    }
    
    static class TagPredicate implements Predicate<BlockInWorld> {
        private final Tag<Block> tag;
        @Nullable
        private final CompoundTag nbt;
        private final Map<String, String> vagueProperties;
        
        private TagPredicate(final Tag<Block> adf, final Map<String, String> map, @Nullable final CompoundTag le) {
            this.tag = adf;
            this.vagueProperties = map;
            this.nbt = le;
        }
        
        @Override
        public boolean test(final BlockInWorld cfn) {
            final BlockState cfj3 = cfn.getState();
            if (!cfj3.is(this.tag)) {
                return false;
            }
            for (final Map.Entry<String, String> entry5 : this.vagueProperties.entrySet()) {
                final Property<?> cgl6 = cfj3.getBlock().getStateDefinition().getProperty(entry5.getKey());
                if (cgl6 == null) {
                    return false;
                }
                final Comparable<?> comparable7 = cgl6.getValue(entry5.getValue()).orElse(null);
                if (comparable7 == null) {
                    return false;
                }
                if (cfj3.getValue(cgl6) != comparable7) {
                    return false;
                }
            }
            if (this.nbt != null) {
                final BlockEntity cdl4 = cfn.getEntity();
                return cdl4 != null && NbtUtils.compareNbt(this.nbt, cdl4.save(new CompoundTag()), true);
            }
            return true;
        }
    }
    
    public interface Result {
        Predicate<BlockInWorld> create(final TagManager adh) throws CommandSyntaxException;
    }
}
