package j;

import g.d;
import g.d0;
import g.p;
import g.r;
import g.s;
import g.v;
import g.y;
import g.y.b;
import g.z;
import j.z;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;

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

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final d.a f6971d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final j<g.f0, T> f6972e;

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    @GuardedBy("this")
    @Nullable
    public g.d f6974g;

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

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

    public class a implements g.e {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public final /* synthetic */ d f6977a;

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

        public void a(g.d dVar, IOException iOException) {
            try {
                this.f6977a.b(t.this, iOException);
            } catch (Throwable th) {
                h0.o(th);
                th.printStackTrace();
            }
        }

        public void b(g.d dVar, g.d0 d0Var) {
            try {
                try {
                    this.f6977a.a(t.this, t.this.c(d0Var));
                } catch (Throwable th) {
                    h0.o(th);
                    th.printStackTrace();
                }
            } catch (Throwable th2) {
                h0.o(th2);
                try {
                    this.f6977a.b(t.this, th2);
                } catch (Throwable th3) {
                    h0.o(th3);
                    th3.printStackTrace();
                }
            }
        }
    }

    public static final class b extends g.f0 {

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public final g.f0 f6979c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        public final h.h f6980d;

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

        public class a extends h.k {
            public a(h.x xVar) {
                super(xVar);
            }

            @Override // h.x
            public long v(h.f fVar, long j2) throws IOException {
                try {
                    return this.f6551b.v(fVar, j2);
                } catch (IOException e2) {
                    b.this.f6981e = e2;
                    throw e2;
                }
            }
        }

        public b(g.f0 f0Var) {
            this.f6979c = f0Var;
            a aVar = new a(f0Var.o());
            Logger logger = h.p.f6564a;
            this.f6980d = new h.s(aVar);
        }

        @Override // g.f0
        public long a() {
            return this.f6979c.a();
        }

        @Override // g.f0, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            this.f6979c.close();
        }

        @Override // g.f0
        public g.u i() {
            return this.f6979c.i();
        }

        @Override // g.f0
        public h.h o() {
            return this.f6980d;
        }
    }

    public static final class c extends g.f0 {

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        @Nullable
        public final g.u f6983c;

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

        public c(@Nullable g.u uVar, long j2) {
            this.f6983c = uVar;
            this.f6984d = j2;
        }

        @Override // g.f0
        public long a() {
            return this.f6984d;
        }

        @Override // g.f0
        public g.u i() {
            return this.f6983c;
        }

        @Override // g.f0
        public h.h o() {
            throw new IllegalStateException("Cannot read raw response body of a converted body.");
        }
    }

    public t(a0 a0Var, Object[] objArr, d.a aVar, j<g.f0, T> jVar) {
        this.f6969b = a0Var;
        this.f6970c = objArr;
        this.f6971d = aVar;
        this.f6972e = jVar;
    }

    @Override // j.b
    public void B(d<T> dVar) {
        g.d dVar2;
        Throwable th;
        synchronized (this) {
            if (this.f6976i) {
                throw new IllegalStateException("Already executed.");
            }
            this.f6976i = true;
            dVar2 = this.f6974g;
            th = this.f6975h;
            if (dVar2 == null && th == null) {
                try {
                    g.d dVarB = b();
                    this.f6974g = dVarB;
                    dVar2 = dVarB;
                } catch (Throwable th2) {
                    th = th2;
                    h0.o(th);
                    this.f6975h = th;
                }
            }
        }
        if (th != null) {
            dVar.b(this, th);
            return;
        }
        if (this.f6973f) {
            ((g.y) dVar2).a();
        }
        a aVar = new a(dVar);
        g.y yVar = (g.y) dVar2;
        synchronized (yVar) {
            if (yVar.f6515h) {
                throw new IllegalStateException("Already Executed");
            }
            yVar.f6515h = true;
        }
        yVar.f6510c.f6227c = g.i0.j.f.f6419a.j("response.body().close()");
        Objects.requireNonNull(yVar.f6512e);
        g.l lVar = yVar.f6509b.f6492d;
        y.b bVar = yVar.new b(aVar);
        synchronized (lVar) {
            lVar.f6436b.add(bVar);
        }
        lVar.b();
    }

    @Override // j.b
    public boolean E() {
        boolean z = true;
        if (this.f6973f) {
            return true;
        }
        synchronized (this) {
            g.d dVar = this.f6974g;
            if (dVar == null || !((g.y) dVar).f6510c.f6228d) {
                z = false;
            }
        }
        return z;
    }

    @Override // j.b
    public void a() {
        g.d dVar;
        this.f6973f = true;
        synchronized (this) {
            dVar = this.f6974g;
        }
        if (dVar != null) {
            ((g.y) dVar).a();
        }
    }

    public final g.d b() {
        g.s sVarA;
        d.a aVar = this.f6971d;
        a0 a0Var = this.f6969b;
        Object[] objArr = this.f6970c;
        x<?>[] xVarArr = a0Var.f6878j;
        int length = objArr.length;
        if (length != xVarArr.length) {
            StringBuilder sbD = a.b.c.a.a.D("Argument count (", length, ") doesn't match expected count (");
            sbD.append(xVarArr.length);
            sbD.append(")");
            throw new IllegalArgumentException(sbD.toString());
        }
        z zVar = new z(a0Var.f6871c, a0Var.f6870b, a0Var.f6872d, a0Var.f6873e, a0Var.f6874f, a0Var.f6875g, a0Var.f6876h, a0Var.f6877i);
        if (a0Var.k) {
            length--;
        }
        ArrayList arrayList = new ArrayList(length);
        for (int i2 = 0; i2 < length; i2++) {
            arrayList.add(objArr[i2]);
            xVarArr[i2].a(zVar, objArr[i2]);
        }
        s.a aVar2 = zVar.f7035f;
        if (aVar2 != null) {
            sVarA = aVar2.a();
        } else {
            s.a aVarK = zVar.f7033d.k(zVar.f7034e);
            sVarA = aVarK != null ? aVarK.a() : null;
            if (sVarA == null) {
                StringBuilder sbC = a.b.c.a.a.C("Malformed URL. Base: ");
                sbC.append(zVar.f7033d);
                sbC.append(", Relative: ");
                sbC.append(zVar.f7034e);
                throw new IllegalArgumentException(sbC.toString());
            }
        }
        g.c0 aVar3 = zVar.m;
        if (aVar3 == null) {
            p.a aVar4 = zVar.l;
            if (aVar4 != null) {
                aVar3 = new g.p(aVar4.f6445a, aVar4.f6446b);
            } else {
                v.a aVar5 = zVar.k;
                if (aVar5 != null) {
                    if (aVar5.f6487c.isEmpty()) {
                        throw new IllegalStateException("Multipart body must have at least one part.");
                    }
                    aVar3 = new g.v(aVar5.f6485a, aVar5.f6486b, aVar5.f6487c);
                } else if (zVar.f7039j) {
                    long j2 = 0;
                    g.i0.c.c(j2, j2, j2);
                    aVar3 = new g.b0(null, 0, new byte[0], 0);
                }
            }
        }
        g.u uVar = zVar.f7038i;
        if (uVar != null) {
            if (aVar3 != null) {
                aVar3 = new z.a(aVar3, uVar);
            } else {
                zVar.f7037h.a("Content-Type", uVar.f6473c);
            }
        }
        z.a aVar6 = zVar.f7036g;
        aVar6.e(sVarA);
        List<String> list = zVar.f7037h.f6452a;
        String[] strArr = (String[]) list.toArray(new String[list.size()]);
        r.a aVar7 = new r.a();
        Collections.addAll(aVar7.f6452a, strArr);
        aVar6.f6526c = aVar7;
        aVar6.c(zVar.f7032c, aVar3);
        aVar6.d(n.class, new n(a0Var.f6869a, arrayList));
        g.d dVarB = aVar.b(aVar6.a());
        Objects.requireNonNull(dVarB, "Call.Factory returned null.");
        return dVarB;
    }

    public b0<T> c(g.d0 d0Var) throws IOException {
        g.f0 f0Var = d0Var.f6095h;
        d0.a aVar = new d0.a(d0Var);
        aVar.f6104g = new c(f0Var.i(), f0Var.a());
        g.d0 d0VarA = aVar.a();
        int i2 = d0VarA.f6091d;
        if (i2 < 200 || i2 >= 300) {
            try {
                g.f0 f0VarA = h0.a(f0Var);
                if (d0VarA.a()) {
                    throw new IllegalArgumentException("rawResponse should not be successful response");
                }
                return new b0<>(d0VarA, null, f0VarA);
            } finally {
                f0Var.close();
            }
        }
        if (i2 == 204 || i2 == 205) {
            f0Var.close();
            return b0.b(null, d0VarA);
        }
        b bVar = new b(f0Var);
        try {
            return b0.b(this.f6972e.a(bVar), d0VarA);
        } catch (RuntimeException e2) {
            IOException iOException = bVar.f6981e;
            if (iOException == null) {
                throw e2;
            }
            throw iOException;
        }
    }

    public Object clone() {
        return new t(this.f6969b, this.f6970c, this.f6971d, this.f6972e);
    }

    @Override // j.b
    /* JADX INFO: renamed from: i */
    public j.b clone() {
        return new t(this.f6969b, this.f6970c, this.f6971d, this.f6972e);
    }

    @Override // j.b
    public b0<T> o() {
        g.d dVarB;
        synchronized (this) {
            if (this.f6976i) {
                throw new IllegalStateException("Already executed.");
            }
            this.f6976i = true;
            Throwable th = this.f6975h;
            if (th != null) {
                if (th instanceof IOException) {
                    throw ((IOException) th);
                }
                if (th instanceof RuntimeException) {
                    throw ((RuntimeException) th);
                }
                throw ((Error) th);
            }
            dVarB = this.f6974g;
            if (dVarB == null) {
                try {
                    dVarB = b();
                    this.f6974g = dVarB;
                } catch (IOException | Error | RuntimeException e2) {
                    h0.o(e2);
                    this.f6975h = e2;
                    throw e2;
                }
            }
        }
        if (this.f6973f) {
            ((g.y) dVarB).a();
        }
        g.y yVar = (g.y) dVarB;
        synchronized (yVar) {
            if (yVar.f6515h) {
                throw new IllegalStateException("Already Executed");
            }
            yVar.f6515h = true;
        }
        yVar.f6510c.f6227c = g.i0.j.f.f6419a.j("response.body().close()");
        yVar.f6511d.i();
        Objects.requireNonNull(yVar.f6512e);
        try {
            try {
                g.l lVar = yVar.f6509b.f6492d;
                synchronized (lVar) {
                    lVar.f6438d.add(yVar);
                }
                g.d0 d0VarB = yVar.b();
                if (d0VarB == null) {
                    throw new IOException("Canceled");
                }
                g.l lVar2 = yVar.f6509b.f6492d;
                lVar2.a(lVar2.f6438d, yVar);
                return c(d0VarB);
            } catch (IOException e3) {
                IOException iOExceptionD = yVar.d(e3);
                Objects.requireNonNull(yVar.f6512e);
                throw iOExceptionD;
            }
        } catch (Throwable th2) {
            g.l lVar3 = yVar.f6509b.f6492d;
            lVar3.a(lVar3.f6438d, yVar);
            throw th2;
        }
    }

    @Override // j.b
    public synchronized g.z z() {
        g.d dVar = this.f6974g;
        if (dVar != null) {
            return ((g.y) dVar).f6513f;
        }
        Throwable th = this.f6975h;
        if (th != null) {
            if (th instanceof IOException) {
                throw new RuntimeException("Unable to create request.", this.f6975h);
            }
            if (th instanceof RuntimeException) {
                throw ((RuntimeException) th);
            }
            throw ((Error) th);
        }
        try {
            g.d dVarB = b();
            this.f6974g = dVarB;
            return ((g.y) dVarB).f6513f;
        } catch (IOException e2) {
            this.f6975h = e2;
            throw new RuntimeException("Unable to create request.", e2);
        } catch (Error e3) {
            e = e3;
            h0.o(e);
            this.f6975h = e;
            throw e;
        } catch (RuntimeException e4) {
            e = e4;
            h0.o(e);
            this.f6975h = e;
            throw e;
        }
    }
}
