package j;

import com.ifpdos.sdk.httpd.binder.aidl.RequestLine;
import j.j;
import j.s.q;
import j.s.r;
import j.s.s;
import j.s.t;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import okhttp3.Call;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

/* JADX INFO: compiled from: ServiceMethod.java */
/* JADX INFO: loaded from: classes.dex */
final class o<R, T> {
    static final Pattern m = Pattern.compile("\\{([a-zA-Z][a-zA-Z0-9_-]*)\\}");
    static final Pattern n = Pattern.compile("[a-zA-Z][a-zA-Z0-9_-]*");
    final Call.Factory a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    final c<R, T> f4382b;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private final e<ResponseBody, R> f4384d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final String f4385e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final String f4386f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final Headers f4387g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private final MediaType f4388h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private final boolean f4389i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private final boolean f4390j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private final boolean f4391k;
    private final j<?>[] l;

    /* JADX INFO: compiled from: ServiceMethod.java */
    static final class a<T, R> {
        final n a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final Method f4392b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        final Annotation[] f4393c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        final Annotation[][] f4394d;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        final Type[] f4395e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        Type f4396f;

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

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        boolean f4398h;

        /* JADX INFO: renamed from: i, reason: collision with root package name */
        boolean f4399i;

        /* JADX INFO: renamed from: j, reason: collision with root package name */
        boolean f4400j;

        /* JADX INFO: renamed from: k, reason: collision with root package name */
        boolean f4401k;
        boolean l;
        String m;
        boolean n;
        boolean o;
        boolean p;
        String q;
        Headers r;
        MediaType s;
        Set<String> t;
        j<?>[] u;
        e<ResponseBody, T> v;
        c<T, R> w;

        a(n nVar, Method method) {
            this.a = nVar;
            this.f4392b = method;
            this.f4393c = method.getAnnotations();
            this.f4395e = method.getGenericParameterTypes();
            this.f4394d = method.getParameterAnnotations();
        }

        private c<T, R> b() {
            Type genericReturnType = this.f4392b.getGenericReturnType();
            if (p.l(genericReturnType)) {
                throw d("Method return type must not include a type variable or wildcard: %s", genericReturnType);
            }
            if (genericReturnType == Void.TYPE) {
                throw d("Service methods cannot return void.", new Object[0]);
            }
            try {
                return (c<T, R>) this.a.b(genericReturnType, this.f4392b.getAnnotations());
            } catch (RuntimeException e2) {
                throw e(e2, "Unable to create call adapter for %s", genericReturnType);
            }
        }

        private e<ResponseBody, T> c() {
            try {
                return this.a.k(this.f4396f, this.f4392b.getAnnotations());
            } catch (RuntimeException e2) {
                throw e(e2, "Unable to create converter for %s", this.f4396f);
            }
        }

        private RuntimeException d(String str, Object... objArr) {
            return e(null, str, objArr);
        }

        private RuntimeException e(Throwable th, String str, Object... objArr) {
            return new IllegalArgumentException(String.format(str, objArr) + "\n    for method " + this.f4392b.getDeclaringClass().getSimpleName() + "." + this.f4392b.getName(), th);
        }

        private RuntimeException f(int i2, String str, Object... objArr) {
            return d(str + " (parameter #" + (i2 + 1) + ")", objArr);
        }

        private RuntimeException g(Throwable th, int i2, String str, Object... objArr) {
            return e(th, str + " (parameter #" + (i2 + 1) + ")", objArr);
        }

        private Headers h(String[] strArr) {
            Headers.Builder builder = new Headers.Builder();
            for (String str : strArr) {
                int iIndexOf = str.indexOf(58);
                if (iIndexOf == -1 || iIndexOf == 0 || iIndexOf == str.length() - 1) {
                    throw d("@Headers value must be in the form \"Name: Value\". Found: \"%s\"", str);
                }
                String strSubstring = str.substring(0, iIndexOf);
                String strTrim = str.substring(iIndexOf + 1).trim();
                if ("Content-Type".equalsIgnoreCase(strSubstring)) {
                    MediaType mediaType = MediaType.parse(strTrim);
                    if (mediaType == null) {
                        throw d("Malformed content type: %s", strTrim);
                    }
                    this.s = mediaType;
                } else {
                    builder.add(strSubstring, strTrim);
                }
            }
            return builder.build();
        }

