package com.bytedance.retrofit2;

import X.0HH;
import X.3Ld;
import X.AnonymousClass8BD;
import X.AnonymousClass8X0;
import X.C21868Wz;
import X.C23858bw;
import X.C28338jA;
import X.C50759eE;
import X.C51029ef;
import X.C52059gK;
import X.C52139gS;
import X.C52149gT;
import X.C52159gU;
import X.C52169gV;
import X.C52729hP;
import X.C53919jK;
import X.JLw;
import X.QWB;
import android.os.Looper;
import android.os.SystemClock;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.retrofit2.client.Request;
import com.bytedance.retrofit2.mime.TypedInput;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executor;

public class SsHttpCall<T> implements JLw<T>, C21868Wz, AnonymousClass8X0 {
    public static C52159gU sCallMonitor;
    public static C52149gT sReqLevelControl;
    public static C52169gV sThrottleControl;
    public long appCallTime;
    public final Object[] args;
    public final CallServerInterceptor callServerInterceptor;
    public final C52059gK httpCallMetrics;
    public int mReqControlLevel = -1;
    public Request originalRequest;
    public Throwable preBuildURLException;
    public final C52729hP<T> requestFactory;

    static {
        Covode.recordClassIndex(36968);
    }

    public C51029ef execute() {
        return com_bytedance_retrofit2_SsHttpCall_com_ss_android_ugc_aweme_net_lancet_NetIOCheckLancet_execute(this);
    }

    public C52059gK getRetrofitMetrics() {
        return this.httpCallMetrics;
    }

    public void cancel() {
        CallServerInterceptor callServerInterceptor2 = this.callServerInterceptor;
        if (callServerInterceptor2 != null) {
            callServerInterceptor2.cancel();
        }
    }

    public void doCollect() {
        CallServerInterceptor callServerInterceptor2 = this.callServerInterceptor;
        if (callServerInterceptor2 != null) {
            callServerInterceptor2.doCollect();
        }
    }

    public SsHttpCall<T> clone() {
        return new SsHttpCall<>(this.requestFactory, this.args);
    }

    public Object getRequestInfo() {
        CallServerInterceptor callServerInterceptor2 = this.callServerInterceptor;
        if (callServerInterceptor2 != null) {
            return callServerInterceptor2.getRequestInfo();
        }
        return null;
    }

    public boolean isCanceled() {
        CallServerInterceptor callServerInterceptor2 = this.callServerInterceptor;
        if (callServerInterceptor2 == null || !callServerInterceptor2.isCanceled()) {
            return false;
        }
        return true;
    }

    public synchronized boolean isExecuted() {
        MethodCollector.i(8634);
        CallServerInterceptor callServerInterceptor2 = this.callServerInterceptor;
        if (callServerInterceptor2 == null || !callServerInterceptor2.isExecuted()) {
            MethodCollector.o(8634);
            return false;
        }
        MethodCollector.o(8634);
        return true;
    }

    public C51029ef getResponseWithInterceptorChain() {
        C52059gK r6 = this.httpCallMetrics;
        r6.LJIIZILJ = SystemClock.uptimeMillis();
        LinkedList linkedList = new LinkedList();
        linkedList.addAll(this.requestFactory.LIZJ);
        linkedList.add(this.callServerInterceptor);
        r6.LJII = this.appCallTime;
        r6.LJIIIIZZ = System.currentTimeMillis();
        this.originalRequest.setMetrics(r6);
        C51029ef LIZ = new C23858bw(linkedList, 0, this.originalRequest, this, r6).LIZ(this.originalRequest);
        LIZ.LIZLLL = r6;
        return LIZ;
    }

    public Request request() {
        Request request;
        CallServerInterceptor callServerInterceptor2 = this.callServerInterceptor;
        if (callServerInterceptor2 != null && (request = callServerInterceptor2.request()) != null) {
            return request;
        }
        if (this.originalRequest == null) {
            try {
                C52059gK r2 = this.httpCallMetrics;
                r2.LJIJ = SystemClock.uptimeMillis();
                this.originalRequest = this.requestFactory.LIZ(this.args);
                r2.LJIJI = SystemClock.uptimeMillis();
            } catch (RuntimeException e) {
                throw e;
            } catch (IOException e2) {
                throw new RuntimeException("Unable to create request.", e2);
            }
        }
        return this.originalRequest;
    }

