package o7;

import B6.t;
import java.io.IOException;
import java.net.Proxy;
import java.net.Socket;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.ConcurrentLinkedQueue;
import k7.C1109a;
import k7.F;
import k7.q;
import l6.C1139a;
import m4.C1179o;
import r7.B;
import r7.C1511a;

/* JADX INFO: loaded from: classes.dex */
public final class e {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final R0.m f15952a;

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final i f15954c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public C1179o f15955d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public F4.a f15956e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public int f15957f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public int f15958g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public int f15959h;
    public F i;

    public e(R0.m mVar, C1109a c1109a, i iVar) {
        P6.g.e(mVar, "connectionPool");
        P6.g.e(iVar, "call");
        this.f15952a = mVar;
        this.f15953b = c1109a;
        this.f15954c = iVar;
    }

    /* JADX WARN: Removed duplicated region for block: B:31:0x0062  */
    /* JADX WARN: Removed duplicated region for block: B:32:0x0071  */
    /* JADX WARN: Removed duplicated region for block: B:70:0x017d  */
    /* JADX WARN: Removed duplicated region for block: B:71:0x0198  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final l a(int i, int i5, int i8, boolean z8, boolean z9) throws IOException {
        ArrayList arrayList;
        List listL;
        boolean z10;
        Socket socketM;
        while (!this.f15954c.f15967C) {
            l lVar = this.f15954c.f15976x;
            if (lVar != null) {
                synchronized (lVar) {
                    try {
                        socketM = (lVar.f15990j || !b(lVar.f15983b.f14024a.f14041h)) ? this.f15954c.m() : null;
                    } catch (Throwable th) {
                        throw th;
                    }
                }
                if (this.f15954c.f15976x != null) {
                    if (socketM != null) {
                        throw new IllegalStateException("Check failed.");
                    }
                    z10 = z9;
                } else {
                    if (socketM != null) {
                        l7.b.e(socketM);
                    }
                    P6.g.e(this.f15954c, "call");
                    this.f15957f = 0;
                    this.f15958g = 0;
                    this.f15959h = 0;
                    if (this.f15952a.a(this.f15953b, this.f15954c, null, false)) {
                        F f3 = this.i;
                        try {
                            if (f3 != null) {
                                this.i = null;
                            } else {
                                C1179o c1179o = this.f15955d;
                                if (c1179o == null || !c1179o.b()) {
                                    F4.a aVar = this.f15956e;
                                    if (aVar == null) {
                                        C1109a c1109a = this.f15953b;
                                        i iVar = this.f15954c;
                                        C1139a c1139a = iVar.f15969q.f14183P;
                                        P6.g.e(c1139a, "routeDatabase");
                                        P6.g.e(iVar, "call");
                                        aVar = new F4.a();
                                        aVar.f2495b = c1109a;
                                        aVar.f2496c = c1139a;
                                        aVar.f2497d = iVar;
                                        t tVar = t.f1032q;
                                        aVar.f2498e = tVar;
                                        aVar.f2499f = tVar;
                                        aVar.f2500g = new ArrayList();
                                        q qVar = c1109a.f14041h;
                                        P6.g.e(qVar, "url");
                                        URI uriH = qVar.h();
                                        if (uriH.getHost() == null) {
                                            listL = l7.b.l(Proxy.NO_PROXY);
                                        } else {
                                            List<Proxy> listSelect = c1109a.f14040g.select(uriH);
                                            listL = (listSelect == null || listSelect.isEmpty()) ? l7.b.l(Proxy.NO_PROXY) : l7.b.x(listSelect);
                                        }
                                        aVar.f2498e = listL;
                                        aVar.f2494a = 0;
                                        this.f15956e = aVar;
                                    }
                                    C1179o c1179oL = aVar.l();
                                    this.f15955d = c1179oL;
                                    arrayList = c1179oL.f14641a;
                                    if (this.f15954c.f15967C) {
                                        throw new IOException("Canceled");
                                    }
                                    if (this.f15952a.a(this.f15953b, this.f15954c, arrayList, false)) {
                                        lVar = this.f15954c.f15976x;
                                        P6.g.b(lVar);
                                        P6.g.e(this.f15954c, "call");
                                    } else {
                                        if (!c1179oL.b()) {
                                            throw new NoSuchElementException();
                                        }
                                        int i9 = c1179oL.f14642b;
                                        c1179oL.f14642b = i9 + 1;
                                        f3 = (F) c1179oL.f14641a.get(i9);
                                        l lVar2 = new l(this.f15952a, f3);
                                        this.f15954c.f15968E = lVar2;
                                        lVar2.c(i, i5, i8, z8, this.f15954c);
                                        this.f15954c.f15968E = null;
                                        this.f15954c.f15969q.f14183P.n(f3);
                                        if (this.f15952a.a(this.f15953b, this.f15954c, arrayList, true)) {
                                            synchronized (lVar2) {
                                                R0.m mVar = this.f15952a;
                                                mVar.getClass();
                                                byte[] bArr = l7.b.f14412a;
                                                ((ConcurrentLinkedQueue) mVar.f5955e).add(lVar2);
                                                ((n7.c) mVar.f5953c).c((n7.b) mVar.f5954d, 0L);
                                                this.f15954c.d(lVar2);
                                            }
                                            P6.g.e(this.f15954c, "call");
                                            z10 = z9;
                                            lVar = lVar2;
                                        } else {
                                            l lVar3 = this.f15954c.f15976x;
                                            P6.g.b(lVar3);
                                            this.i = f3;
                                            Socket socket = lVar2.f15985d;
                                            P6.g.b(socket);
                                            l7.b.e(socket);
                                            P6.g.e(this.f15954c, "call");
                                            lVar = lVar3;
                                        }
                                    }
                                } else {
                                    C1179o c1179o2 = this.f15955d;
                                    P6.g.b(c1179o2);
                                    if (!c1179o2.b()) {
                                        throw new NoSuchElementException();
                                    }
                                    int i10 = c1179o2.f14642b;
                                    c1179o2.f14642b = i10 + 1;
                                    f3 = (F) c1179o2.f14641a.get(i10);
                                }
                            }
                            lVar2.c(i, i5, i8, z8, this.f15954c);
                            this.f15954c.f15968E = null;
                            this.f15954c.f15969q.f14183P.n(f3);
                            if (this.f15952a.a(this.f15953b, this.f15954c, arrayList, true)) {
                            }
                        } catch (Throwable th2) {
                            this.f15954c.f15968E = null;
                            throw th2;
                        }
                        arrayList = null;
                        l lVar22 = new l(this.f15952a, f3);
                        this.f15954c.f15968E = lVar22;
                    } else {
                        lVar = this.f15954c.f15976x;
                        P6.g.b(lVar);
                        P6.g.e(this.f15954c, "call");
                    }
                    z10 = z9;
                }
            } else {
                this.f15957f = 0;
                this.f15958g = 0;
                this.f15959h = 0;
                if (this.f15952a.a(this.f15953b, this.f15954c, null, false)) {
                }
                z10 = z9;
            }
            if (lVar.j(z10)) {
                return lVar;
            }
            lVar.l();
            if (this.i == null) {
                C1179o c1179o3 = this.f15955d;
                if (c1179o3 != null ? c1179o3.b() : true) {
                    continue;
                } else {
                    F4.a aVar2 = this.f15956e;
                    if (!(aVar2 != null ? aVar2.j() : true)) {
                        throw new IOException("exhausted all routes");
                    }
                }
            }
        }
        throw new IOException("Canceled");
    }

    public final boolean b(q qVar) {
        P6.g.e(qVar, "url");
        q qVar2 = this.f15953b.f14041h;
        return qVar.f14125e == qVar2.f14125e && P6.g.a(qVar.f14124d, qVar2.f14124d);
    }

    public final void c(IOException iOException) {
        P6.g.e(iOException, "e");
        this.i = null;
        if ((iOException instanceof B) && ((B) iOException).f16582q == 8) {
            this.f15957f++;
        } else if (iOException instanceof C1511a) {
            this.f15958g++;
        } else {
            this.f15959h++;
        }
    }
}
