package com.heyu.api.data.utils;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ExpressionParse {

    public static <T> T getValue(String expression) {
        String className = "";
        String methodName = "";
        List<String> paramsList = new ArrayList<>();
        Object result = null;
        try {
            int index = expression.lastIndexOf(".");
            className = expression.substring(0, index);
            System.out.println();
            String methodNameParams = expression.substring(index + 1);

            String methodNameParamArrays[] = methodNameParams.split("\\(");
            methodName = methodNameParamArrays[0];
            String params = methodNameParamArrays[1];
            System.out.println();
            String realParams[] = params.split("\\)");

            if (realParams.length > 0) {
                String realParam = realParams[0];
                String[] ps = realParam.split(",");
                for (String p : ps) {
                    paramsList.add(p);
                }
            }
            Class clazz = Class.forName(className);
            Method methods[] = clazz.getDeclaredMethods();

            for (Method method : methods) {
                if (method.getName().equals(methodName)
                        && method.getParameterTypes().length == paramsList.size()) {
                    method.setAccessible(true);
                    Object pas[] = new Object[paramsList.size()];
                    if (method.getParameterTypes().length > 0) {
                        for (int i = 0; i < paramsList.size(); i++) {
                            String paramValue = paramsList.get(i);
                            if (paramValue.startsWith("'") && paramValue.endsWith("'")) {
                                paramValue = paramValue.substring(1, paramValue.length() - 1);
                            }
                            pas[i] = BeanUtil.parseBasicTypeWrapper(method.getParameterTypes()[i], paramValue);
                        }
                    }
                    if (Modifier.isStatic(method.getModifiers())) {
                        result = method.invoke(null, pas);
                    } else {
                        result = method.invoke(clazz.newInstance(), pas);
                    }
                    return (T) BeanUtil.parseBasicTypeWrapper(method.getReturnType(), result);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            log.info("ExpressionParse.getValue className:{},methodName:{},paramsList:{},result:{},expression:{}",
                    className, methodName, paramsList, result, expression);
        }
        return null;

    }

    public static void main(String[] args) throws Exception {
        String a = "com.heyu.api.data.utils.SqlDateParse.addDaysReturnTimestamp('yyyy-MM-dd 00:00:00',-1)";

        String b = "com.heyu.api.data.utils.SqlDateParse.addDaysReturnTimestamp('yyyy-MM-dd 23:59:59',-1)";


        Long aTime= getValue(a);
        Long bTime = getValue(b);



        System.out.println(aTime);
        System.out.println(bTime);
    }
}