        private void i(String str, String str2, boolean z) {
            String str3 = this.m;
            if (str3 != null) {
                throw d("Only one HTTP method is allowed. Found: %s and %s.", str3, str);
            }
            this.m = str;
            this.n = z;
            if (str2.isEmpty()) {
                return;
            }
            int iIndexOf = str2.indexOf(63);
            if (iIndexOf != -1 && iIndexOf < str2.length() - 1) {
                String strSubstring = str2.substring(iIndexOf + 1);
                if (o.m.matcher(strSubstring).find()) {
                    throw d("URL query string \"%s\" must not have replace block. For dynamic query parameters use @Query.", strSubstring);
                }
            }
            this.q = str2;
            this.t = o.b(str2);
        }

        private void j(Annotation annotation) {
            if (annotation instanceof j.s.b) {
                i("DELETE", ((j.s.b) annotation).value(), false);
                return;
            }
            if (annotation instanceof j.s.f) {
                i(RequestLine.METHOD_GET, ((j.s.f) annotation).value(), false);
                return;
            }
            if (annotation instanceof j.s.g) {
                i("HEAD", ((j.s.g) annotation).value(), false);
                if (!Void.class.equals(this.f4396f)) {
                    throw d("HEAD method must use Void as response type.", new Object[0]);
                }
                return;
            }
            if (annotation instanceof j.s.l) {
                i("PATCH", ((j.s.l) annotation).value(), true);
                return;
            }
            if (annotation instanceof j.s.m) {
                i(RequestLine.METHOD_POST, ((j.s.m) annotation).value(), true);
                return;
            }
            if (annotation instanceof j.s.n) {
                i("PUT", ((j.s.n) annotation).value(), true);
                return;
            }
            if (annotation instanceof j.s.k) {
                i("OPTIONS", ((j.s.k) annotation).value(), false);
                return;
            }
            if (annotation instanceof j.s.h) {
                j.s.h hVar = (j.s.h) annotation;
                i(hVar.method(), hVar.path(), hVar.hasBody());
                return;
            }
            if (annotation instanceof j.s.j) {
                String[] strArrValue = ((j.s.j) annotation).value();
                if (strArrValue.length == 0) {
                    throw d("@Headers annotation is empty.", new Object[0]);
                }
                this.r = h(strArrValue);
                return;
            }
            if (annotation instanceof j.s.e) {
                if (this.p) {
                    throw d("Only one encoding annotation is allowed.", new Object[0]);
                }
                this.o = true;
            }
        }

        private j<?> k(int i2, Type type, Annotation[] annotationArr) {
            j<?> jVar = null;
            for (Annotation annotation : annotationArr) {
                j<?> jVarL = l(i2, type, annotationArr, annotation);
                if (jVarL != null) {
                    if (jVar != null) {
                        throw f(i2, "Multiple Retrofit annotations found, only one allowed.", new Object[0]);
                    }
                    jVar = jVarL;
                }
            }
            if (jVar != null) {
                return jVar;
            }
            throw f(i2, "No Retrofit annotation found.", new Object[0]);
        }

