package net.minecraft.advancements;

import net.minecraft.advancements.critereon.SerializationContext;
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 net.minecraft.advancements.critereon.DeserializationContext;
import com.google.gson.JsonObject;
import net.minecraft.network.FriendlyByteBuf;

public class Criterion {
    private final CriterionTriggerInstance trigger;
    
    public Criterion(final CriterionTriggerInstance ae) {
        this.trigger = ae;
    }
    
    public Criterion() {
        this.trigger = null;
    }
    
    public void serializeToNetwork(final FriendlyByteBuf mg) {
    }
    
    public static Criterion criterionFromJson(final JsonObject jsonObject, final DeserializationContext av) {
        final ResourceLocation uh3 = new ResourceLocation(GsonHelper.getAsString(jsonObject, "trigger"));
        final CriterionTrigger<?> ad4 = CriteriaTriggers.getCriterion(uh3);
        if (ad4 == null) {
            throw new JsonSyntaxException("Invalid criterion trigger: " + uh3);
        }
        final CriterionTriggerInstance ae5 = (CriterionTriggerInstance)ad4.createInstance(GsonHelper.getAsJsonObject(jsonObject, "conditions", new JsonObject()), av);
        return new Criterion(ae5);
    }
    
    public static Criterion criterionFromNetwork(final FriendlyByteBuf mg) {
        return new Criterion();
    }
    
    public static Map<String, Criterion> criteriaFromJson(final JsonObject jsonObject, final DeserializationContext av) {
        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"), av));
        }
        return map3;
    }
    
    public static Map<String, Criterion> criteriaFromNetwork(final FriendlyByteBuf mg) {
        final Map<String, Criterion> map2 = Maps.newHashMap();
        for (int integer3 = mg.readVarInt(), integer4 = 0; integer4 < integer3; ++integer4) {
            map2.put(mg.readUtf(32767), criterionFromNetwork(mg));
        }
        return map2;
    }
    
    public static void serializeToNetwork(final Map<String, Criterion> map, final FriendlyByteBuf mg) {
        mg.writeVarInt(map.size());
        for (final Map.Entry<String, Criterion> entry4 : map.entrySet()) {
            mg.writeUtf(entry4.getKey());
            entry4.getValue().serializeToNetwork(mg);
        }
    }
    
    @Nullable
    public CriterionTriggerInstance getTrigger() {
        return this.trigger;
    }
    
    public JsonElement serializeToJson() {
        final JsonObject jsonObject2 = new JsonObject();
        jsonObject2.addProperty("trigger", this.trigger.getCriterion().toString());
        final JsonObject jsonObject3 = this.trigger.serializeToJson(SerializationContext.INSTANCE);
        if (jsonObject3.size() != 0) {
            jsonObject2.add("conditions", jsonObject3);
        }
        return jsonObject2;
    }
}
