package com.parse;

import android.util.Base64;
import com.parse.ParseQuery;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import p138.p139.p143.p144.C2803;

/* JADX INFO: loaded from: classes.dex */
public abstract class ParseEncoder {
    public static boolean isValidType(Object obj) {
        return (obj instanceof String) || (obj instanceof Number) || (obj instanceof Boolean) || (obj instanceof Date) || (obj instanceof List) || (obj instanceof Map) || (obj instanceof byte[]) || obj == JSONObject.NULL || (obj instanceof ParseObject) || (obj instanceof ParseACL) || (obj instanceof ParseFile) || (obj instanceof ParseGeoPoint) || (obj instanceof ParsePolygon) || (obj instanceof ParseRelation);
    }

    public Object encode(Object obj) {
        try {
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        if (obj instanceof ParseObject) {
            return encodeRelatedObject((ParseObject) obj);
        }
        if (obj instanceof ParseQuery.State.Builder) {
            return encode(((ParseQuery.State.Builder) obj).build());
        }
        if (obj instanceof ParseQuery.State) {
            return ((ParseQuery.State) obj).toJSON(this);
        }
        if (obj instanceof Date) {
            JSONObject jSONObject = new JSONObject();
            String str = ParseDateFormat.INSTANCE.format((Date) obj);
            try {
                jSONObject.put("__type", "Date");
                jSONObject.put("iso", str);
                return jSONObject;
            } catch (JSONException e2) {
                throw new RuntimeException(e2);
            }
        }
        if (obj instanceof byte[]) {
            JSONObject jSONObject2 = new JSONObject();
            jSONObject2.put("__type", "Bytes");
            jSONObject2.put("base64", Base64.encodeToString((byte[]) obj, 2));
            return jSONObject2;
        }
        if (obj instanceof ParseFile) {
            return ((ParseFile) obj).encode();
        }
        if (obj instanceof ParseGeoPoint) {
            ParseGeoPoint parseGeoPoint = (ParseGeoPoint) obj;
            JSONObject jSONObject3 = new JSONObject();
            jSONObject3.put("__type", "GeoPoint");
            jSONObject3.put("latitude", parseGeoPoint.latitude);
            jSONObject3.put("longitude", parseGeoPoint.longitude);
            return jSONObject3;
        }
        if (obj instanceof ParsePolygon) {
            JSONObject jSONObject4 = new JSONObject();
            jSONObject4.put("__type", "Polygon");
            jSONObject4.put("coordinates", ((ParsePolygon) obj).coordinatesToJSONArray());
            return jSONObject4;
        }
        if (obj instanceof ParseACL) {
            return ((ParseACL) obj).toJSONObject(this);
        }
        if (obj instanceof Map) {
            JSONObject jSONObject5 = new JSONObject();
            for (Map.Entry entry : ((Map) obj).entrySet()) {
                jSONObject5.put((String) entry.getKey(), encode(entry.getValue()));
            }
            return jSONObject5;
        }
        if (obj instanceof Collection) {
            JSONArray jSONArray = new JSONArray();
            Iterator it = ((Collection) obj).iterator();
            while (it.hasNext()) {
                jSONArray.put(encode(it.next()));
            }
            return jSONArray;
        }
        if (obj instanceof ParseRelation) {
            return ((ParseRelation) obj).encodeToJSON(this);
        }
        if (obj instanceof ParseFieldOperation) {
            return ((ParseFieldOperation) obj).encode(this);
        }
        if (!(obj instanceof ParseQuery.RelationConstraint)) {
            if (obj == null) {
                return JSONObject.NULL;
            }
            if (isValidType(obj)) {
                return obj;
            }
            StringBuilder sbM4889 = C2803.m4889("invalid type for ParseObject: ");
            sbM4889.append(obj.getClass().toString());
            throw new IllegalArgumentException(sbM4889.toString());
        }
        if (((ParseQuery.RelationConstraint) obj) == null) {
            throw null;
        }
        JSONObject jSONObject6 = new JSONObject();
        try {
            jSONObject6.put("key", (Object) null);
            jSONObject6.put("object", encodeRelatedObject(null));
            return jSONObject6;
        } catch (JSONException e3) {
            throw new RuntimeException(e3);
        }
        throw new RuntimeException(e);
    }

    public abstract JSONObject encodeRelatedObject(ParseObject parseObject);
}