        private j<?> l(int i2, Type type, Annotation[] annotationArr, Annotation annotation) {
            if (annotation instanceof q) {
                if (this.f4401k) {
                    throw f(i2, "A @Path parameter must not come after a @Query.", new Object[0]);
                }
                if (this.l) {
                    throw f(i2, "@Path parameters may not be used with @Url.", new Object[0]);
                }
                if (this.q == null) {
                    throw f(i2, "@Path can only be used with relative url on @%s", this.m);
                }
                this.f4400j = true;
                q qVar = (q) annotation;
                String strValue = qVar.value();
                m(i2, strValue);
                return new j.i(strValue, this.a.l(type, annotationArr), qVar.encoded());
            }
            if (annotation instanceof r) {
                r rVar = (r) annotation;
                String strValue2 = rVar.value();
                boolean zEncoded = rVar.encoded();
                Class<?> clsJ = p.j(type);
                this.f4401k = true;
                if (!Iterable.class.isAssignableFrom(clsJ)) {
                    return clsJ.isArray() ? new j.C0137j(strValue2, this.a.l(o.a(clsJ.getComponentType()), annotationArr), zEncoded).b() : new j.C0137j(strValue2, this.a.l(type, annotationArr), zEncoded);
                }
                if (type instanceof ParameterizedType) {
                    return new j.C0137j(strValue2, this.a.l(p.i(0, (ParameterizedType) type), annotationArr), zEncoded).c();
                }
                throw f(i2, clsJ.getSimpleName() + " must include generic type (e.g., " + clsJ.getSimpleName() + "<String>)", new Object[0]);
            }
            if (annotation instanceof t) {
                boolean zEncoded2 = ((t) annotation).encoded();
                Class<?> clsJ2 = p.j(type);
                this.f4401k = true;
                if (!Iterable.class.isAssignableFrom(clsJ2)) {
                    return clsJ2.isArray() ? new j.l(this.a.l(o.a(clsJ2.getComponentType()), annotationArr), zEncoded2).b() : new j.l(this.a.l(type, annotationArr), zEncoded2);
                }
                if (type instanceof ParameterizedType) {
                    return new j.l(this.a.l(p.i(0, (ParameterizedType) type), annotationArr), zEncoded2).c();
                }
                throw f(i2, clsJ2.getSimpleName() + " must include generic type (e.g., " + clsJ2.getSimpleName() + "<String>)", new Object[0]);
            }
            if (annotation instanceof s) {
                Class<?> clsJ3 = p.j(type);
                if (!Map.class.isAssignableFrom(clsJ3)) {
                    throw f(i2, "@QueryMap parameter type must be Map.", new Object[0]);
                }
                Type typeK = p.k(type, clsJ3, Map.class);
                if (!(typeK instanceof ParameterizedType)) {
                    throw f(i2, "Map must include generic types (e.g., Map<String, String>)", new Object[0]);
                }
                ParameterizedType parameterizedType = (ParameterizedType) typeK;
                Type typeI = p.i(0, parameterizedType);
                if (String.class == typeI) {
                    return new j.k(this.a.l(p.i(1, parameterizedType), annotationArr), ((s) annotation).encoded());
                }
                throw f(i2, "@QueryMap keys must be of type String: " + typeI, new Object[0]);
            }
            if (annotation instanceof j.s.i) {
                String strValue3 = ((j.s.i) annotation).value();
                Class<?> clsJ4 = p.j(type);
                if (!Iterable.class.isAssignableFrom(clsJ4)) {
                    return clsJ4.isArray() ? new j.f(strValue3, this.a.l(o.a(clsJ4.getComponentType()), annotationArr)).b() : new j.f(strValue3, this.a.l(type, annotationArr));
                }
                if (type instanceof ParameterizedType) {
                    return new j.f(strValue3, this.a.l(p.i(0, (ParameterizedType) type), annotationArr)).c();
                }
                throw f(i2, clsJ4.getSimpleName() + " must include generic type (e.g., " + clsJ4.getSimpleName() + "<String>)", new Object[0]);
            }
            if (annotation instanceof j.s.c) {
                if (!this.o) {
                    throw f(i2, "@Field parameters can only be used with form encoding.", new Object[0]);
                }
                j.s.c cVar = (j.s.c) annotation;
                String strValue4 = cVar.value();
                boolean zEncoded3 = cVar.encoded();
                this.f4397g = true;
                Class<?> clsJ5 = p.j(type);
                if (!Iterable.class.isAssignableFrom(clsJ5)) {
                    return clsJ5.isArray() ? new j.d(strValue4, this.a.l(o.a(clsJ5.getComponentType()), annotationArr), zEncoded3).b() : new j.d(strValue4, this.a.l(type, annotationArr), zEncoded3);
                }
                if (type instanceof ParameterizedType) {
                    return new j.d(strValue4, this.a.l(p.i(0, (ParameterizedType) type), annotationArr), zEncoded3).c();
                }
                throw f(i2, clsJ5.getSimpleName() + " must include generic type (e.g., " + clsJ5.getSimpleName() + "<String>)", new Object[0]);
            }
            if (annotation instanceof j.s.d) {
                if (!this.o) {
                    throw f(i2, "@FieldMap parameters can only be used with form encoding.", new Object[0]);
                }
                Class<?> clsJ6 = p.j(type);
                if (!Map.class.isAssignableFrom(clsJ6)) {
                    throw f(i2, "@FieldMap parameter type must be Map.", new Object[0]);
                }
                Type typeK2 = p.k(type, clsJ6, Map.class);
                if (!(typeK2 instanceof ParameterizedType)) {
                    throw f(i2, "Map must include generic types (e.g., Map<String, String>)", new Object[0]);
                }
                ParameterizedType parameterizedType2 = (ParameterizedType) typeK2;
                Type typeI2 = p.i(0, parameterizedType2);
                if (String.class == typeI2) {
                    e<T, String> eVarL = this.a.l(p.i(1, parameterizedType2), annotationArr);
                    this.f4397g = true;
                    return new j.e(eVarL, ((j.s.d) annotation).encoded());
                }
                throw f(i2, "@FieldMap keys must be of type String: " + typeI2, new Object[0]);
            }
            if (!(annotation instanceof j.s.o)) {
                if (!(annotation instanceof j.s.p)) {
                    if (!(annotation instanceof j.s.a)) {
                        return null;
                    }
                    if (this.o || this.p) {
                        throw f(i2, "@Body parameters cannot be used with form or multi-part encoding.", new Object[0]);
                    }
                    if (this.f4399i) {
                        throw f(i2, "Multiple @Body method annotations found.", new Object[0]);
                    }
                    try {
                        e<T, RequestBody> eVarJ = this.a.j(type, annotationArr, this.f4393c);
                        this.f4399i = true;
                        return new j.c(eVarJ);
                    } catch (RuntimeException e2) {
                        throw g(e2, i2, "Unable to create @Body converter for %s", type);
                    }
                }
                if (!this.p) {
                    throw f(i2, "@PartMap parameters can only be used with multipart encoding.", new Object[0]);
                }
                this.f4398h = true;
                Class<?> clsJ7 = p.j(type);
                if (!Map.class.isAssignableFrom(clsJ7)) {
                    throw f(i2, "@PartMap parameter type must be Map.", new Object[0]);
                }
                Type typeK3 = p.k(type, clsJ7, Map.class);
                if (!(typeK3 instanceof ParameterizedType)) {
                    throw f(i2, "Map must include generic types (e.g., Map<String, String>)", new Object[0]);
                }
                ParameterizedType parameterizedType3 = (ParameterizedType) typeK3;
                Type typeI3 = p.i(0, parameterizedType3);
                if (String.class == typeI3) {
                    Type typeI4 = p.i(1, parameterizedType3);
                    if (MultipartBody.Part.class.isAssignableFrom(p.j(typeI4))) {
                        throw f(i2, "@PartMap values cannot be MultipartBody.Part. Use @Part List<Part> or a different value type instead.", new Object[0]);
                    }
                    return new j.h(this.a.j(typeI4, annotationArr, this.f4393c), ((j.s.p) annotation).encoding());
                }
                throw f(i2, "@PartMap keys must be of type String: " + typeI3, new Object[0]);
            }
            if (!this.p) {
                throw f(i2, "@Part parameters can only be used with multipart encoding.", new Object[0]);
            }
            j.s.o oVar = (j.s.o) annotation;
            this.f4398h = true;
            String strValue5 = oVar.value();
            Class<?> clsJ8 = p.j(type);
            if (strValue5.isEmpty()) {
                if (!Iterable.class.isAssignableFrom(clsJ8)) {
                    if (clsJ8.isArray()) {
                        if (MultipartBody.Part.class.isAssignableFrom(clsJ8.getComponentType())) {
                            return j.m.a.b();
                        }
                        throw f(i2, "@Part annotation must supply a name or use MultipartBody.Part parameter type.", new Object[0]);
                    }
                    if (MultipartBody.Part.class.isAssignableFrom(clsJ8)) {
                        return j.m.a;
                    }
                    throw f(i2, "@Part annotation must supply a name or use MultipartBody.Part parameter type.", new Object[0]);
                }
                if (type instanceof ParameterizedType) {
                    if (MultipartBody.Part.class.isAssignableFrom(p.j(p.i(0, (ParameterizedType) type)))) {
                        return j.m.a.c();
                    }
                    throw f(i2, "@Part annotation must supply a name or use MultipartBody.Part parameter type.", new Object[0]);
                }
                throw f(i2, clsJ8.getSimpleName() + " must include generic type (e.g., " + clsJ8.getSimpleName() + "<String>)", new Object[0]);
            }
            Headers headersOf = Headers.of("Content-Disposition", "form-data; name=\"" + strValue5 + "\"", "Content-Transfer-Encoding", oVar.encoding());
            if (!Iterable.class.isAssignableFrom(clsJ8)) {
                if (!clsJ8.isArray()) {
                    if (MultipartBody.Part.class.isAssignableFrom(clsJ8)) {
                        throw f(i2, "@Part parameters using the MultipartBody.Part must not include a part name in the annotation.", new Object[0]);
                    }
                    return new j.g(headersOf, this.a.j(type, annotationArr, this.f4393c));
                }
                Class<?> clsA = o.a(clsJ8.getComponentType());
                if (MultipartBody.Part.class.isAssignableFrom(clsA)) {
                    throw f(i2, "@Part parameters using the MultipartBody.Part must not include a part name in the annotation.", new Object[0]);
                }
                return new j.g(headersOf, this.a.j(clsA, annotationArr, this.f4393c)).b();
            }
            if (type instanceof ParameterizedType) {
                Type typeI5 = p.i(0, (ParameterizedType) type);
                if (MultipartBody.Part.class.isAssignableFrom(p.j(typeI5))) {
                    throw f(i2, "@Part parameters using the MultipartBody.Part must not include a part name in the annotation.", new Object[0]);
                }
                return new j.g(headersOf, this.a.j(typeI5, annotationArr, this.f4393c)).c();
            }
            throw f(i2, clsJ8.getSimpleName() + " must include generic type (e.g., " + clsJ8.getSimpleName() + "<String>)", new Object[0]);
        }

