package o7;

import B6.q;
import e5.C0884d;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.lang.ref.Reference;
import java.net.Socket;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import k7.C;
import k7.InterfaceC1113e;
import k7.w;
import k7.y;
import s7.n;

/* JADX INFO: loaded from: classes.dex */
public final class i implements Cloneable {

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public boolean f15965A;

    /* JADX INFO: renamed from: B, reason: collision with root package name */
    public boolean f15966B;

    /* JADX INFO: renamed from: C, reason: collision with root package name */
    public volatile boolean f15967C;
    public volatile d D;

    /* JADX INFO: renamed from: E, reason: collision with root package name */
    public volatile l f15968E;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final w f15969q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final y f15970r;

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

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final h f15972t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final AtomicBoolean f15973u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public Object f15974v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public e f15975w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public l f15976x;

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

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public boolean f15978z;

    public i(w wVar, y yVar) {
        P6.g.e(wVar, "client");
        P6.g.e(yVar, "originalRequest");
        this.f15969q = wVar;
        this.f15970r = yVar;
        this.f15971s = (R0.m) wVar.f14185r.f11784r;
        wVar.f14188u.getClass();
        h hVar = new h(this);
        long j8 = wVar.f14179L;
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        hVar.g(j8);
        this.f15972t = hVar;
        this.f15973u = new AtomicBoolean();
        this.f15966B = true;
    }

    public static final String c(i iVar) {
        StringBuilder sb = new StringBuilder();
        sb.append(iVar.f15967C ? "canceled " : "");
        sb.append("call");
        sb.append(" to ");
        sb.append(iVar.f15970r.f14202a.g());
        return sb.toString();
    }

    public final void cancel() {
        Socket socket;
        if (this.f15967C) {
            return;
        }
        this.f15967C = true;
        d dVar = this.D;
        if (dVar != null) {
            dVar.f15949c.cancel();
        }
        l lVar = this.f15968E;
        if (lVar == null || (socket = lVar.f15984c) == null) {
            return;
        }
        l7.b.e(socket);
    }

    public final Object clone() {
        return new i(this.f15969q, this.f15970r);
    }

    public final void d(l lVar) {
        byte[] bArr = l7.b.f14412a;
        if (this.f15976x != null) {
            throw new IllegalStateException("Check failed.");
        }
        this.f15976x = lVar;
        lVar.f15996p.add(new g(this, this.f15974v));
    }

    public final IOException e(IOException iOException) {
        IOException interruptedIOException;
        Socket socketM;
        byte[] bArr = l7.b.f14412a;
        l lVar = this.f15976x;
        if (lVar != null) {
            synchronized (lVar) {
                socketM = m();
            }
            if (this.f15976x == null) {
                if (socketM != null) {
                    l7.b.e(socketM);
                }
            } else if (socketM != null) {
                throw new IllegalStateException("Check failed.");
            }
        }
        if (this.f15972t.i()) {
            interruptedIOException = new InterruptedIOException("timeout");
            if (iOException != null) {
                interruptedIOException.initCause(iOException);
            }
        } else {
            interruptedIOException = iOException;
        }
        if (iOException != null) {
            P6.g.b(interruptedIOException);
        }
        return interruptedIOException;
    }

    public final void f(InterfaceC1113e interfaceC1113e) {
        f fVar;
        if (!this.f15973u.compareAndSet(false, true)) {
            throw new IllegalStateException("Already Executed");
        }
        n nVar = n.f16942a;
        this.f15974v = n.f16942a.g();
        C2.a aVar = this.f15969q.f14184q;
        f fVar2 = new f(this, interfaceC1113e);
        aVar.getClass();
        synchronized (aVar) {
            ((ArrayDeque) aVar.f1157r).add(fVar2);
            String str = this.f15970r.f14202a.f14124d;
            Iterator it = ((ArrayDeque) aVar.f1159t).iterator();
            while (true) {
                if (!it.hasNext()) {
                    Iterator it2 = ((ArrayDeque) aVar.f1157r).iterator();
                    while (true) {
                        if (!it2.hasNext()) {
                            fVar = null;
                            break;
                        } else {
                            fVar = (f) it2.next();
                            if (P6.g.a(fVar.f15962s.f15970r.f14202a.f14124d, str)) {
                                break;
                            }
                        }
                    }
                } else {
                    fVar = (f) it.next();
                    if (P6.g.a(fVar.f15962s.f15970r.f14202a.f14124d, str)) {
                        break;
                    }
                }
            }
            if (fVar != null) {
                fVar2.f15961r = fVar.f15961r;
            }
        }
        aVar.A();
    }

