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 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 dv3 = new BlockStateParser(stringReader, true).parse(true);
        if (dv3.getState() != null) {
            final BlockPredicate a4 = new BlockPredicate(dv3.getState(), dv3.getProperties().keySet(), dv3.getNbt());
            return abb -> a4;
        }
        final ResourceLocation sm4 = dv3.getTag();
        final ResourceLocation sm5;
        final Tag<Object> aaz4;
        final BlockStateParser blockStateParser;
        return abb -> {
            aaz4 = abb.getBlocks().getTag(sm5);
            if (aaz4 == null) {
                throw BlockPredicateArgument.ERROR_UNKNOWN_TAG.create(sm5.toString());
            }
            else {
                return new TagPredicate((Tag)aaz4, (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 dv5 = new BlockStateParser(stringReader4, true);
        try {
            dv5.parse(true);
        }
        catch (CommandSyntaxException ex) {}
        return dv5.fillSuggestions(suggestionsBuilder);
    }
    
    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 byj, final Set<Property<?>> set, @Nullable final CompoundTag jt) {
            this.state = byj;
            this.properties = set;
            this.nbt = jt;
        }
        
        @Override
        public boolean test(final BlockInWorld byn) {
            final BlockState byj3 = byn.getState();
            if (byj3.getBlock() != this.state.getBlock()) {
                return false;
            }
            for (final Property<?> bzm5 : this.properties) {
                if (byj3.getValue(bzm5) != this.state.getValue(bzm5)) {
                    return false;
                }
            }
            if (this.nbt != null) {
                final BlockEntity bwl4 = byn.getEntity();
                return bwl4 != null && NbtUtils.compareNbt(this.nbt, bwl4.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> aaz, final Map<String, String> map, @Nullable final CompoundTag jt) {
            this.tag = aaz;
            this.vagueProperties = map;
            this.nbt = jt;
        }
        
        @Override
        public boolean test(final BlockInWorld byn) {
            final BlockState byj3 = byn.getState();
            if (!byj3.is(this.tag)) {
                return false;
            }
            for (final Map.Entry<String, String> entry5 : this.vagueProperties.entrySet()) {
                final Property<?> bzm6 = byj3.getBlock().getStateDefinition().getProperty(entry5.getKey());
                if (bzm6 == null) {
                    return false;
                }
                final Comparable<?> comparable7 = bzm6.getValue(entry5.getValue()).orElse(null);
                if (comparable7 == null) {
                    return false;
                }
                if (byj3.getValue(bzm6) != comparable7) {
                    return false;
                }
            }
            if (this.nbt != null) {
                final BlockEntity bwl4 = byn.getEntity();
                return bwl4 != null && NbtUtils.compareNbt(this.nbt, bwl4.save(new CompoundTag()), true);
            }
            return true;
        }
    }
    
    public interface Result {
        Predicate<BlockInWorld> create(final TagManager abb) throws CommandSyntaxException;
    }
}
