package F4;

import B6.j;
import N1.C0207h;
import P6.g;
import a.AbstractC0386a;
import com.google.android.gms.internal.measurement.C1;
import d5.AbstractC0844a;
import java.io.EOFException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.NoSuchElementException;
import k7.C;
import k7.C1109a;
import k7.o;
import k7.q;
import k7.w;
import k7.x;
import k7.y;
import l6.C1139a;
import m4.C1179o;
import o7.i;
import o7.l;
import q7.d;
import q7.e;
import q7.f;
import x7.A;
import x7.B;
import x7.F;
import x7.H;

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

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

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

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

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

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

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

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

    public a(w wVar, l lVar, B b8, A a3) {
        g.e(lVar, "connection");
        g.e(b8, "source");
        g.e(a3, "sink");
        this.f2495b = wVar;
        this.f2496c = lVar;
        this.f2497d = b8;
        this.f2498e = a3;
        this.f2499f = new C0207h(b8);
    }

    @Override // p7.b
    public H a(C c3) {
        if (!p7.c.a(c3)) {
            return k(0L);
        }
        if ("chunked".equalsIgnoreCase(C.a("Transfer-Encoding", c3))) {
            q qVar = c3.f14010q.f14202a;
            if (this.f2494a == 4) {
                this.f2494a = 5;
                return new q7.c(this, qVar);
            }
            throw new IllegalStateException(("state: " + this.f2494a).toString());
        }
        long jK = l7.b.k(c3);
        if (jK != -1) {
            return k(jK);
        }
        if (this.f2494a == 4) {
            this.f2494a = 5;
            ((l) this.f2496c).l();
            return new f(this);
        }
        throw new IllegalStateException(("state: " + this.f2494a).toString());
    }

    @Override // p7.b
    public void b(y yVar) {
        g.e(yVar, "request");
        Proxy.Type type = ((l) this.f2496c).f15983b.f14025b.type();
        g.d(type, "connection.route().proxy.type()");
        StringBuilder sb = new StringBuilder();
        sb.append(yVar.f14203b);
        sb.append(' ');
        q qVar = yVar.f14202a;
        if (qVar.f14129j || type != Proxy.Type.HTTP) {
            String strB = qVar.b();
            String strD = qVar.d();
            if (strD != null) {
                strB = strB + '?' + strD;
            }
            sb.append(strB);
        } else {
            sb.append(qVar);
        }
        sb.append(" HTTP/1.1");
        String string = sb.toString();
        g.d(string, "StringBuilder().apply(builderAction).toString()");
        m(yVar.f14204c, string);
    }

    @Override // p7.b
    public F c(y yVar, long j8) {
        g.e(yVar, "request");
        if ("chunked".equalsIgnoreCase(yVar.f14204c.a("Transfer-Encoding"))) {
            if (this.f2494a == 1) {
                this.f2494a = 2;
                return new q7.b(this);
            }
            throw new IllegalStateException(("state: " + this.f2494a).toString());
        }
        if (j8 == -1) {
            throw new IllegalStateException("Cannot stream a request body without chunked encoding or a known content length!");
        }
        if (this.f2494a == 1) {
            this.f2494a = 2;
            return new e(this);
        }
        throw new IllegalStateException(("state: " + this.f2494a).toString());
    }

    @Override // p7.b
    public void cancel() {
        Socket socket = ((l) this.f2496c).f15984c;
        if (socket != null) {
            l7.b.e(socket);
        }
    }

    @Override // p7.b
    public long d(C c3) {
        if (!p7.c.a(c3)) {
            return 0L;
        }
        if ("chunked".equalsIgnoreCase(C.a("Transfer-Encoding", c3))) {
            return -1L;
        }
        return l7.b.k(c3);
    }

    @Override // p7.b
    public void e() {
        ((A) this.f2498e).flush();
    }

    @Override // p7.b
    public void f() {
        ((A) this.f2498e).flush();
    }

    @Override // p7.b
    public k7.B g(boolean z8) throws IOException {
        C0207h c0207h = (C0207h) this.f2499f;
        int i = this.f2494a;
        if (i != 1 && i != 2 && i != 3) {
            throw new IllegalStateException(("state: " + this.f2494a).toString());
        }
        try {
            String strK = ((B) c0207h.f4548s).K(c0207h.f4547r);
            c0207h.f4547r -= (long) strK.length();
            A1.H hX = AbstractC0386a.x(strK);
            int i5 = hX.f65r;
            k7.B b8 = new k7.B();
            b8.f13996b = (x) hX.f66s;
            b8.f13997c = i5;
            b8.f13998d = (String) hX.f67t;
            b8.f14000f = c0207h.x().f();
            if (z8 && i5 == 100) {
                return null;
            }
            if (i5 == 100) {
                this.f2494a = 3;
                return b8;
            }
            if (102 > i5 || i5 >= 200) {
                this.f2494a = 4;
                return b8;
            }
            this.f2494a = 3;
            return b8;
        } catch (EOFException e8) {
            throw new IOException("unexpected end of stream on ".concat(((l) this.f2496c).f15983b.f14024a.f14041h.g()), e8);
        }
    }

    @Override // p7.b
    public l h() {
        return (l) this.f2496c;
    }

    public b i() {
        String strL = this.f2494a == 0 ? " registrationStatus" : "";
        if (((Long) this.f2499f) == null) {
            strL = strL.concat(" expiresInSecs");
        }
        if (((Long) this.f2500g) == null) {
            strL = AbstractC0844a.l(strL, " tokenCreationEpochInSecs");
        }
        if (strL.isEmpty()) {
            return new b((String) this.f2495b, this.f2494a, (String) this.f2496c, (String) this.f2497d, ((Long) this.f2499f).longValue(), ((Long) this.f2500g).longValue(), (String) this.f2498e);
        }
        throw new IllegalStateException("Missing required properties:".concat(strL));
    }

    public boolean j() {
        return this.f2494a < ((List) this.f2498e).size() || !((ArrayList) this.f2500g).isEmpty();
    }

    public d k(long j8) {
        if (this.f2494a == 4) {
            this.f2494a = 5;
            return new d(this, j8);
        }
        throw new IllegalStateException(("state: " + this.f2494a).toString());
    }

    /* JADX WARN: Type inference failed for: r3v13, types: [java.lang.Object, java.util.List] */
    public C1179o l() throws SocketException, UnknownHostException {
        String hostAddress;
        int port;
        List listC;
        boolean zContains;
        if (!j()) {
            throw new NoSuchElementException();
        }
        ArrayList arrayList = new ArrayList();
        while (this.f2494a < ((List) this.f2498e).size()) {
            boolean z8 = this.f2494a < ((List) this.f2498e).size();
            C1109a c1109a = (C1109a) this.f2495b;
            if (!z8) {
                throw new SocketException("No route to " + c1109a.f14041h.f14124d + "; exhausted proxy configurations: " + ((List) this.f2498e));
            }
            List list = (List) this.f2498e;
            int i = this.f2494a;
            this.f2494a = i + 1;
            Proxy proxy = (Proxy) list.get(i);
            ArrayList arrayList2 = new ArrayList();
            this.f2499f = arrayList2;
            if (proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.SOCKS) {
                q qVar = c1109a.f14041h;
                hostAddress = qVar.f14124d;
                port = qVar.f14125e;
            } else {
                SocketAddress socketAddressAddress = proxy.address();
                if (!(socketAddressAddress instanceof InetSocketAddress)) {
                    throw new IllegalArgumentException(("Proxy.address() is not an InetSocketAddress: " + socketAddressAddress.getClass()).toString());
                }
                g.d(socketAddressAddress, "proxyAddress");
                InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddressAddress;
                g.e(inetSocketAddress, "<this>");
                InetAddress address = inetSocketAddress.getAddress();
                if (address == null) {
                    hostAddress = inetSocketAddress.getHostName();
                    g.d(hostAddress, "hostName");
                } else {
                    hostAddress = address.getHostAddress();
                    g.d(hostAddress, "address.hostAddress");
                }
                port = inetSocketAddress.getPort();
            }
            if (1 > port || port >= 65536) {
                throw new SocketException("No route to " + hostAddress + ':' + port + "; port is out of range");
            }
            if (proxy.type() == Proxy.Type.SOCKS) {
                arrayList2.add(InetSocketAddress.createUnresolved(hostAddress, port));
            } else {
                byte[] bArr = l7.b.f14412a;
                g.e(hostAddress, "<this>");
                X6.d dVar = l7.b.f14417f;
                dVar.getClass();
                if (dVar.f7248q.matcher(hostAddress).matches()) {
                    listC = C1.C(InetAddress.getByName(hostAddress));
                } else {
                    g.e((i) this.f2497d, "call");
                    c1109a.f14034a.getClass();
                    try {
                        InetAddress[] allByName = InetAddress.getAllByName(hostAddress);
                        g.d(allByName, "getAllByName(hostname)");
                        List listP = j.P(allByName);
                        if (listP.isEmpty()) {
                            throw new UnknownHostException(c1109a.f14034a + " returned no addresses for " + hostAddress);
                        }
                        listC = listP;
                    } catch (NullPointerException e8) {
                        UnknownHostException unknownHostException = new UnknownHostException("Broken system behaviour for dns lookup of ".concat(hostAddress));
                        unknownHostException.initCause(e8);
                        throw unknownHostException;
                    }
                }
                Iterator it = listC.iterator();
                while (it.hasNext()) {
                    arrayList2.add(new InetSocketAddress((InetAddress) it.next(), port));
                }
            }
            Iterator it2 = this.f2499f.iterator();
            while (it2.hasNext()) {
                k7.F f3 = new k7.F((C1109a) this.f2495b, proxy, (InetSocketAddress) it2.next());
                C1139a c1139a = (C1139a) this.f2496c;
                synchronized (c1139a) {
                    zContains = ((LinkedHashSet) c1139a.f14409q).contains(f3);
                }
                if (zContains) {
                    ((ArrayList) this.f2500g).add(f3);
                } else {
                    arrayList.add(f3);
                }
            }
            if (!arrayList.isEmpty()) {
                break;
            }
        }
        if (arrayList.isEmpty()) {
            B6.q.d0((ArrayList) this.f2500g, arrayList);
            ((ArrayList) this.f2500g).clear();
        }
        return new C1179o(arrayList);
    }

    public void m(o oVar, String str) {
        g.e(str, "requestLine");
        if (this.f2494a != 0) {
            throw new IllegalStateException(("state: " + this.f2494a).toString());
        }
        A a3 = (A) this.f2498e;
        a3.Y(str);
        a3.Y("\r\n");
        int size = oVar.size();
        for (int i = 0; i < size; i++) {
            a3.Y(oVar.c(i));
            a3.Y(": ");
            a3.Y(oVar.g(i));
            a3.Y("\r\n");
        }
        a3.Y("\r\n");
        this.f2494a = 1;
    }
}
