package com.ss.android.ugc.effectmanager.knadapt;

import X.M12;
import X.Q8P;
import android.accounts.NetworkErrorException;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.ss.android.ugc.effectmanager.common.EffectRequest;
import com.ss.android.ugc.effectmanager.common.exception.StatusCodeException;
import com.ss.android.ugc.effectmanager.common.listener.IJsonConverter;
import com.ss.android.ugc.effectmanager.common.network.EffectNetWorkerWrapper;
import com.ss.android.ugc.effectmanager.common.utils.CloseUtil;
import com.ss.android.ugc.effectmanager.common.utils.NetworkUtils;
import java.io.InputStream;
import kotlin.h.b.n;
import org.json.JSONException;

public final class AdapterExtKt {
    static {
        Covode.recordClassIndex(125525);
    }

    public static final <T extends Q8P<?>> T execute(EffectNetWorkerWrapper effectNetWorkerWrapper, EffectRequest effectRequest, IJsonConverter iJsonConverter, Class<T> cls) {
        String str;
        M12.LIZ(effectNetWorkerWrapper, iJsonConverter);
        InputStream execute = effectNetWorkerWrapper.getIEffectNetWorker().execute(effectRequest);
        if (execute != null) {
            T t = (Q8P) iJsonConverter.convertJsonToObj(execute, cls);
            CloseUtil.close(execute);
            if (t != null) {
                int statusCode = t.getStatusCode();
                if (statusCode == 0) {
                    return t;
                }
                throw new StatusCodeException(statusCode, t.getResponseMessage());
            }
            throw new JSONException("Json convert fail");
        } else if (NetworkUtils.isNetworkAvailable(effectNetWorkerWrapper.getContext())) {
            String str2 = null;
            if (effectRequest != null) {
                str = effectRequest.getErrorMsg();
            } else {
                str = null;
            }
            if (!TextUtils.isEmpty(str)) {
                if (effectRequest != null) {
                    str2 = effectRequest.getErrorMsg();
                }
                throw new NetworkErrorException(str2);
            }
            throw new NetworkErrorException("Download error");
        } else {
            throw new Exception("network unavailable");
        }
    }

    public static final <T extends Q8P<?>> T parse(EffectNetWorkerWrapper effectNetWorkerWrapper, EffectRequest effectRequest, InputStream inputStream, IJsonConverter iJsonConverter, Class<T> cls) {
        M12.LIZ(effectNetWorkerWrapper, iJsonConverter);
        if (inputStream == null) {
            n.LIZ();
        }
        T t = (Q8P) iJsonConverter.convertJsonToObj(inputStream, cls);
        if (t != null) {
            int statusCode = t.getStatusCode();
            if (statusCode == 0) {
                return t;
            }
            throw new StatusCodeException(statusCode, t.getResponseMessage());
        }
        throw new JSONException("Json convert fail");
    }
}
