package com.seewo.sdk.util;

import android.text.TextUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seewo.sdk.internal.model.SDKParsable;
import com.seewo.sdk.internal.model.SDKResponse;
import com.seewo.sdk.internal.response.common.RespBooleanResult;
import com.seewo.sdk.internal.response.common.RespByteArrayResult;
import com.seewo.sdk.internal.response.common.RespFloatResult;
import com.seewo.sdk.internal.response.common.RespIntArrayResult;
import com.seewo.sdk.internal.response.common.RespIntegerResult;
import com.seewo.sdk.internal.response.common.RespStringArrayResult;
import com.seewo.sdk.internal.response.common.RespStringResult;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public class ParseUtil {
    private static final String TAG = "ParseUtil";

    private ParseUtil() {
    }

    public static String getClassName(SDKParsable sDKParsable) {
        return sDKParsable.getClass().getName();
    }

    public static String encodeJSON(Object obj) {
        return JSON.toJSONString(obj);
    }

    public static <T> T decodeJSON(String str, Class<T> cls) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        return (T) JSON.parseObject(str, cls);
    }

    public static <T> T decodeJSONObject(Object obj, Class<T> cls) {
        if (obj == null || !(obj instanceof JSONObject)) {
            return null;
        }
        return (T) ((JSONObject) obj).toJavaObject((Class) cls);
    }

    public static <T> List<T> decodeJSONArray(Object obj, Class<T> cls) {
        if (obj == null || !(obj instanceof JSONArray)) {
            return null;
        }
        return ((JSONArray) obj).toJavaList(cls);
    }

    public static <T> T decodeJSON(SDKResponse sDKResponse, Class<T> cls) {
        if (sDKResponse == null) {
            return (T) createInstance(cls);
        }
        if (sDKResponse.getStatus() != SDKResponse.Status.SUCCESS) {
            RLog.e(TAG, "Response error! [ " + sDKResponse.getStatus() + " ]");
            return (T) createInstance(cls);
        }
        T t = (T) JSON.parseObject(sDKResponse.getParamsJson(), cls);
        return t == null ? (T) createInstance(cls) : t;
    }

    private static <T> T createInstance(Class<T> cls) {
        try {
            Constructor<T> declaredConstructor = cls.getDeclaredConstructor(new Class[0]);
            if (declaredConstructor == null) {
                return null;
            }
            declaredConstructor.setAccessible(true);
            return declaredConstructor.newInstance(new Object[0]);
        } catch (IllegalAccessException e) {
            RLog.e(TAG, e);
            return null;
        } catch (InstantiationException e2) {
            RLog.e(TAG, e2);
            return null;
        } catch (NoSuchMethodException e3) {
            RLog.e(TAG, e3);
            return null;
        } catch (InvocationTargetException e4) {
            RLog.e(TAG, e4);
            return null;
        }
    }

    public static String decodeStringResponse(SDKResponse sDKResponse) {
        RespStringResult respStringResult;
        if (sDKResponse == null || (respStringResult = (RespStringResult) decodeJSON(sDKResponse, RespStringResult.class)) == null) {
            return null;
        }
        return respStringResult.getResult();
    }

    public static boolean decodeBooleanResponse(SDKResponse sDKResponse) {
        RespBooleanResult respBooleanResult;
        return (sDKResponse == null || (respBooleanResult = (RespBooleanResult) decodeJSON(sDKResponse, RespBooleanResult.class)) == null || !respBooleanResult.getResult()) ? false : true;
    }

    public static int decodeIntResponse(SDKResponse sDKResponse) {
        RespIntegerResult respIntegerResult;
        if (sDKResponse == null || (respIntegerResult = (RespIntegerResult) decodeJSON(sDKResponse, RespIntegerResult.class)) == null) {
            return -1;
        }
        return respIntegerResult.result;
    }

    public static float decodeFloatResponse(SDKResponse sDKResponse) {
        RespFloatResult respFloatResult;
        if (sDKResponse == null || (respFloatResult = (RespFloatResult) decodeJSON(sDKResponse, RespFloatResult.class)) == null) {
            return -1.0f;
        }
        return respFloatResult.getResult();
    }

    public static int[] decodeIntArrayResponse(SDKResponse sDKResponse) {
        RespIntArrayResult respIntArrayResult;
        if (sDKResponse == null || (respIntArrayResult = (RespIntArrayResult) decodeJSON(sDKResponse, RespIntArrayResult.class)) == null) {
            return null;
        }
        return respIntArrayResult.result;
    }

    public static byte[] decodeByteArrayResponse(SDKResponse sDKResponse) {
        RespByteArrayResult respByteArrayResult;
        if (sDKResponse == null || (respByteArrayResult = (RespByteArrayResult) decodeJSON(sDKResponse, RespByteArrayResult.class)) == null) {
            return null;
        }
        return respByteArrayResult.getResult();
    }

    public static String[] decodeStringArrayResponse(SDKResponse sDKResponse) {
        RespStringArrayResult respStringArrayResult;
        if (sDKResponse == null || (respStringArrayResult = (RespStringArrayResult) decodeJSON(sDKResponse, RespStringArrayResult.class)) == null) {
            return null;
        }
        return respStringArrayResult.getResult();
    }

    public static String toJSONString(Object obj) {
        return JSON.toJSONString(obj);
    }
}
