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.world.level.storage.loot.Serializer;
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 LootItemConditionType getType() {
        return LootItemConditions.ENTITY_SCORES;
    }
    
    @Override
    public Set<LootContextParam<?>> getReferencedContextParams() {
        return ImmutableSet.of(this.entityTarget.getParam());
    }
    
    @Override
    public boolean test(final LootContext dat) {
        final Entity aom3 = dat.<Entity>getParamOrNull(this.entityTarget.getParam());
        if (aom3 == null) {
            return false;
        }
        final Scoreboard dfm4 = aom3.level.getScoreboard();
        for (final Map.Entry<String, RandomValueBounds> entry6 : this.scores.entrySet()) {
            if (!this.hasScore(aom3, dfm4, entry6.getKey(), entry6.getValue())) {
                return false;
            }
        }
        return true;
    }
    
    protected boolean hasScore(final Entity aom, final Scoreboard dfm, final String string, final RandomValueBounds dbb) {
        final Objective dfj6 = dfm.getObjective(string);
        if (dfj6 == null) {
            return false;
        }
        final String string2 = aom.getScoreboardName();
        return dfm.hasPlayerScore(string2, dfj6) && dbb.matchesValue(dfm.getOrCreatePlayerScore(string2, dfj6).getScore());
    }
    
    public static class Serializer implements net.minecraft.world.level.storage.loot.Serializer<EntityHasScoreCondition> {
        @Override
        public void serialize(final JsonObject jsonObject, final EntityHasScoreCondition ddh, final JsonSerializationContext jsonSerializationContext) {
            final JsonObject jsonObject2 = new JsonObject();
            for (final Map.Entry<String, RandomValueBounds> entry7 : ddh.scores.entrySet()) {
                jsonObject2.add(entry7.getKey(), jsonSerializationContext.serialize(entry7.getValue()));
            }
            jsonObject.add("scores", jsonObject2);
            jsonObject.add("entity", jsonSerializationContext.serialize(ddh.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);
        }
    }
}
