package net.minecraft.advancements;

import javax.annotation.Nullable;
import java.util.Iterator;
import com.google.gson.JsonElement;
import com.google.common.collect.Maps;
import java.util.Map;
import com.google.gson.JsonSyntaxException;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import net.minecraft.network.FriendlyByteBuf;

public class Criterion {
    private final CriterionTriggerInstance trigger;
    
    public Criterion(final CriterionTriggerInstance ac) {
        this.trigger = ac;
    }
    
    public Criterion() {
        this.trigger = null;
    }
    
    public void serializeToNetwork(final FriendlyByteBuf kv) {
    }
    
    public static Criterion criterionFromJson(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        final ResourceLocation sm3 = new ResourceLocation(GsonHelper.getAsString(jsonObject, "trigger"));
        final CriterionTrigger<?> ab4 = CriteriaTriggers.getCriterion(sm3);
        if (ab4 == null) {
            throw new JsonSyntaxException("Invalid criterion trigger: " + sm3);
        }
        final CriterionTriggerInstance ac5 = (CriterionTriggerInstance)ab4.createInstance(GsonHelper.getAsJsonObject(jsonObject, "conditions", new JsonObject()), jsonDeserializationContext);
        return new Criterion(ac5);
    }
    
    public static Criterion criterionFromNetwork(final FriendlyByteBuf kv) {
        return new Criterion();
    }
    
    public static Map<String, Criterion> criteriaFromJson(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
        final Map<String, Criterion> map3 = Maps.newHashMap();
        for (final Map.Entry<String, JsonElement> entry5 : jsonObject.entrySet()) {
            map3.put(entry5.getKey(), criterionFromJson(GsonHelper.convertToJsonObject(entry5.getValue(), "criterion"), jsonDeserializationContext));
        }
        return map3;
    }
    
    public static Map<String, Criterion> criteriaFromNetwork(final FriendlyByteBuf kv) {
        final Map<String, Criterion> map2 = Maps.newHashMap();
        for (int integer3 = kv.readVarInt(), integer4 = 0; integer4 < integer3; ++integer4) {
            map2.put(kv.readUtf(32767), criterionFromNetwork(kv));
        }
        return map2;
    }
    
    public static void serializeToNetwork(final Map<String, Criterion> map, final FriendlyByteBuf kv) {
        kv.writeVarInt(map.size());
        for (final Map.Entry<String, Criterion> entry4 : map.entrySet()) {
            kv.writeUtf(entry4.getKey());
            entry4.getValue().serializeToNetwork(kv);
        }
    }
    
    @Nullable
    public CriterionTriggerInstance getTrigger() {
        return this.trigger;
    }
    
    public JsonElement serializeToJson() {
        final JsonObject jsonObject2 = new JsonObject();
        jsonObject2.addProperty("trigger", this.trigger.getCriterion().toString());
        jsonObject2.add("conditions", this.trigger.serializeToJson());
        return jsonObject2;
    }
}