    public C51029ef<T> com_bytedance_retrofit2_SsHttpCall__execute$___twin___() {
        int i;
        Request request;
        String str;
        Request request2;
        MethodCollector.i(8452);
        C52059gK r4 = this.httpCallMetrics;
        r4.LJIILL = SystemClock.uptimeMillis();
        this.appCallTime = System.currentTimeMillis();
        r4.LJIJ = SystemClock.uptimeMillis();
        try {
            this.originalRequest = this.requestFactory.LIZ(this.args);
            r4.LJIJI = SystemClock.uptimeMillis();
            C52149gT r0 = sReqLevelControl;
            if (r0 != null && r0.LIZ() && (request2 = this.originalRequest) != null && !TextUtils.isEmpty(request2.getPath())) {
                int LIZ = sReqLevelControl.LIZ(this.originalRequest.getPath());
                this.mReqControlLevel = LIZ;
                if (LIZ == 2) {
                    cancel();
                    IOException iOException = new IOException("Canceled by Requset Controller");
                    MethodCollector.o(8452);
                    throw iOException;
                } else if (LIZ == 1) {
                    sReqLevelControl.LIZIZ();
                }
            }
            C52169gV r2 = sThrottleControl;
            if (r2 != null && this.mReqControlLevel == -1) {
                if (r2.LIZ()) {
                    Request request3 = this.originalRequest;
                    if (request3 != null && !TextUtils.isEmpty(request3.getPath())) {
                        i = sThrottleControl.LIZ(this.originalRequest.getPath());
                    }
                    i = 0;
                } else {
                    if (sThrottleControl.LIZIZ() && (request = this.originalRequest) != null) {
                        List<AnonymousClass8BD> headers = request.headers("x-tt-request-tag");
                        if (headers == null || headers.size() <= 0 || TextUtils.isEmpty(headers.get(0).LIZIZ)) {
                            str = "";
                        } else {
                            str = headers.get(0).LIZIZ;
                        }
                        i = sThrottleControl.LIZ(this.originalRequest.getUrl(), str);
                    }
                    i = 0;
                }
                long j = (long) i;
                r4.LJIILLIIL = j;
                Thread.sleep(j);
            }
            try {
                C51029ef<T> responseWithInterceptorChain = getResponseWithInterceptorChain();
                C52149gT r02 = sReqLevelControl;
                if (r02 != null && r02.LIZ()) {
                    sReqLevelControl.LIZ(this.mReqControlLevel);
                }
                r4.LJJJJJL = SystemClock.uptimeMillis();
                if (!this.requestFactory.LJII) {
                    reportRequestOk(responseWithInterceptorChain, false);
                }
                MethodCollector.o(8452);
                return responseWithInterceptorChain;
            } catch (Throwable th) {
                r4.LJJJJJL = SystemClock.uptimeMillis();
                reportRequestError(th, false);
                MethodCollector.o(8452);
                throw th;
            }
        } catch (Exception e) {
            MethodCollector.o(8452);
            throw e;
        }
    }

    public static void setCallMonitor(C52159gU r0) {
        sCallMonitor = r0;
    }

    public static void setReqLevelControl(C52149gT r0) {
        sReqLevelControl = r0;
    }

    public static void setThrottleControl(C52169gV r0) {
        sThrottleControl = r0;
    }

    public T toResponseBody(TypedInput typedInput) {
        return C52729hP.LIZ(this.requestFactory, typedInput);
    }

    public boolean setThrottleNetSpeed(long j) {
        CallServerInterceptor callServerInterceptor2 = this.callServerInterceptor;
        if (callServerInterceptor2 != null) {
            return callServerInterceptor2.setThrottleNetSpeed(j);
        }
        return false;
    }

    public static C51029ef com_bytedance_retrofit2_SsHttpCall_com_ss_android_ugc_aweme_net_lancet_NetIOCheckLancet_execute(SsHttpCall ssHttpCall) {
        String str;
        if (Thread.currentThread() != Looper.getMainLooper().getThread()) {
            return ssHttpCall.com_bytedance_retrofit2_SsHttpCall__execute$___twin___();
        }
        long currentTimeMillis = System.currentTimeMillis();
        C51029ef com_bytedance_retrofit2_SsHttpCall__execute$___twin___ = ssHttpCall.com_bytedance_retrofit2_SsHttpCall__execute$___twin___();
        long currentTimeMillis2 = System.currentTimeMillis() - currentTimeMillis;
        try {
            Field declaredField = C51029ef.class.getDeclaredField("LIZ");
            declaredField.setAccessible(true);
            str = ((C50759eE) declaredField.get(com_bytedance_retrofit2_SsHttpCall__execute$___twin___)).LIZ;
        } catch (Exception unused) {
            str = "";
        }
        C28338jA.LIZ("core_java_custom_exception_monitor", new Throwable(), "do network IO on UI thread，url=" + str + "，duration=" + ((currentTimeMillis2 / 100) * 100));
        return com_bytedance_retrofit2_SsHttpCall__execute$___twin___;
    }