        private void m(int i2, String str) {
            if (!o.n.matcher(str).matches()) {
                throw f(i2, "@Path parameter name must match %s. Found: %s", o.m.pattern(), str);
            }
            if (!this.t.contains(str)) {
                throw f(i2, "URL \"%s\" does not contain \"{%s}\".", this.q, str);
            }
        }

        public o a() {
            c<T, R> cVarB = b();
            this.w = cVarB;
            Type typeA = cVarB.a();
            this.f4396f = typeA;
            if (typeA == m.class || typeA == Response.class) {
                throw d("'" + p.j(this.f4396f).getName() + "' is not a valid response body type. Did you mean ResponseBody?", new Object[0]);
            }
            this.v = c();
            for (Annotation annotation : this.f4393c) {
                j(annotation);
            }
            if (this.m == null) {
                throw d("HTTP method annotation is required (e.g., @GET, @POST, etc.).", new Object[0]);
            }
            if (!this.n) {
                if (this.p) {
                    throw d("Multipart can only be specified on HTTP methods with request body (e.g., @POST).", new Object[0]);
                }
                if (this.o) {
                    throw d("FormUrlEncoded can only be specified on HTTP methods with request body (e.g., @POST).", new Object[0]);
                }
            }
            int length = this.f4394d.length;
            this.u = new j[length];
            for (int i2 = 0; i2 < length; i2++) {
                Type type = this.f4395e[i2];
                if (p.l(type)) {
                    throw f(i2, "Parameter type must not include a type variable or wildcard: %s", type);
                }
                Annotation[] annotationArr = this.f4394d[i2];
                if (annotationArr == null) {
                    throw f(i2, "No Retrofit annotation found.", new Object[0]);
                }
                this.u[i2] = k(i2, type, annotationArr);
            }
            if (this.q == null && !this.l) {
                throw d("Missing either @%s URL or @Url parameter.", this.m);
            }
            if (!this.o && !this.p && !this.n && this.f4399i) {
                throw d("Non-body HTTP method cannot contain @Body.", new Object[0]);
            }
            if (this.o && !this.f4397g) {
                throw d("Form-encoded method must contain at least one @Field.", new Object[0]);
            }
            if (!this.p || this.f4398h) {
                return new o(this);
            }
            throw d("Multipart method must contain at least one @Part.", new Object[0]);
        }
    }

