package j;

import j.a;
import j.c;
import j.e;
import j.o;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import javax.annotation.Nullable;
import okhttp3.Call;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/* JADX INFO: compiled from: Retrofit.java */
/* JADX INFO: loaded from: classes.dex */
public final class n {
    private final Map<Method, o<?, ?>> a = new ConcurrentHashMap();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    final Call.Factory f4368b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    final HttpUrl f4369c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    final List<e.a> f4370d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    final List<c.a> f4371e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    final boolean f4372f;

    /* JADX INFO: compiled from: Retrofit.java */
    class a implements InvocationHandler {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final k f4373b = k.d();

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        final /* synthetic */ Class f4374f;

        a(Class cls) {
            this.f4374f = cls;
        }

        @Override // java.lang.reflect.InvocationHandler
        public Object invoke(Object obj, Method method, @Nullable Object[] objArr) throws Throwable {
            if (method.getDeclaringClass() == Object.class) {
                return method.invoke(this, objArr);
            }
            if (this.f4373b.f(method)) {
                return this.f4373b.e(method, this.f4374f, obj, objArr);
            }
            o<?, ?> oVarF = n.this.f(method);
            return oVarF.f4382b.b(new i(oVarF, objArr));
        }
    }

    n(Call.Factory factory, HttpUrl httpUrl, List<e.a> list, List<c.a> list2, @Nullable Executor executor, boolean z) {
        this.f4368b = factory;
        this.f4369c = httpUrl;
        this.f4370d = Collections.unmodifiableList(list);
        this.f4371e = Collections.unmodifiableList(list2);
        this.f4372f = z;
    }

    private void e(Class<?> cls) {
        k kVarD = k.d();
        for (Method method : cls.getDeclaredMethods()) {
            if (!kVarD.f(method)) {
                f(method);
            }
        }
    }

    public HttpUrl a() {
        return this.f4369c;
    }

    public c<?, ?> b(Type type, Annotation[] annotationArr) {
        return g(null, type, annotationArr);
    }

    public Call.Factory c() {
        return this.f4368b;
    }

    public <T> T d(Class<T> cls) {
        p.s(cls);
        if (this.f4372f) {
            e(cls);
        }
        return (T) Proxy.newProxyInstance(cls.getClassLoader(), new Class[]{cls}, new a(cls));
    }

    o<?, ?> f(Method method) {
        o oVarA;
        o<?, ?> oVar = this.a.get(method);
        if (oVar != null) {
            return oVar;
        }
        synchronized (this.a) {
            oVarA = this.a.get(method);
            if (oVarA == null) {
                oVarA = new o.a(this, method).a();
                this.a.put(method, oVarA);
            }
        }
        return oVarA;
    }

    public c<?, ?> g(@Nullable c.a aVar, Type type, Annotation[] annotationArr) {
        p.b(type, "returnType == null");
        p.b(annotationArr, "annotations == null");
        int iIndexOf = this.f4371e.indexOf(aVar) + 1;
        int size = this.f4371e.size();
        for (int i2 = iIndexOf; i2 < size; i2++) {
            c<?, ?> cVarA = this.f4371e.get(i2).a(type, annotationArr, this);
            if (cVarA != null) {
                return cVarA;
            }
        }
        StringBuilder sb = new StringBuilder("Could not locate call adapter for ");
        sb.append(type);
        sb.append(".\n");
        if (aVar != null) {
            sb.append("  Skipped:");
            for (int i3 = 0; i3 < iIndexOf; i3++) {
                sb.append("\n   * ");
                sb.append(this.f4371e.get(i3).getClass().getName());
            }
            sb.append('\n');
        }
        sb.append("  Tried:");
        int size2 = this.f4371e.size();
        while (iIndexOf < size2) {
            sb.append("\n   * ");
            sb.append(this.f4371e.get(iIndexOf).getClass().getName());
            iIndexOf++;
        }
        throw new IllegalArgumentException(sb.toString());
    }

    public <T> e<T, RequestBody> h(@Nullable e.a aVar, Type type, Annotation[] annotationArr, Annotation[] annotationArr2) {
        p.b(type, "type == null");
        p.b(annotationArr, "parameterAnnotations == null");
        p.b(annotationArr2, "methodAnnotations == null");
        int iIndexOf = this.f4370d.indexOf(aVar) + 1;
        int size = this.f4370d.size();
        for (int i2 = iIndexOf; i2 < size; i2++) {
            e<T, RequestBody> eVar = (e<T, RequestBody>) this.f4370d.get(i2).a(type, annotationArr, annotationArr2, this);
            if (eVar != null) {
                return eVar;
            }
        }
        StringBuilder sb = new StringBuilder("Could not locate RequestBody converter for ");
        sb.append(type);
        sb.append(".\n");
        if (aVar != null) {
            sb.append("  Skipped:");
            for (int i3 = 0; i3 < iIndexOf; i3++) {
                sb.append("\n   * ");
                sb.append(this.f4370d.get(i3).getClass().getName());
            }
            sb.append('\n');
        }
        sb.append("  Tried:");
        int size2 = this.f4370d.size();
        while (iIndexOf < size2) {
            sb.append("\n   * ");
            sb.append(this.f4370d.get(iIndexOf).getClass().getName());
            iIndexOf++;
        }
        throw new IllegalArgumentException(sb.toString());
    }