    /* JADX WARNING: type inference failed for: r2v0, types: [com.bytedance.retrofit2.SsHttpCall$1, java.lang.Runnable] */
    /* JADX WARNING: type inference failed for: r0v10, types: [com.bytedance.retrofit2.SsHttpCall$2, java.lang.Runnable] */
    public void enqueue(final 3Ld<T> r7) {
        final C52139gS r3;
        Request request;
        final C52059gK r5 = this.httpCallMetrics;
        r5.LJIILJJIL = SystemClock.uptimeMillis();
        this.appCallTime = System.currentTimeMillis();
        Objects.requireNonNull(r7, "callback == null");
        CallServerInterceptor callServerInterceptor2 = this.callServerInterceptor;
        if (callServerInterceptor2 == null || !callServerInterceptor2.isExecuted()) {
            final Executor executor = this.requestFactory.LIZLLL;
            if (r7 instanceof C52139gS) {
                r3 = (C52139gS) r7;
            } else {
                r3 = null;
            }
            final ? r2 = new QWB() {
                static {
                    Covode.recordClassIndex(36969);
                }

                public final int LIZ() {
                    return SsHttpCall.this.requestFactory.LJ;
                }

                public final boolean LIZIZ() {
                    return SsHttpCall.this.requestFactory.LJII;
                }

                public final int LIZJ() {
                    String str;
                    int i = 0;
                    if (SsHttpCall.sThrottleControl != null) {
                        if (SsHttpCall.sThrottleControl.LIZ()) {
                            if (SsHttpCall.this.originalRequest != null && !TextUtils.isEmpty(SsHttpCall.this.originalRequest.getPath())) {
                                i = SsHttpCall.sThrottleControl.LIZ(SsHttpCall.this.originalRequest.getPath());
                            }
                        } else if (SsHttpCall.sThrottleControl.LIZIZ() && SsHttpCall.this.originalRequest != null) {
                            List<AnonymousClass8BD> headers = SsHttpCall.this.originalRequest.headers("x-tt-request-tag");
                            if (headers == null || headers.size() <= 0 || TextUtils.isEmpty(headers.get(0).LIZIZ)) {
                                str = "";
                            } else {
                                str = headers.get(0).LIZIZ;
                            }
                            i = SsHttpCall.sThrottleControl.LIZ(SsHttpCall.this.originalRequest.getUrl(), str);
                        }
                    }
                    r5.LJIILLIIL = (long) i;
                    return i;
                }

                public final void run() {
                    try {
                        if (SsHttpCall.this.preBuildURLException == null) {
                            if (SsHttpCall.this.originalRequest == null) {
                                r5.LJIJ = SystemClock.uptimeMillis();
                                SsHttpCall ssHttpCall = SsHttpCall.this;
                                ssHttpCall.originalRequest = ssHttpCall.requestFactory.LIZ(SsHttpCall.this.args);
                                r5.LJIJI = SystemClock.uptimeMillis();
                            }
                            C51029ef responseWithInterceptorChain = SsHttpCall.this.getResponseWithInterceptorChain();
                            if (SsHttpCall.sReqLevelControl != null && SsHttpCall.sReqLevelControl.LIZ()) {
                                SsHttpCall.sReqLevelControl.LIZ(SsHttpCall.this.mReqControlLevel);
                            }
                            r5.LJJJJIZL = SystemClock.uptimeMillis();
                            try {
                                r7.LIZ(SsHttpCall.this, responseWithInterceptorChain);
                                C52139gS r1 = r3;
                                if (r1 != null) {
                                    r1.LIZIZ(SsHttpCall.this, responseWithInterceptorChain);
                                }
                            } catch (Throwable unused) {
                            }
                            r5.LJJJJJ = SystemClock.uptimeMillis();
                            if (!LIZIZ()) {
                                SsHttpCall.this.reportRequestOk(responseWithInterceptorChain, true);
                                return;
                            }
                            return;
                        }
                        throw SsHttpCall.this.preBuildURLException;
                    } catch (Throwable unused2) {
                    }
                    r5.LJJJJJ = SystemClock.uptimeMillis();
                    SsHttpCall.this.reportRequestError(th, true);
                }
            };
            try {
                r5.LJIJ = SystemClock.uptimeMillis();
                this.originalRequest = this.requestFactory.LIZ(this.args);
                r5.LJIJI = SystemClock.uptimeMillis();
            } catch (IOException e) {
                0HH.LIZ(e);
            }
            C52149gT r0 = sReqLevelControl;
            if (r0 != null && r0.LIZ() && (request = this.originalRequest) != null && !TextUtils.isEmpty(request.getPath())) {
                int LIZ = sReqLevelControl.LIZ(this.originalRequest.getPath());
                this.mReqControlLevel = LIZ;
                if (LIZ == 2) {
                    cancel();
                    r7.LIZ(this, new IOException("Canceled by Requset Controller"));
                    return;
                } else if (LIZ == 1 && sReqLevelControl.LIZ(executor, r2)) {
                    return;
                }
            }
            C52169gV r02 = sThrottleControl;
            if (r02 == null || ((!r02.LIZ() && !sThrottleControl.LIZIZ()) || this.mReqControlLevel != -1)) {
                executor.execute(r2);
            } else {
                executor.execute(new QWB() {
                    static {
                        Covode.recordClassIndex(36970);
                    }

                    public final int LIZJ() {
                        return 0;
                    }

                    public final int LIZ() {
                        return SsHttpCall.this.requestFactory.LJ;
                    }

                    public final boolean LIZIZ() {
                        return SsHttpCall.this.requestFactory.LJII;
                    }

                    public final void run() {
                        try {
                            if (SsHttpCall.this.originalRequest == null) {
                                C52059gK r3 = SsHttpCall.this.httpCallMetrics;
                                r3.LJIJ = SystemClock.uptimeMillis();
                                SsHttpCall ssHttpCall = SsHttpCall.this;
                                ssHttpCall.originalRequest = ssHttpCall.requestFactory.LIZ(SsHttpCall.this.args);
                                r3.LJIJI = SystemClock.uptimeMillis();
                            }
                        } catch (Throwable th) {
                            SsHttpCall.this.preBuildURLException = th;
                        }
                        executor.execute(r2);
                    }
                });
            }
        } else {
            throw new IllegalStateException("Already executed.");
        }
    }

