package zq;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import kotlin.jvm.internal.t;
import sm.u;
import sm.z;
import uq.f0;
import uq.r;
import uq.v;

/* loaded from: classes3.dex */
public final class o {

    /* renamed from: j, reason: collision with root package name */
    public static final a f53718j = new a(null);

    /* renamed from: a, reason: collision with root package name */
    private final uq.a f53719a;

    /* renamed from: b, reason: collision with root package name */
    private final m f53720b;

    /* renamed from: c, reason: collision with root package name */
    private final uq.e f53721c;

    /* renamed from: d, reason: collision with root package name */
    private final boolean f53722d;

    /* renamed from: e, reason: collision with root package name */
    private final r f53723e;

    /* renamed from: f, reason: collision with root package name */
    private List f53724f;

    /* renamed from: g, reason: collision with root package name */
    private int f53725g;

    /* renamed from: h, reason: collision with root package name */
    private List f53726h;

    /* renamed from: i, reason: collision with root package name */
    private final List f53727i;

    /* loaded from: classes3.dex */
    public static final class a {
        private a() {
        }

        public /* synthetic */ a(kotlin.jvm.internal.k kVar) {
            this();
        }

        public final String a(InetSocketAddress inetSocketAddress) {
            t.g(inetSocketAddress, "<this>");
            InetAddress address = inetSocketAddress.getAddress();
            if (address == null) {
                String hostName = inetSocketAddress.getHostName();
                t.f(hostName, "hostName");
                return hostName;
            }
            String hostAddress = address.getHostAddress();
            t.f(hostAddress, "address.hostAddress");
            return hostAddress;
        }
    }

    /* loaded from: classes3.dex */
    public static final class b {

        /* renamed from: a, reason: collision with root package name */
        private final List f53728a;

        /* renamed from: b, reason: collision with root package name */
        private int f53729b;

        public b(List routes) {
            t.g(routes, "routes");
            this.f53728a = routes;
        }

        public final List a() {
            return this.f53728a;
        }

        public final boolean b() {
            return this.f53729b < this.f53728a.size();
        }

        public final f0 c() {
            if (!b()) {
                throw new NoSuchElementException();
            }
            List list = this.f53728a;
            int i10 = this.f53729b;
            this.f53729b = i10 + 1;
            return (f0) list.get(i10);
        }
    }

    public o(uq.a address, m routeDatabase, uq.e call, boolean z10, r eventListener) {
        List k10;
        List k11;
        t.g(address, "address");
        t.g(routeDatabase, "routeDatabase");
        t.g(call, "call");
        t.g(eventListener, "eventListener");
        this.f53719a = address;
        this.f53720b = routeDatabase;
        this.f53721c = call;
        this.f53722d = z10;
        this.f53723e = eventListener;
        k10 = u.k();
        this.f53724f = k10;
        k11 = u.k();
        this.f53726h = k11;
        this.f53727i = new ArrayList();
        f(address.l(), address.g());
    }

    private final boolean b() {
        return this.f53725g < this.f53724f.size();
    }

    private final Proxy d() {
        if (b()) {
            List list = this.f53724f;
            int i10 = this.f53725g;
            this.f53725g = i10 + 1;
            Proxy proxy = (Proxy) list.get(i10);
            e(proxy);
            return proxy;
        }
        throw new SocketException("No route to " + this.f53719a.l().i() + "; exhausted proxy configurations: " + this.f53724f);
    }

    private final void e(Proxy proxy) {
        String i10;
        int o10;
        List<InetAddress> a10;
        ArrayList arrayList = new ArrayList();
        this.f53726h = arrayList;
        if (proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.SOCKS) {
            i10 = this.f53719a.l().i();
            o10 = this.f53719a.l().o();
        } else {
            SocketAddress proxyAddress = proxy.address();
            if (!(proxyAddress instanceof InetSocketAddress)) {
                throw new IllegalArgumentException(("Proxy.address() is not an InetSocketAddress: " + proxyAddress.getClass()).toString());
            }
            a aVar = f53718j;
            t.f(proxyAddress, "proxyAddress");
            InetSocketAddress inetSocketAddress = (InetSocketAddress) proxyAddress;
            i10 = aVar.a(inetSocketAddress);
            o10 = inetSocketAddress.getPort();
        }
        boolean z10 = false;
        if (1 <= o10 && o10 < 65536) {
            z10 = true;
        }
        if (!z10) {
            throw new SocketException("No route to " + i10 + ':' + o10 + "; port is out of range");
        }
        if (proxy.type() == Proxy.Type.SOCKS) {
            arrayList.add(InetSocketAddress.createUnresolved(i10, o10));
            return;
        }
        if (vq.f.a(i10)) {
            a10 = sm.t.e(InetAddress.getByName(i10));
        } else {
            this.f53723e.dnsStart(this.f53721c, i10);
            a10 = this.f53719a.c().a(i10);
            if (a10.isEmpty()) {
                throw new UnknownHostException(this.f53719a.c() + " returned no addresses for " + i10);
            }
            this.f53723e.dnsEnd(this.f53721c, i10, a10);
        }
        if (this.f53722d) {
            a10 = g.a(a10);
        }
        Iterator<InetAddress> it = a10.iterator();
        while (it.hasNext()) {
            arrayList.add(new InetSocketAddress(it.next(), o10));
        }
    }

    private final void f(v vVar, Proxy proxy) {
        this.f53723e.proxySelectStart(this.f53721c, vVar);
        List<Proxy> g10 = g(proxy, vVar, this);
        this.f53724f = g10;
        this.f53725g = 0;
        this.f53723e.proxySelectEnd(this.f53721c, vVar, g10);
    }

    private static final List g(Proxy proxy, v vVar, o oVar) {
        List e10;
        if (proxy != null) {
            e10 = sm.t.e(proxy);
            return e10;
        }
        URI t10 = vVar.t();
        if (t10.getHost() == null) {
            return vq.p.k(Proxy.NO_PROXY);
        }
        List<Proxy> proxiesOrNull = oVar.f53719a.i().select(t10);
        List<Proxy> list = proxiesOrNull;
        if (list == null || list.isEmpty()) {
            return vq.p.k(Proxy.NO_PROXY);
        }
        t.f(proxiesOrNull, "proxiesOrNull");
        return vq.p.w(proxiesOrNull);
    }

    public final boolean a() {
        return b() || (this.f53727i.isEmpty() ^ true);
    }

    public final b c() {
        if (!a()) {
            throw new NoSuchElementException();
        }
        ArrayList arrayList = new ArrayList();
        while (b()) {
            Proxy d10 = d();
            Iterator it = this.f53726h.iterator();
            while (it.hasNext()) {
                f0 f0Var = new f0(this.f53719a, d10, (InetSocketAddress) it.next());
                if (this.f53720b.c(f0Var)) {
                    this.f53727i.add(f0Var);
                } else {
                    arrayList.add(f0Var);
                }
            }
            if (!arrayList.isEmpty()) {
                break;
            }
        }
        if (arrayList.isEmpty()) {
            z.A(arrayList, this.f53727i);
            this.f53727i.clear();
        }
        return new b(arrayList);
    }
}