    public final void h(boolean z8) {
        d dVar;
        synchronized (this) {
            if (!this.f15966B) {
                throw new IllegalStateException("released");
            }
        }
        if (z8 && (dVar = this.D) != null) {
            dVar.f15949c.cancel();
            dVar.f15947a.k(dVar, true, true, null);
        }
        this.f15977y = null;
    }

    public final C i() {
        ArrayList arrayList = new ArrayList();
        q.d0(this.f15969q.f14186s, arrayList);
        arrayList.add(new e5.i(this.f15969q));
        arrayList.add(new e5.i(this.f15969q.f14193z));
        this.f15969q.getClass();
        arrayList.add(new m7.b());
        arrayList.add(a.f15935a);
        q.d0(this.f15969q.f14187t, arrayList);
        arrayList.add(new C0884d(2));
        y yVar = this.f15970r;
        w wVar = this.f15969q;
        try {
            try {
                C cB = new p7.d(this, arrayList, 0, null, yVar, wVar.f14180M, wVar.f14181N, wVar.f14182O).b(this.f15970r);
                if (this.f15967C) {
                    l7.b.d(cB);
                    throw new IOException("Canceled");
                }
                l(null);
                return cB;
            } catch (IOException e8) {
                IOException iOExceptionL = l(e8);
                P6.g.c(iOExceptionL, "null cannot be cast to non-null type kotlin.Throwable");
                throw iOExceptionL;
            }
        } catch (Throwable th) {
            if (0 == 0) {
                l(null);
            }
            throw th;
        }
    }

    public final IOException k(d dVar, boolean z8, boolean z9, IOException iOException) {
        boolean z10;
        boolean z11;
        P6.g.e(dVar, "exchange");
        if (dVar.equals(this.D)) {
            synchronized (this) {
                z10 = false;
                if (z8) {
                    try {
                        if (!this.f15978z) {
                            if (z9 || !this.f15965A) {
                                z11 = false;
                            }
                        }
                        if (z8) {
                            this.f15978z = false;
                        }
                        if (z9) {
                            this.f15965A = false;
                        }
                        boolean z12 = this.f15978z;
                        boolean z13 = (z12 || this.f15965A) ? false : true;
                        if (!z12 && !this.f15965A) {
                            if (!this.f15966B) {
                                z10 = true;
                            }
                        }
                        z11 = z10;
                        z10 = z13;
                    } catch (Throwable th) {
                        throw th;
                    }
                } else {
                    if (z9) {
                    }
                    z11 = false;
                }
            }
            if (z10) {
                this.D = null;
                l lVar = this.f15976x;
                if (lVar != null) {
                    lVar.h();
                }
            }
            if (z11) {
                return e(iOException);
            }
        }
        return iOException;
    }

    public final IOException l(IOException iOException) {
        boolean z8;
        synchronized (this) {
            z8 = false;
            if (this.f15966B) {
                this.f15966B = false;
                if (!this.f15978z) {
                    if (!this.f15965A) {
                        z8 = true;
                    }
                }
            }
        }
        return z8 ? e(iOException) : iOException;
    }

    public final Socket m() {
        l lVar = this.f15976x;
        P6.g.b(lVar);
        byte[] bArr = l7.b.f14412a;
        ArrayList arrayList = lVar.f15996p;
        int size = arrayList.size();
        int i = 0;
        int i5 = 0;
        while (true) {
            if (i5 >= size) {
                i = -1;
                break;
            }
            Object obj = arrayList.get(i5);
            i5++;
            if (P6.g.a(((Reference) obj).get(), this)) {
                break;
            }
            i++;
        }
        if (i == -1) {
            throw new IllegalStateException("Check failed.");
        }
        arrayList.remove(i);
        this.f15976x = null;
        if (!arrayList.isEmpty()) {
            return null;
        }
        lVar.f15997q = System.nanoTime();
        R0.m mVar = this.f15971s;
        mVar.getClass();
        byte[] bArr2 = l7.b.f14412a;
        boolean z8 = lVar.f15990j;
        n7.c cVar = (n7.c) mVar.f5953c;
        if (!z8) {
            cVar.c((n7.b) mVar.f5954d, 0L);
            return null;
        }
        lVar.f15990j = true;
        ConcurrentLinkedQueue concurrentLinkedQueue = (ConcurrentLinkedQueue) mVar.f5955e;
        concurrentLinkedQueue.remove(lVar);
        if (concurrentLinkedQueue.isEmpty()) {
            cVar.a();
        }
        Socket socket = lVar.f15985d;
        P6.g.b(socket);
        return socket;
    }
}
