package g.i0.f;

import g.g;
import g.i;
import g.w;
import java.net.UnknownServiceException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import javax.net.ssl.SSLSocket;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final List<i> f6155a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public int f6156b = 0;

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

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

    public b(List<i> list) {
        this.f6155a = list;
    }

    public i a(SSLSocket sSLSocket) throws UnknownServiceException {
        i iVar;
        boolean z;
        int i2 = this.f6156b;
        int size = this.f6155a.size();
        while (true) {
            if (i2 >= size) {
                iVar = null;
                break;
            }
            iVar = this.f6155a.get(i2);
            if (iVar.a(sSLSocket)) {
                this.f6156b = i2 + 1;
                break;
            }
            i2++;
        }
        if (iVar == null) {
            StringBuilder sbC = a.b.c.a.a.C("Unable to find acceptable protocols. isFallback=");
            sbC.append(this.f6158d);
            sbC.append(", modes=");
            sbC.append(this.f6155a);
            sbC.append(", supported protocols=");
            sbC.append(Arrays.toString(sSLSocket.getEnabledProtocols()));
            throw new UnknownServiceException(sbC.toString());
        }
        int i3 = this.f6156b;
        while (true) {
            if (i3 >= this.f6155a.size()) {
                z = false;
                break;
            }
            if (this.f6155a.get(i3).a(sSLSocket)) {
                z = true;
                break;
            }
            i3++;
        }
        this.f6157c = z;
        g.i0.a aVar = g.i0.a.f6138a;
        boolean z2 = this.f6158d;
        Objects.requireNonNull((w.a) aVar);
        String[] strArrQ = iVar.f6132g != null ? g.i0.c.q(g.g.f6105a, sSLSocket.getEnabledCipherSuites(), iVar.f6132g) : sSLSocket.getEnabledCipherSuites();
        String[] strArrQ2 = iVar.f6133h != null ? g.i0.c.q(g.i0.c.o, sSLSocket.getEnabledProtocols(), iVar.f6133h) : sSLSocket.getEnabledProtocols();
        String[] supportedCipherSuites = sSLSocket.getSupportedCipherSuites();
        Comparator<String> comparator = g.g.f6105a;
        byte[] bArr = g.i0.c.f6140a;
        int length = supportedCipherSuites.length;
        int i4 = 0;
        while (true) {
            if (i4 >= length) {
                i4 = -1;
                break;
            }
            if (((g.a) comparator).compare(supportedCipherSuites[i4], "TLS_FALLBACK_SCSV") == 0) {
                break;
            }
            i4++;
        }
        if (z2 && i4 != -1) {
            String str = supportedCipherSuites[i4];
            int length2 = strArrQ.length + 1;
            String[] strArr = new String[length2];
            System.arraycopy(strArrQ, 0, strArr, 0, strArrQ.length);
            strArr[length2 - 1] = str;
            strArrQ = strArr;
        }
        boolean z3 = iVar.f6130e;
        if (!z3) {
            throw new IllegalStateException("no cipher suites for cleartext connections");
        }
        if (strArrQ.length == 0) {
            throw new IllegalArgumentException("At least one cipher suite is required");
        }
        String[] strArr2 = (String[]) strArrQ.clone();
        if (!z3) {
            throw new IllegalStateException("no TLS versions for cleartext connections");
        }
        if (strArrQ2.length == 0) {
            throw new IllegalArgumentException("At least one TLS version is required");
        }
        sSLSocket.setEnabledProtocols((String[]) strArrQ2.clone());
        sSLSocket.setEnabledCipherSuites(strArr2);
        return iVar;
    }
}
