package net.minecraft.advancements;

import java.text.ParseException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.JsonNull;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonElement;
import net.minecraft.network.FriendlyByteBuf;
import java.util.Date;
import java.text.SimpleDateFormat;

public class CriterionProgress {
    private static final SimpleDateFormat DATE_FORMAT;
    private Date obtained;
    
    public boolean isDone() {
        return this.obtained != null;
    }
    
    public void grant() {
        this.obtained = new Date();
    }
    
    public void revoke() {
        this.obtained = null;
    }
    
    public Date getObtained() {
        return this.obtained;
    }
    
    @Override
    public String toString() {
        return "CriterionProgress{obtained=" + ((this.obtained == null) ? "false" : this.obtained) + '}';
    }
    
    public void serializeToNetwork(final FriendlyByteBuf kv) {
        kv.writeBoolean(this.obtained != null);
        if (this.obtained != null) {
            kv.writeDate(this.obtained);
        }
    }
    
    public JsonElement serializeToJson() {
        if (this.obtained != null) {
            return new JsonPrimitive(CriterionProgress.DATE_FORMAT.format(this.obtained));
        }
        return JsonNull.INSTANCE;
    }
    
    public static CriterionProgress fromNetwork(final FriendlyByteBuf kv) {
        final CriterionProgress aa2 = new CriterionProgress();
        if (kv.readBoolean()) {
            aa2.obtained = kv.readDate();
        }
        return aa2;
    }
    
    public static CriterionProgress fromJson(final String string) {
        final CriterionProgress aa2 = new CriterionProgress();
        try {
            aa2.obtained = CriterionProgress.DATE_FORMAT.parse(string);
        }
        catch (ParseException parseException3) {
            throw new JsonSyntaxException("Invalid datetime: " + string, parseException3);
        }
        return aa2;
    }
    
    static {
        DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
    }
}