    public SsHttpCall(C52729hP<T> r5, Object[] objArr) {
        this.requestFactory = r5;
        this.args = objArr;
        this.callServerInterceptor = new CallServerInterceptor(r5);
        C52059gK r3 = r5.LJIILL;
        C52059gK r2 = new C52059gK();
        r2.LJJJJ = r3.LJJJJ;
        r2.LJI = r3.LJII;
        r2.LIZ = r3.LIZ;
        r2.LJIIL = r3.LJIIL;
        r2.LJIILIIL = r3.LJIILIIL;
        this.httpCallMetrics = r2;
    }

    public void reportRequestOk(C51029ef<T> r10, boolean z) {
        long j;
        if (sCallMonitor != null && !isCanceled()) {
            if (z) {
                j = this.requestFactory.LJIILL.LJIILJJIL;
            } else {
                j = this.requestFactory.LJIILL.LJIILL;
            }
            sCallMonitor.LIZ(SystemClock.uptimeMillis() - j, j, r10.LIZ.LIZ, r10.LIZ.LJI, r10.LIZ.LJFF);
        }
    }

    public void reportRequestError(Throwable th, boolean z) {
        long j;
        if (sCallMonitor != null) {
            if (z) {
                j = this.requestFactory.LJIILL.LJIILJJIL;
            } else {
                j = this.requestFactory.LJIILL.LJIILL;
            }
            long uptimeMillis = SystemClock.uptimeMillis() - j;
            Throwable th2 = th;
            if (th2 instanceof C53919jK) {
                C53919jK r1 = (C53919jK) th2;
                if (r1.needReport) {
                    if (r1.reportMonitorOk) {
                        sCallMonitor.LIZ(uptimeMillis, j, r1.url, r1.traceCode, r1.infoObj);
                    }
                    if (r1.reportMonitorError) {
                        sCallMonitor.LIZ(uptimeMillis, j, r1.url, r1.traceCode, r1.infoObj, th2);
                    }
                }
            } else if (sCallMonitor.LIZ()) {
                cancelNormalRequest(false, th2, true);
            }
        }
    }

    public void cancelNormalRequest(boolean z, Throwable th, boolean z2) {
        CallServerInterceptor callServerInterceptor2 = this.callServerInterceptor;
        if (callServerInterceptor2 != null) {
            callServerInterceptor2.cancelNormalRequest(z, th, z2);
        }
    }
}
