package l;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import l.z;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

/* JADX INFO: compiled from: OkHttpCall.java */
/* JADX INFO: loaded from: classes.dex */
public final class t<T> implements l.b<T> {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final a0 f5057b;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final Object[] f5058e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final Call.Factory f5059f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final j<ResponseBody, T> f5060g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public volatile boolean f5061h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    @GuardedBy("this")
    @Nullable
    public Call f5062i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    @GuardedBy("this")
    @Nullable
    public Throwable f5063j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    @GuardedBy("this")
    public boolean f5064k;

    /* JADX INFO: compiled from: OkHttpCall.java */
    public class a implements Callback {
        public final /* synthetic */ d a;

        public a(d dVar) {
            this.a = dVar;
        }

        @Override // okhttp3.Callback
        public void onFailure(Call call, IOException iOException) {
            try {
                this.a.a(t.this, iOException);
            } catch (Throwable th) {
                g0.o(th);
                th.printStackTrace();
            }
        }

        @Override // okhttp3.Callback
        public void onResponse(Call call, Response response) {
            try {
                try {
                    this.a.b(t.this, t.this.b(response));
                } catch (Throwable th) {
                    g0.o(th);
                    th.printStackTrace();
                }
            } catch (Throwable th2) {
                g0.o(th2);
                try {
                    this.a.a(t.this, th2);
                } catch (Throwable th3) {
                    g0.o(th3);
                    th3.printStackTrace();
                }
            }
        }
    }

    /* JADX INFO: compiled from: OkHttpCall.java */
    public static final class b extends ResponseBody {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final ResponseBody f5066b;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public final j.g f5067e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        @Nullable
        public IOException f5068f;

        /* JADX INFO: compiled from: OkHttpCall.java */
        public class a extends j.k {
            public a(j.b0 b0Var) {
                super(b0Var);
            }

            @Override // j.k, j.b0
            public long read(j.d dVar, long j2) throws IOException {
                try {
                    return super.read(dVar, j2);
                } catch (IOException e2) {
                    b.this.f5068f = e2;
                    throw e2;
                }
            }
        }

        public b(ResponseBody responseBody) {
            this.f5066b = responseBody;
            this.f5067e = f.a.y.a.t(new a(responseBody.source()));
        }

        @Override // okhttp3.ResponseBody, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            this.f5066b.close();
        }

        @Override // okhttp3.ResponseBody
        public long contentLength() {
            return this.f5066b.contentLength();
        }

        @Override // okhttp3.ResponseBody
        public MediaType contentType() {
            return this.f5066b.contentType();
        }