    public <T> e<ResponseBody, T> i(@Nullable e.a aVar, Type type, Annotation[] annotationArr) {
        p.b(type, "type == null");
        p.b(annotationArr, "annotations == null");
        int iIndexOf = this.f4370d.indexOf(aVar) + 1;
        int size = this.f4370d.size();
        for (int i2 = iIndexOf; i2 < size; i2++) {
            e<ResponseBody, T> eVar = (e<ResponseBody, T>) this.f4370d.get(i2).b(type, annotationArr, this);
            if (eVar != null) {
                return eVar;
            }
        }
        StringBuilder sb = new StringBuilder("Could not locate ResponseBody converter for ");
        sb.append(type);
        sb.append(".\n");
        if (aVar != null) {
            sb.append("  Skipped:");
            for (int i3 = 0; i3 < iIndexOf; i3++) {
                sb.append("\n   * ");
                sb.append(this.f4370d.get(i3).getClass().getName());
            }
            sb.append('\n');
        }
        sb.append("  Tried:");
        int size2 = this.f4370d.size();
        while (iIndexOf < size2) {
            sb.append("\n   * ");
            sb.append(this.f4370d.get(iIndexOf).getClass().getName());
            iIndexOf++;
        }
        throw new IllegalArgumentException(sb.toString());
    }

    public <T> e<T, RequestBody> j(Type type, Annotation[] annotationArr, Annotation[] annotationArr2) {
        return h(null, type, annotationArr, annotationArr2);
    }

    public <T> e<ResponseBody, T> k(Type type, Annotation[] annotationArr) {
        return i(null, type, annotationArr);
    }

    public <T> e<T, String> l(Type type, Annotation[] annotationArr) {
        p.b(type, "type == null");
        p.b(annotationArr, "annotations == null");
        int size = this.f4370d.size();
        for (int i2 = 0; i2 < size; i2++) {
            e<T, String> eVar = (e<T, String>) this.f4370d.get(i2).c(type, annotationArr, this);
            if (eVar != null) {
                return eVar;
            }
        }
        return a.d.a;
    }

    /* JADX INFO: compiled from: Retrofit.java */
    public static final class b {
        private final k a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        @Nullable
        private Call.Factory f4376b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        private HttpUrl f4377c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        private final List<e.a> f4378d;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        private final List<c.a> f4379e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        @Nullable
        private Executor f4380f;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        private boolean f4381g;

        b(k kVar) {
            this.f4378d = new ArrayList();
            this.f4379e = new ArrayList();
            this.a = kVar;
            this.f4378d.add(new j.a());
        }

        public b a(c.a aVar) {
            List<c.a> list = this.f4379e;
            p.b(aVar, "factory == null");
            list.add(aVar);
            return this;
        }

        public b b(e.a aVar) {
            List<e.a> list = this.f4378d;
            p.b(aVar, "factory == null");
            list.add(aVar);
            return this;
        }

        public b c(String str) {
            p.b(str, "baseUrl == null");
            HttpUrl httpUrl = HttpUrl.parse(str);
            if (httpUrl != null) {
                d(httpUrl);
                return this;
            }
            throw new IllegalArgumentException("Illegal URL: " + str);
        }

        public b d(HttpUrl httpUrl) {
            p.b(httpUrl, "baseUrl == null");
            if ("".equals(httpUrl.pathSegments().get(r0.size() - 1))) {
                this.f4377c = httpUrl;
                return this;
            }
            throw new IllegalArgumentException("baseUrl must end in /: " + httpUrl);
        }

        public n e() {
            if (this.f4377c == null) {
                throw new IllegalStateException("Base URL required.");
            }
            Call.Factory okHttpClient = this.f4376b;
            if (okHttpClient == null) {
                okHttpClient = new OkHttpClient();
            }
            Call.Factory factory = okHttpClient;
            Executor executorB = this.f4380f;
            if (executorB == null) {
                executorB = this.a.b();
            }
            Executor executor = executorB;
            ArrayList arrayList = new ArrayList(this.f4379e);
            arrayList.add(this.a.a(executor));
            return new n(factory, this.f4377c, new ArrayList(this.f4378d), arrayList, executor, this.f4381g);
        }

        public b f(Call.Factory factory) {
            p.b(factory, "factory == null");
            this.f4376b = factory;
            return this;
        }

        public b g(OkHttpClient okHttpClient) {
            p.b(okHttpClient, "client == null");
            f(okHttpClient);
            return this;
        }

        public b() {
            this(k.d());
        }
    }
}
