package net.minecraft.world.level.storage.loot;

import javax.annotation.Nullable;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.HashMultimap;
import java.util.Set;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.resources.ResourceLocation;
import java.util.function.Function;
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSet;
import java.util.function.Supplier;
import com.google.common.collect.Multimap;

public class ValidationContext {
    private final Multimap<String, String> problems;
    private final Supplier<String> context;
    private final LootContextParamSet params;
    private final Function<ResourceLocation, LootItemCondition> conditionResolver;
    private final Set<ResourceLocation> visitedConditions;
    private final Function<ResourceLocation, LootTable> tableResolver;
    private final Set<ResourceLocation> visitedTables;
    private String contextCache;
    
    public ValidationContext(final LootContextParamSet dcy, final Function<ResourceLocation, LootItemCondition> function2, final Function<ResourceLocation, LootTable> function3) {
        this(HashMultimap.create(), () -> "", dcy, function2, ImmutableSet.of(), function3, ImmutableSet.of());
    }
    
    public ValidationContext(final Multimap<String, String> multimap, final Supplier<String> supplier, final LootContextParamSet dcy, final Function<ResourceLocation, LootItemCondition> function4, final Set<ResourceLocation> set5, final Function<ResourceLocation, LootTable> function6, final Set<ResourceLocation> set7) {
        this.problems = multimap;
        this.context = supplier;
        this.params = dcy;
        this.conditionResolver = function4;
        this.visitedConditions = set5;
        this.tableResolver = function6;
        this.visitedTables = set7;
    }
    
    private String getContext() {
        if (this.contextCache == null) {
            this.contextCache = this.context.get();
        }
        return this.contextCache;
    }
    
    public void reportProblem(final String string) {
        this.problems.put(this.getContext(), string);
    }
    
    public ValidationContext forChild(final String string) {
        return new ValidationContext(this.problems, () -> this.getContext() + string, this.params, this.conditionResolver, this.visitedConditions, this.tableResolver, this.visitedTables);
    }
    
    public ValidationContext enterTable(final String string, final ResourceLocation uh) {
        final ImmutableSet<ResourceLocation> immutableSet4 = ImmutableSet.<ResourceLocation>builder().addAll(this.visitedTables).add(uh).build();
        return new ValidationContext(this.problems, () -> this.getContext() + string, this.params, this.conditionResolver, this.visitedConditions, this.tableResolver, immutableSet4);
    }
    
    public ValidationContext enterCondition(final String string, final ResourceLocation uh) {
        final ImmutableSet<ResourceLocation> immutableSet4 = ImmutableSet.<ResourceLocation>builder().addAll(this.visitedConditions).add(uh).build();
        return new ValidationContext(this.problems, () -> this.getContext() + string, this.params, this.conditionResolver, immutableSet4, this.tableResolver, this.visitedTables);
    }
    
    public boolean hasVisitedTable(final ResourceLocation uh) {
        return this.visitedTables.contains(uh);
    }
    
    public boolean hasVisitedCondition(final ResourceLocation uh) {
        return this.visitedConditions.contains(uh);
    }
    
    public Multimap<String, String> getProblems() {
        return ImmutableMultimap.copyOf(this.problems);
    }
    
    public void validateUser(final LootContextUser dau) {
        this.params.validateUser(this, dau);
    }
    
    @Nullable
    public LootTable resolveLootTable(final ResourceLocation uh) {
        return this.tableResolver.apply(uh);
    }
    
    @Nullable
    public LootItemCondition resolveCondition(final ResourceLocation uh) {
        return this.conditionResolver.apply(uh);
    }
    
    public ValidationContext setParams(final LootContextParamSet dcy) {
        return new ValidationContext(this.problems, this.context, dcy, this.conditionResolver, this.visitedConditions, this.tableResolver, this.visitedTables);
    }
}
