package seu.socket;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;

@Component
public class RequestRouter {
    //TODO: 测试
    private ApplicationContext context;

    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    public ServerResponse getResponse(ClientRequest clientRequest) {
//        String requestType = clientRequest.getRequestType();

//        switch (requestType) {
//            case "getAll":
//                return nonParam(clientRequest.getServiceName(), clientRequest.getMethodName());
//            case "getOne":
//            case "delete":
////                return intParam(clientRequest.getServiceName(), clientRequest.getMethodName(), Integer.class, clientRequest.getParam());
//            case "update":
//            case "add":
//                return objParam(clientRequest.getServiceName(), clientRequest.getMethodName(), Object.class, clientRequest.getParam());
//        }
        return new ServerResponse(invokeMethod(clientRequest.getServiceName(), clientRequest.getMethodName(), clientRequest.getParamTypes(), clientRequest.getParams()));
    }

//    public Object nonParam(String serviceName, String methodName) {
//        Method method = ReflectionUtils.findMethod(context.getBean(serviceName).getClass(), methodName);
//        return ReflectionUtils.invokeMethod(method, context.getBean(serviceName));
//    }
//
//    public Object objParam(String serviceName, String methodName, Class aClass, Object param) {
//        Method method = ReflectionUtils.findMethod(context.getBean(serviceName).getClass(), methodName, aClass);
//        return ReflectionUtils.invokeMethod(method, context.getBean(serviceName), aClass, param);
//    }

    private Object invokeMethod(String beanName, String methodName, Class<?>[] paramTypes, Object[] params) {
        Method method;
        if (paramTypes == null || params == null) {
            method = ReflectionUtils.findMethod(context.getBean(beanName).getClass(), methodName);
            return ReflectionUtils.invokeMethod(method, context.getBean(beanName));
        } else {
            method = ReflectionUtils.findMethod(context.getBean(beanName).getClass(), methodName, paramTypes);
            return ReflectionUtils.invokeMethod(method, context.getBean(beanName), params);
        }
    }

//    public Object intParam(String serviceName, String methodName, Class aClass, Object param) {
//        Method method = ReflectionUtils.findMethod(context.getBean(serviceName).getClass(), methodName, aClass);
//        return ReflectionUtils.invokeMethod(method, context.getBean(serviceName), aClass, param);
//    }
}

@@ -309,22 +309,8 @@
 r %7B%0A
-    //TODO: %E6%B5%8B%E8%AF%95
 %0A   
@@ -531,1401 +531,178 @@
 ) %7B%0A
-//        String requestType = clientRequest.getRequestType();%0A%0A//        switch (requestType) %7B%0A//            case %22getAll%22:%0A//                return nonParam(clientRequest.getServiceName(), clientRequest.getMethodName());%0A//            case %22getOne%22:%0A//            case %22delete%22:%0A////                return intParam(clientRequest.getServiceName(), clientRequest.getMethodName(), Integer.class, clientRequest.getParam());%0A//            case %22update%22:%0A//            case %22add%22:%0A//                return objParam(clientRequest.getServiceName(), clientRequest.getMethodName(), Object.class, clientRequest.getParam());%0A//        %7D%0A        return new ServerResponse(invokeMethod(clientRequest.getServiceName(), clientRequest.getMethodName(), clientRequest.getParamTypes(), clientRequest.getParams()));%0A    %7D%0A%0A//    public Object nonParam(String serviceName, String methodName) %7B%0A//        Method method = ReflectionUtils.findMethod(context.getBean(serviceName).getClass(), methodName);%0A//        return ReflectionUtils.invokeMethod(method, context.getBean(serviceName));%0A//    %7D%0A//%0A//    public Object objParam(String serviceName, String methodName, Class aClass, Object param) %7B%0A//        Method method = ReflectionUtils.findMethod(context.getBean(serviceName).getClass(), methodName, aClass);%0A//        return ReflectionUtils.invokeMethod(method, context.getBean(serviceName), aClass, param);%0A//
+        return new ServerResponse(invokeMethod(clientRequest.getServiceName(), clientRequest.getMethodName(), clientRequest.getParamTypes(), clientRequest.getParams()));%0A
     
@@ -1311,329 +1311,7 @@
    %7D
-%0A%0A//    public Object intParam(String serviceName, String methodName, Class aClass, Object param) %7B%0A//        Method method = ReflectionUtils.findMethod(context.getBean(serviceName).getClass(), methodName, aClass);%0A//        return ReflectionUtils.invokeMethod(method, context.getBean(serviceName), aClass, param);%0A//    %7D
 %0A%7D%0A

delete todo of request router