package cn.com.capcloud.api.action.service.util;

import cn.com.capcloud.ccui.core.common.json.JackJSONArray;
import cn.com.capcloud.ccui.core.common.json.JackJSONObject;
import cn.com.capcloud.core.noentitydb.JtPage;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author LiaoJiaHui
 * @Create 2026/04/27
 */
public class ReflectionUtil {

    public static List<Map<String, Object>> mySelfMethodRetListMap(String actionName, String methodName, String jsonStr, JackJSONObject retJson, HttpServletRequest request) {
        try {
            Class actionClass = Class.forName(actionName);
            Object myActionClassInstance=actionClass.newInstance();
            java.lang.reflect.Method method = actionClass.getMethod(methodName,String.class,JackJSONObject.class,HttpServletRequest.class);
            Object retObj=method.invoke(myActionClassInstance,jsonStr,retJson,request);
            return (List<Map<String, Object>>) retObj;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
    /**
     * 返回类型是JtPage
     */
    public static JtPage mySelfMethodRetJtPage(String actionName, String methodName, String jsonStr, JackJSONObject retJson, HttpServletRequest request, JtPage jtPage) {
        try {
            Class actionClass = Class.forName(actionName);
            Object myActionClassInstance=actionClass.newInstance();
            java.lang.reflect.Method method = actionClass.getMethod(methodName,String.class,JackJSONObject.class,HttpServletRequest.class,JtPage.class);
            Object retObj=method.invoke(myActionClassInstance,jsonStr,retJson,request,jtPage);
            return (JtPage) retObj;
        } catch (Exception e) {
            e.printStackTrace();
            return new JtPage();
        }
    }
    /**
     * 返回类型是JSONArray
     */
    public static JackJSONArray mySelfMethodRetListMap(String actionName, String methodName, String jsonStr, JackJSONObject retJson, HttpServletRequest request, JackJSONArray array) {
        try {
            Class actionClass = Class.forName(actionName);
            Object myActionClassInstance=actionClass.newInstance();
            java.lang.reflect.Method method = actionClass.getMethod(methodName,String.class,JackJSONObject.class,HttpServletRequest.class,JackJSONArray.class);
            Object retObj=method.invoke(myActionClassInstance,jsonStr,retJson,request,array);
            return (JackJSONArray) retObj;
        } catch (Exception e) {
            e.printStackTrace();
            return new JackJSONArray();
        }
    }
    public static Map<String, Object> mySelfMethodRetMap(String actionName, String methodName, String jsonStr, JackJSONObject retJson, HttpServletRequest request) {
        try {
            Class actionClass = Class.forName(actionName);
            Object myActionClassInstance=actionClass.newInstance();

            java.lang.reflect.Method method = actionClass.getMethod(methodName,String.class,JackJSONObject.class,HttpServletRequest.class);
            Object retObj=method.invoke(myActionClassInstance,jsonStr,retJson,request);
            return (Map<String, Object>) retObj;
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap<>();
        }
    }
    public static Map<String, Object> mySelfMethodRetMap(String actionName,String methodName, String jsonStr, JackJSONObject retJson, HttpServletRequest request, Map<String,Object> map) {
        try {
            Class actionClass = Class.forName(actionName);
            Object myActionClassInstance=actionClass.newInstance();
            java.lang.reflect.Method method = actionClass.getMethod(methodName,String.class,JackJSONObject.class,HttpServletRequest.class, Map.class);
            Object retObj=method.invoke(myActionClassInstance,jsonStr,retJson,request,map);
            return (Map<String, Object>) retObj;
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap<>();
        }
    }
    public static String mySelfMethodRetStr(String actionName,String methodName, String jsonStr, JackJSONObject retJson, HttpServletRequest request) {
        try {
            Class actionClass = Class.forName(actionName);
            Object myActionClassInstance=actionClass.newInstance();
            Method method = null;
            try {
                method = actionClass.getMethod(methodName,String.class, JackJSONObject.class, HttpServletRequest.class);
            } catch (NoSuchMethodException e) {
                retJson.put("status","no_code");//后续补编码状态
                retJson.put("message","异常，扩展类找不多对应的方法，请检查 \""+methodName+"\" 方法是否正确，或是否更新");
                return "-1";
            } catch (SecurityException e) {
                return "-1";
            }
            Object retObj=method.invoke(myActionClassInstance,jsonStr,retJson,request);
            return String.valueOf(retObj);
        } catch (Exception e) {
            e.printStackTrace();
            return "-1";
        }
    }
    public static boolean isMethod(String actionClass, String methodName) {
        try {
            Class obj = Class.forName(actionClass);
            Class[] paraTypes = new Class[]{String.class,JackJSONObject.class,HttpServletRequest.class};
            return judgeMethodIsExist(obj.getDeclaredConstructor().newInstance(),methodName,paraTypes);
        } catch (Exception e) {
            return false;
        }
    }
    public static boolean judgeMethodIsExist(Object obj, String methodName, Class[] paraTypes) {
        boolean result = true;
        try {
            if (null != obj) {
                Method method =  obj.getClass().getMethod(methodName, paraTypes);
            }
        } catch (NoSuchMethodException ex) {
            result = false;
        }
        return result;
    }

}