        @Override // okhttp3.ResponseBody
        public j.g source() {
            return this.f5067e;
        }
    }

    /* JADX INFO: compiled from: OkHttpCall.java */
    public static final class c extends ResponseBody {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        @Nullable
        public final MediaType f5070b;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public final long f5071e;

        public c(@Nullable MediaType mediaType, long j2) {
            this.f5070b = mediaType;
            this.f5071e = j2;
        }

        @Override // okhttp3.ResponseBody
        public long contentLength() {
            return this.f5071e;
        }

        @Override // okhttp3.ResponseBody
        public MediaType contentType() {
            return this.f5070b;
        }

        @Override // okhttp3.ResponseBody
        public j.g source() {
            throw new IllegalStateException("Cannot read raw response body of a converted body.");
        }
    }

    public t(a0 a0Var, Object[] objArr, Call.Factory factory, j<ResponseBody, T> jVar) {
        this.f5057b = a0Var;
        this.f5058e = objArr;
        this.f5059f = factory;
        this.f5060g = jVar;
    }

    public final Call a() throws IOException {
        HttpUrl httpUrlResolve;
        Call.Factory factory = this.f5059f;
        a0 a0Var = this.f5057b;
        Object[] objArr = this.f5058e;
        x<?>[] xVarArr = a0Var.f4984j;
        int length = objArr.length;
        if (length != xVarArr.length) {
            throw new IllegalArgumentException(d.b.a.a.a.s(d.b.a.a.a.C("Argument count (", length, ") doesn't match expected count ("), xVarArr.length, ")"));
        }
        z zVar = new z(a0Var.f4977c, a0Var.f4976b, a0Var.f4978d, a0Var.f4979e, a0Var.f4980f, a0Var.f4981g, a0Var.f4982h, a0Var.f4983i);
        if (a0Var.f4985k) {
            length--;
        }
        ArrayList arrayList = new ArrayList(length);
        for (int i2 = 0; i2 < length; i2++) {
            arrayList.add(objArr[i2]);
            xVarArr[i2].a(zVar, objArr[i2]);
        }
        HttpUrl.Builder builder = zVar.f5101f;
        if (builder != null) {
            httpUrlResolve = builder.build();
        } else {
            httpUrlResolve = zVar.f5099d.resolve(zVar.f5100e);
            if (httpUrlResolve == null) {
                StringBuilder sbB = d.b.a.a.a.B("Malformed URL. Base: ");
                sbB.append(zVar.f5099d);
                sbB.append(", Relative: ");
                sbB.append(zVar.f5100e);
                throw new IllegalArgumentException(sbB.toString());
            }
        }
        RequestBody aVar = zVar.m;
        if (aVar == null) {
            FormBody.Builder builder2 = zVar.f5107l;
            if (builder2 != null) {
                aVar = builder2.build();
            } else {
                MultipartBody.Builder builder3 = zVar.f5106k;
                if (builder3 != null) {
                    aVar = builder3.build();
                } else if (zVar.f5105j) {
                    aVar = RequestBody.create((MediaType) null, new byte[0]);
                }
            }
        }
        MediaType mediaType = zVar.f5104i;
        if (mediaType != null) {
            if (aVar != null) {
                aVar = new z.a(aVar, mediaType);
            } else {
                zVar.f5103h.add("Content-Type", mediaType.toString());
            }
        }
        Call callNewCall = factory.newCall(zVar.f5102g.url(httpUrlResolve).headers(zVar.f5103h.build()).method(zVar.f5098c, aVar).tag(n.class, new n(a0Var.a, arrayList)).build());
        Objects.requireNonNull(callNewCall, "Call.Factory returned null.");
        return callNewCall;
    }

    public b0<T> b(Response response) throws IOException {
        ResponseBody responseBodyBody = response.body();
        Response responseBuild = response.newBuilder().body(new c(responseBodyBody.contentType(), responseBodyBody.contentLength())).build();
        int iCode = responseBuild.code();
        if (iCode < 200 || iCode >= 300) {
            try {
                ResponseBody responseBodyA = g0.a(responseBodyBody);
                Objects.requireNonNull(responseBodyA, "body == null");
                if (responseBuild.isSuccessful()) {
                    throw new IllegalArgumentException("rawResponse should not be successful response");
                }
                return new b0<>(responseBuild, null, responseBodyA);
            } finally {
                responseBodyBody.close();
            }
        }
        if (iCode == 204 || iCode == 205) {
            responseBodyBody.close();
            return b0.b(null, responseBuild);
        }
        b bVar = new b(responseBodyBody);
        try {
            return b0.b(this.f5060g.a(bVar), responseBuild);
        } catch (RuntimeException e2) {
            IOException iOException = bVar.f5068f;
            if (iOException == null) {
                throw e2;
            }
            throw iOException;
        }
    }

    @Override // l.b
    public void cancel() {
        Call call;
        this.f5061h = true;
        synchronized (this) {
            call = this.f5062i;
        }
        if (call != null) {
            call.cancel();
        }
    }

    public Object clone() throws CloneNotSupportedException {
        return new t(this.f5057b, this.f5058e, this.f5059f, this.f5060g);
    }

    @Override // l.b
    public b0<T> execute() throws IOException {
        Call callA;
        synchronized (this) {
            if (this.f5064k) {
                throw new IllegalStateException("Already executed.");
            }
            this.f5064k = true;
            Throwable th = this.f5063j;
            if (th != null) {
                if (th instanceof IOException) {
                    throw ((IOException) th);
                }
                if (th instanceof RuntimeException) {
                    throw ((RuntimeException) th);
                }
                throw ((Error) th);
            }
            callA = this.f5062i;
            if (callA == null) {
                try {
                    callA = a();
                    this.f5062i = callA;
                } catch (IOException | Error | RuntimeException e2) {
                    g0.o(e2);
                    this.f5063j = e2;
                    throw e2;
                }
            }
        }
        if (this.f5061h) {
            callA.cancel();
        }
        return b(callA.execute());
    }

    @Override // l.b
    public void f(d<T> dVar) {
        Call call;
        Throwable th;
        synchronized (this) {
            if (this.f5064k) {
                throw new IllegalStateException("Already executed.");
            }
            this.f5064k = true;
            call = this.f5062i;
            th = this.f5063j;
            if (call == null && th == null) {
                try {
                    Call callA = a();
                    this.f5062i = callA;
                    call = callA;
                } catch (Throwable th2) {
                    th = th2;
                    g0.o(th);
                    this.f5063j = th;
                }
            }
        }
        if (th != null) {
            dVar.a(this, th);
            return;
        }
        if (this.f5061h) {
            call.cancel();
        }
        call.enqueue(new a(dVar));
    }

    @Override // l.b
    public boolean isCanceled() {
        boolean z = true;
        if (this.f5061h) {
            return true;
        }
        synchronized (this) {
            Call call = this.f5062i;
            if (call == null || !call.isCanceled()) {
                z = false;
            }
        }
        return z;
    }

    @Override // l.b
    public synchronized Request request() {
        Call call = this.f5062i;
        if (call != null) {
            return call.request();
        }
        Throwable th = this.f5063j;
        if (th != null) {
            if (th instanceof IOException) {
                throw new RuntimeException("Unable to create request.", this.f5063j);
            }
            if (th instanceof RuntimeException) {
                throw ((RuntimeException) th);
            }
            throw ((Error) th);
        }
        try {
            Call callA = a();
            this.f5062i = callA;
            return callA.request();
        } catch (IOException e2) {
            this.f5063j = e2;
            throw new RuntimeException("Unable to create request.", e2);
        } catch (Error e3) {
            e = e3;
            g0.o(e);
            this.f5063j = e;
            throw e;
        } catch (RuntimeException e4) {
            e = e4;
            g0.o(e);
            this.f5063j = e;
            throw e;
        }
    }

    @Override // l.b
    public l.b clone() {
        return new t(this.f5057b, this.f5058e, this.f5059f, this.f5060g);
    }
}
