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

import com.google.common.collect.Maps;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.scores.Objective;
import java.util.Iterator;
import net.minecraft.world.scores.Scoreboard;
import net.minecraft.world.entity.Entity;
import com.google.common.collect.ImmutableSet;
import net.minecraft.world.level.storage.loot.parameters.LootContextParam;
import java.util.Set;
import com.google.common.collect.ImmutableMap;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.RandomValueBounds;
import java.util.Map;

public class EntityHasScoreCondition implements LootItemCondition {
    private final Map<String, RandomValueBounds> scores;
    private final LootContext.EntityTarget entityTarget;
    
    private EntityHasScoreCondition(final Map<String, RandomValueBounds> map, final LootContext.EntityTarget c) {
        this.scores = ImmutableMap.copyOf(map);
        this.entityTarget = c;
    }
    
    @Override
    public Set<LootContextParam<?>> getReferencedContextParams() {
        return ImmutableSet.of(this.entityTarget.getParam());
    }
    
    @Override
    public boolean test(final LootContext crx) {
        final Entity akq3 = crx.<Entity>getParamOrNull(this.entityTarget.getParam());
        if (akq3 == null) {
            return false;
        }
        final Scoreboard cwl4 = akq3.level.getScoreboard();
        for (final Map.Entry<String, RandomValueBounds> entry6 : this.scores.entrySet()) {
            if (!this.hasScore(akq3, cwl4, entry6.getKey(), entry6.getValue())) {
                return false;
            }
        }
        return true;
    }
    
    protected boolean hasScore(final Entity akq, final Scoreboard cwl, final String string, final RandomValueBounds csf) {
        final Objective cwi6 = cwl.getObjective(string);
        if (cwi6 == null) {
            return false;
        }
        final String string2 = akq.getScoreboardName();
        return cwl.hasPlayerScore(string2, cwi6) && csf.matchesValue(cwl.getOrCreatePlayerScore(string2, cwi6).getScore());
    }
    
    public static class Serializer extends LootItemCondition.Serializer<EntityHasScoreCondition> {
        protected Serializer() {
            super(new ResourceLocation("entity_scores"), EntityHasScoreCondition.class);
        }
        
        @Override
        public void serialize(final JsonObject jsonObject, final EntityHasScoreCondition cuh, final JsonSerializationContext jsonSerializationContext) {
            final JsonObject jsonObject2 = new JsonObject();
            for (final Map.Entry<String, RandomValueBounds> entry7 : cuh.scores.entrySet()) {
                jsonObject2.add(entry7.getKey(), jsonSerializationContext.serialize(entry7.getValue()));
            }
            jsonObject.add("scores", jsonObject2);
            jsonObject.add("entity", jsonSerializationContext.serialize(cuh.entityTarget));
        }
        
        @Override
        public EntityHasScoreCondition deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
            final Set<Map.Entry<String, JsonElement>> set4 = GsonHelper.getAsJsonObject(jsonObject, "scores").entrySet();
            final Map<String, RandomValueBounds> map5 = Maps.newLinkedHashMap();
            for (final Map.Entry<String, JsonElement> entry7 : set4) {
                map5.put(entry7.getKey(), GsonHelper.<RandomValueBounds>convertToObject((JsonElement)entry7.getValue(), "score", jsonDeserializationContext, RandomValueBounds.class));
            }
            return new EntityHasScoreCondition(map5, GsonHelper.<LootContext.EntityTarget>getAsObject(jsonObject, "entity", jsonDeserializationContext, LootContext.EntityTarget.class), null);
        }
    }
}
