package f;

import f.c;
import f.e;
import f.h;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
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 u {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final Map<Method, v<?>> f1747a = new ConcurrentHashMap();

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

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

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

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

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

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

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private final q f1753a = q.f();

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final Object[] f1754b = new Object[0];

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

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

        @Override // java.lang.reflect.InvocationHandler
        @Nullable
        public Object invoke(Object obj, Method method, @Nullable Object[] objArr) throws Throwable {
            if (method.getDeclaringClass() == Object.class) {
                return method.invoke(this, objArr);
            }
            if (objArr == null) {
                objArr = this.f1754b;
            }
            return this.f1753a.h(method) ? this.f1753a.g(method, this.f1755c, obj, objArr) : u.this.c(method).a(objArr);
        }
    }

    u(Call.Factory factory, HttpUrl httpUrl, List<h.a> list, List<e.a> list2, @Nullable Executor executor, boolean z) {
        this.f1748b = factory;
        this.f1749c = httpUrl;
        this.f1750d = list;
        this.f1751e = list2;
        this.f1752f = z;
    }

    private void j(Class<?> cls) {
        if (!cls.isInterface()) {
            throw new IllegalArgumentException("API declarations must be interfaces.");
        }
        ArrayDeque arrayDeque = new ArrayDeque(1);
        arrayDeque.add(cls);
        while (!arrayDeque.isEmpty()) {
            Class<?> cls2 = (Class) arrayDeque.removeFirst();
            if (cls2.getTypeParameters().length != 0) {
                StringBuilder sb = new StringBuilder("Type parameters are unsupported on ");
                sb.append(cls2.getName());
                if (cls2 != cls) {
                    sb.append(" which is an interface of ");
                    sb.append(cls.getName());
                }
                throw new IllegalArgumentException(sb.toString());
            }
            Collections.addAll(arrayDeque, cls2.getInterfaces());
        }
        if (this.f1752f) {
            q qVarF = q.f();
            for (Method method : cls.getDeclaredMethods()) {
                if (!qVarF.h(method) && !Modifier.isStatic(method.getModifiers())) {
                    c(method);
                }
            }
        }
    }

    public e<?, ?> a(Type type, Annotation[] annotationArr) {
        return d(null, type, annotationArr);
    }

    public <T> T b(Class<T> cls) {
        j(cls);
        return (T) Proxy.newProxyInstance(cls.getClassLoader(), new Class[]{cls}, new a(cls));
    }

    v<?> c(Method method) {
        v<?> vVarB;
        v<?> vVar = this.f1747a.get(method);
        if (vVar != null) {
            return vVar;
        }
        synchronized (this.f1747a) {
            vVarB = this.f1747a.get(method);
            if (vVarB == null) {
                vVarB = v.b(this, method);
                this.f1747a.put(method, vVarB);
            }
        }
        return vVarB;
    }

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

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

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

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

    public <T> h<ResponseBody, T> h(Type type, Annotation[] annotationArr) {
        return f(null, type, annotationArr);
    }

    public <T> h<T, String> i(Type type, Annotation[] annotationArr) {
        Objects.requireNonNull(type, "type == null");
        Objects.requireNonNull(annotationArr, "annotations == null");
        int size = this.f1750d.size();
        for (int i = 0; i < size; i++) {
            h<T, String> hVar = (h<T, String>) this.f1750d.get(i).e(type, annotationArr, this);
            if (hVar != null) {
                return hVar;
            }
        }
        return c.d.f1634a;
    }

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

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private final q f1757a;

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

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

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

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

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

        b(q qVar) {
            this.f1760d = new ArrayList();
            this.f1761e = new ArrayList();
            this.f1757a = qVar;
        }

        public b a(e.a aVar) {
            this.f1761e.add((e.a) Objects.requireNonNull(aVar, "factory == null"));
            return this;
        }

        public b b(h.a aVar) {
            this.f1760d.add((h.a) Objects.requireNonNull(aVar, "factory == null"));
            return this;
        }

        public b c(String str) {
            Objects.requireNonNull(str, "baseUrl == null");
            d(HttpUrl.get(str));
            return this;
        }

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

        public u e() {
            if (this.f1759c == null) {
                throw new IllegalStateException("Base URL required.");
            }
            Call.Factory okHttpClient = this.f1758b;
            if (okHttpClient == null) {
                okHttpClient = new OkHttpClient();
            }
            Call.Factory factory = okHttpClient;
            Executor executorB = this.f1762f;
            if (executorB == null) {
                executorB = this.f1757a.b();
            }
            Executor executor = executorB;
            ArrayList arrayList = new ArrayList(this.f1761e);
            arrayList.addAll(this.f1757a.a(executor));
            ArrayList arrayList2 = new ArrayList(this.f1760d.size() + 1 + this.f1757a.d());
            arrayList2.add(new c());
            arrayList2.addAll(this.f1760d);
            arrayList2.addAll(this.f1757a.c());
            return new u(factory, this.f1759c, Collections.unmodifiableList(arrayList2), Collections.unmodifiableList(arrayList), executor, this.g);
        }

        public b f(Call.Factory factory) {
            this.f1758b = (Call.Factory) Objects.requireNonNull(factory, "factory == null");
            return this;
        }

        public b g(OkHttpClient okHttpClient) {
            f((Call.Factory) Objects.requireNonNull(okHttpClient, "client == null"));
            return this;
        }

        public b() {
            this(q.f());
        }
    }
}
