package p447.p448.p452;

import java.lang.ref.Reference;
import java.net.Proxy;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLPeerUnverifiedException;
import kotlin.TypeCastException;
import kotlin.jvm.internal.DefaultConstructorMarker;
import p138.p139.p143.p144.C2803;
import p402.InterfaceC6540;
import p402.p411.p413.C6635;
import p447.C6860;
import p447.C6871;
import p447.C6874;
import p447.C6901;
import p447.C6908;
import p447.C6938;
import p447.p448.C6943;
import p447.p448.p451.AbstractC6950;
import p447.p448.p451.C6952;
import p447.p448.p451.C6953;
import p447.p448.p452.C6964;
import p447.p448.p457.C7069;
import p447.p448.p460.C7095;

/* JADX INFO: renamed from: ސ.ޣ.ބ.އ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
@InterfaceC6540
public final class C6973 {

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final long f20305;

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final C6952 f20306;

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public final C6975 f20307;

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public final ArrayDeque<C6968> f20308;

    /* JADX INFO: renamed from: ނ, reason: contains not printable characters */
    public final int f20309;

    /* JADX INFO: renamed from: ސ.ޣ.ބ.އ$֏, reason: contains not printable characters */
    public static final class C6974 {
        public C6974() {
        }

        public /* synthetic */ C6974(DefaultConstructorMarker defaultConstructorMarker) {
        }
    }

    /* JADX INFO: renamed from: ސ.ޣ.ބ.އ$ؠ, reason: contains not printable characters */
    public static final class C6975 extends AbstractC6950 {
        public C6975(String str) {
            super(str, false, 2, null);
        }

        @Override // p447.p448.p451.AbstractC6950
        /* JADX INFO: renamed from: ֏ */
        public long mo10183() {
            return C6973.this.m10240(System.nanoTime());
        }
    }

    static {
        new C6974(null);
    }

    public C6973(C6953 c6953, int i, long j, TimeUnit timeUnit) {
        if (c6953 == null) {
            C6635.m9824("taskRunner");
            throw null;
        }
        if (timeUnit == null) {
            C6635.m9824("timeUnit");
            throw null;
        }
        this.f20309 = i;
        this.f20305 = timeUnit.toNanos(j);
        this.f20306 = c6953.m10194();
        this.f20307 = new C6975(C2803.m4884(new StringBuilder(), C6943.f20197, " ConnectionPool"));
        this.f20308 = new ArrayDeque<>();
        if (!(j > 0)) {
            throw new IllegalArgumentException(C2803.m4874("keepAliveDuration <= 0: ", j).toString());
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final int m10239(C6968 c6968, long j) {
        List<Reference<C6964>> list = c6968.f20296;
        int i = 0;
        while (i < list.size()) {
            Reference<C6964> reference = list.get(i);
            if (reference.get() != null) {
                i++;
            } else {
                C6964.C6966 c6966 = (C6964.C6966) reference;
                StringBuilder sbM4889 = C2803.m4889("A connection to ");
                sbM4889.append(c6968.f20299.f20176.f19900);
                sbM4889.append(" was leaked. ");
                sbM4889.append("Did you forget to close a response body?");
                String string = sbM4889.toString();
                if (C7069.f20588 == null) {
                    throw null;
                }
                C7069.f20586.mo10371(string, c6966.f20281);
                list.remove(i);
                c6968.f20290 = true;
                if (list.isEmpty()) {
                    c6968.f20297 = j - this.f20305;
                    return 0;
                }
            }
        }
        return list.size();
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final long m10240(long j) {
        synchronized (this) {
            int i = 0;
            C6968 c6968 = null;
            long j2 = Long.MIN_VALUE;
            int i2 = 0;
            for (C6968 c69682 : this.f20308) {
                C6635.m9823((Object) c69682, "connection");
                if (m10239(c69682, j) > 0) {
                    i2++;
                } else {
                    i++;
                    long j3 = j - c69682.f20297;
                    if (j3 > j2) {
                        c6968 = c69682;
                        j2 = j3;
                    }
                }
            }
            if (j2 < this.f20305 && i <= this.f20309) {
                if (i > 0) {
                    return this.f20305 - j2;
                }
                if (i2 <= 0) {
                    return -1L;
                }
                return this.f20305;
            }
            this.f20308.remove(c6968);
            if (this.f20308.isEmpty()) {
                this.f20306.m10184();
            }
            if (c6968 != null) {
                C6943.m10165(c6968.m10238());
                return 0L;
            }
            C6635.m9822();
            throw null;
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m10241(C6968 c6968) {
        if (c6968 == null) {
            C6635.m9824("connection");
            throw null;
        }
        if (!C6943.f20196 || Thread.holdsLock(this)) {
            this.f20308.add(c6968);
            this.f20306.m10185(this.f20307, 0L);
            return;
        }
        StringBuilder sbM4889 = C2803.m4889("Thread ");
        Thread threadCurrentThread = Thread.currentThread();
        C6635.m9823((Object) threadCurrentThread, "Thread.currentThread()");
        sbM4889.append(threadCurrentThread.getName());
        sbM4889.append(" MUST hold lock on ");
        sbM4889.append(this);
        throw new AssertionError(sbM4889.toString());
    }

    /* JADX WARN: Removed duplicated region for block: B:113:0x012e A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:117:0x0162 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:59:0x00de  */
    /* JADX WARN: Removed duplicated region for block: B:78:0x0125  */
    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean m10242(C6860 c6860, C6964 c6964, List<C6938> list, boolean z) {
        boolean z2;
        C6908 c6908;
        boolean z3;
        C6901 c6901;
        if (c6860 == null) {
            C6635.m9824("address");
            throw null;
        }
        if (c6964 == null) {
            C6635.m9824("call");
            throw null;
        }
        if (C6943.f20196 && !Thread.holdsLock(this)) {
            StringBuilder sbM4889 = C2803.m4889("Thread ");
            Thread threadCurrentThread = Thread.currentThread();
            C6635.m9823((Object) threadCurrentThread, "Thread.currentThread()");
            sbM4889.append(threadCurrentThread.getName());
            sbM4889.append(" MUST hold lock on ");
            sbM4889.append(this);
            throw new AssertionError(sbM4889.toString());
        }
        Iterator<C6968> it = this.f20308.iterator();
        while (true) {
            boolean z4 = false;
            if (!it.hasNext()) {
                return false;
            }
            C6968 next = it.next();
            if (!z || next.m10235()) {
                if (next.f20296.size() < next.f20295 && !next.f20290 && next.f20299.f20176.m10021(c6860)) {
                    if (C6635.m9825((Object) c6860.f19900.f20017, (Object) next.f20299.f20176.f19900.f20017)) {
                        z4 = true;
                    } else if (next.f20287 != null && list != null) {
                        if (list.isEmpty()) {
                            z2 = false;
                            if (z2 && c6860.f19906 == C7095.f20616) {
                                c6908 = c6860.f19900;
                                if (c6908 != null) {
                                    C6635.m9824("url");
                                    throw null;
                                }
                                C6908 c69082 = next.f20299.f20176.f19900;
                                if (c6908.f20018 == c69082.f20018) {
                                    if (!C6635.m9825((Object) c6908.f20017, (Object) c69082.f20017)) {
                                        if (!next.f20291 && (c6901 = next.f20285) != null) {
                                            List<Certificate> listM10065 = c6901.m10065();
                                            if (!listM10065.isEmpty()) {
                                                C7095 c7095 = C7095.f20616;
                                                String str = c6908.f20017;
                                                Certificate certificate = listM10065.get(0);
                                                if (certificate == null) {
                                                    throw new TypeCastException("null cannot be cast to non-null type java.security.cert.X509Certificate");
                                                }
                                                boolean z5 = c7095.m10397(str, (X509Certificate) certificate);
                                                if (z5) {
                                                }
                                                if (z3) {
                                                    try {
                                                        C6871 c6871 = c6860.f19907;
                                                        if (c6871 == null) {
                                                            C6635.m9822();
                                                            throw null;
                                                        }
                                                        String str2 = c6860.f19900.f20017;
                                                        C6901 c69012 = next.f20285;
                                                        if (c69012 == null) {
                                                            C6635.m9822();
                                                            throw null;
                                                        }
                                                        List<Certificate> listM100652 = c69012.m10065();
                                                        if (str2 == null) {
                                                            C6635.m9824("hostname");
                                                            throw null;
                                                        }
                                                        if (listM100652 == null) {
                                                            C6635.m9824("peerCertificates");
                                                            throw null;
                                                        }
                                                        c6871.m10030(str2, new C6874(c6871, listM100652, str2));
                                                        z4 = true;
                                                    } catch (SSLPeerUnverifiedException unused) {
                                                    }
                                                }
                                            }
                                        }
                                        z3 = false;
                                        if (z3) {
                                        }
                                    }
                                    z3 = true;
                                    if (z3) {
                                    }
                                } else {
                                    z3 = false;
                                    if (z3) {
                                    }
                                }
                            }
                        } else {
                            for (C6938 c6938 : list) {
                                if (c6938.f20177.type() == Proxy.Type.DIRECT && next.f20299.f20177.type() == Proxy.Type.DIRECT && C6635.m9825(next.f20299.f20178, c6938.f20178)) {
                                    z2 = true;
                                    break;
                                }
                            }
                            z2 = false;
                            if (z2) {
                                c6908 = c6860.f19900;
                                if (c6908 != null) {
                                }
                            }
                        }
                    }
                }
                if (z4) {
                    C6635.m9823((Object) next, "connection");
                    c6964.m10219(next);
                    return true;
                }
            }
        }
    }
}
