package com.ag.secp.platform.spring;

import com.ag.util.ResourceUtil;
import com.ag.util.StrUtil;
import com.wb.util.WebUtil;
import org.json.JSONObject;
import org.springframework.util.ReflectionUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * Created by shaoqiang on 2017/8/17.
 */
public class PojoMange {
    /**
     * 请求对象。
     */
    public HttpServletRequest request;
    public HttpServletResponse response;
    public String bean;
    public String method;
    public Object params;
    public String pojoname;
    /**
     * status配置
     * 0=部署模式, 开启定时任务, 需打开modulelink.xml中的对moduleRefreshScheduler的注释
     * 1=读取外部jar包开发模式 : pom.xml需要删除扩展工程jar, 修改扩展工程后重打jar包,不需要重启主工程
     * 每次请求直接读取jar包, 没有安装jrebel组件使用.
     * 2=不读jar包开发模式 : pom.xml需要引入扩展工程jar, 建议安装了jrebel组件才使用.
     * 3=測試單RxtAction
     */
    private String statusStr = ResourceUtil.getString("system", "status");

    public Object execute() throws Exception {
        Integer status = 1;
        if (!StrUtil.isEmpty(statusStr)){
            status = Integer.parseInt(statusStr);
        }
        JSONObject fields = WebUtil.fetch(request);
        fields.put("mainParams", params);
        /**
         * 1.判断传入的bean是否是主工程管理的bean, 如果能获取到bean的方法对象, 不需要再读取外部jar包, 直接反射调用并退出.
         * 2.如果status=2, 主工程也会管理扩展工程中实现Action接口的bean, 需要通过AopTargetUtils工具类获取代理类中的目标bean对象.
         */
        Object springBean = null;
        Method methodObj = null;
        if (SpringApplicationContext.isContainsMain(bean)) {
            springBean = SpringApplicationContext.getBean(bean);
            //if (!(springBean instanceof Action)) {
                methodObj = ReflectionUtils.findMethod(springBean.getClass(), method, JSONObject.class, HttpServletResponse.class, HttpServletRequest.class,String.class);
                return ReflectionUtils.invokeMethod(methodObj, springBean, fields, response, request,pojoname);
            //}
        }

        //执行到以下代码, 表示目标bean在扩展工程中
        long on = System.currentTimeMillis();
        if (status == 0) {
            //部署模式
            String beanName = "moudlepojooMangerProcess";
            String defMethod = "deployModule";
            methodObj = ReflectionUtils.findMethod(SpringApplicationContext.getBean(beanName).getClass(), defMethod, JSONObject.class, HttpServletResponse.class, HttpServletRequest.class, String.class, String.class,String.class);
            return ReflectionUtils.invokeMethod(methodObj, SpringApplicationContext.getBean(beanName), fields, response, request, bean, method,pojoname);
        }
        if (status == 1) {
            //读取外部jar包的开发模式
            String beanName = "moudlepojoMangerProcess";
            String defMethod = "developerForJar";
            methodObj = ReflectionUtils.findMethod(SpringApplicationContext.getBean(beanName).getClass(), defMethod, JSONObject.class, HttpServletResponse.class, HttpServletRequest.class, String.class, String.class,String.class);
            return ReflectionUtils.invokeMethod(methodObj, SpringApplicationContext.getBean(beanName), fields, response, request, bean, method,pojoname);
        }
//        if (status == 2) {
//            //不读jar包的开发模式(pom.xml需要引入外部工程jar)
//            //获取代理对象中的目标对象
//            //第一个字母大写，第二个字母小写，把第一个字母转成小写
//            bean=(Character.isUpperCase(bean.charAt(0)) && Character.isLowerCase(bean.charAt(1))) ?
//                    String.valueOf(bean.charAt(0)).toLowerCase()+bean.substring(1):bean;
//            springBean = AopTargetUtils.getTarget(SpringApplicationContext.getBean(bean));
//            methodObj = ReflectionUtils.findMethod(springBean.getClass(), method, JSONObject.class, HttpServletResponse.class, HttpServletRequest.class);
//            return ReflectionUtils.invokeMethod(methodObj, springBean, fields, response, request);
//        }
        long end = System.currentTimeMillis();
        System.out.println("outside jar is run times============" + (end - on));
        return null;
    }

    public PojoMange() {
    }

    public PojoMange(HttpServletRequest request, HttpServletResponse response, String bean, String method, Object params, String pojoname) {
        this.request = request;
        this.response = response;
        this.bean = bean;
        this.method = method;
        this.params = params;
        this.pojoname=pojoname;
    }

}
