package com.seewo.ota.api.udiserver;

import com.seewo.ota.api.udiserver.response.UdiResponse;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/* JADX INFO: loaded from: classes.dex */
public class UdiApiFactory extends com.seewo.ota.j.a.a<UdiServer, UdiServerApi, UdiArg, UdiApiInvoker, UdiParam> {
    private final Set<String> mApiNames = new HashSet();

    @Override // com.seewo.ota.j.a.a
    protected /* bridge */ /* synthetic */ UdiParam createParamsInfo(Class cls, Annotation annotation) {
        return createParamsInfo((Class<?>) cls, (UdiArg) annotation);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.seewo.ota.j.a.a
    public UdiApiInvoker createMethodInfo(Object obj, Method method, UdiServerApi udiServerApi, List<UdiParam> list) {
        String strValue = udiServerApi.value();
        if (strValue == null || strValue.trim().isEmpty()) {
            throw new IllegalArgumentException("empty api name on method:" + method.getName());
        }
        if (this.mApiNames.contains(strValue)) {
            throw new IllegalArgumentException("duplicate api name on method ,method:" + method.getName() + ",api:" + strValue);
        }
        Class<?> returnType = method.getReturnType();
        if (returnType != Void.TYPE && !UdiResponse.class.isAssignableFrom(returnType)) {
            throw new IllegalArgumentException("API can only return UdiResponse type, return type: " + returnType.getName() + ", method:" + method.getName() + ",api:" + udiServerApi.value());
        }
        if (udiServerApi.method() == UdiApiType.GET && returnType == Void.TYPE) {
            throw new IllegalArgumentException("GET API should not return void, method:" + method.getName() + ",api:" + udiServerApi.value());
        }
        if (udiServerApi.method() == UdiApiType.POST && list.isEmpty()) {
            throw new IllegalArgumentException("POST api requires at least one parameter, method:" + method.getName() + ",api:" + udiServerApi.value());
        }
        if (udiServerApi.method() == UdiApiType.GET && !list.isEmpty()) {
            throw new IllegalArgumentException("GET api not allow parameter, method:" + method.getName() + ",api:" + udiServerApi.value());
        }
        HashSet hashSet = new HashSet();
        UdiParam udiParam = null;
        for (UdiParam udiParam2 : list) {
            if (hashSet.contains(udiParam2.name())) {
                throw new IllegalArgumentException("duplicate parameter key:" + udiParam2.name() + ",method:" + method.getName() + ",api:" + udiServerApi.value());
            }
            hashSet.add(udiParam2.name());
            if (udiParam2.isToJson()) {
                udiParam = udiParam2;
            }
            if (udiParam2.getType().getClass().isPrimitive() || udiParam2.getType().getClass().isEnum()) {
                if (udiParam2.isToJson()) {
                    throw new IllegalArgumentException("primitive or enum parameter should not set toJson, param:" + udiParam2.name() + ",method:" + method.getName() + ",api:" + udiServerApi.value());
                }
            }
        }
        if (udiParam == null || list.size() <= 1) {
            this.mApiNames.add(strValue + "@" + udiServerApi.method());
            return new UdiApiInvoker(udiServerApi.method(), strValue, obj, method, list, returnType);
        }
        throw new IllegalArgumentException("Only one json parameter is allowed，key:" + udiParam.name() + ",method:" + method.getName() + ",api:" + udiServerApi.value());
    }

    protected UdiParam createParamsInfo(Class<?> cls, UdiArg udiArg) {
        if (!cls.isPrimitive() || !udiArg.nullable()) {
            return new UdiParam(udiArg.value(), udiArg.nullable(), cls, udiArg.toJson());
        }
        throw new IllegalArgumentException("primitive type can not declare as nullable. args:" + udiArg.value());
    }
}