    o(a<R, T> aVar) {
        this.a = aVar.a.c();
        this.f4382b = aVar.w;
        this.f4383c = aVar.a.a();
        this.f4384d = aVar.v;
        this.f4385e = aVar.m;
        this.f4386f = aVar.q;
        this.f4387g = aVar.r;
        this.f4388h = aVar.s;
        this.f4389i = aVar.n;
        this.f4390j = aVar.o;
        this.f4391k = aVar.p;
        this.l = aVar.u;
    }

    static Class<?> a(Class<?> cls) {
        return Boolean.TYPE == cls ? Boolean.class : Byte.TYPE == cls ? Byte.class : Character.TYPE == cls ? Character.class : Double.TYPE == cls ? Double.class : Float.TYPE == cls ? Float.class : Integer.TYPE == cls ? Integer.class : Long.TYPE == cls ? Long.class : Short.TYPE == cls ? Short.class : cls;
    }

    static Set<String> b(String str) {
        Matcher matcher = m.matcher(str);
        LinkedHashSet linkedHashSet = new LinkedHashSet();
        while (matcher.find()) {
            linkedHashSet.add(matcher.group(1));
        }
        return linkedHashSet;
    }

    Request c(@Nullable Object... objArr) throws IOException {
        l lVar = new l(this.f4385e, this.f4383c, this.f4386f, this.f4387g, this.f4388h, this.f4389i, this.f4390j, this.f4391k);
        j<?>[] jVarArr = this.l;
        int length = objArr != null ? objArr.length : 0;
        if (length == jVarArr.length) {
            for (int i2 = 0; i2 < length; i2++) {
                jVarArr[i2].a(lVar, objArr[i2]);
            }
            return lVar.g();
        }
        throw new IllegalArgumentException("Argument count (" + length + ") doesn't match expected count (" + jVarArr.length + ")");
    }

    R d(ResponseBody responseBody) throws IOException {
        return this.f4384d.a(responseBody);
    }
}
