package cn.reghao.tnb.sb.util;

import cn.reghao.tnb.sb.db.autobatis.StringUtil;
import cn.reghao.tnb.sb.db.autobatis.annotation.field.Varchar;

import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author reghao
 * @date 2023-04-05 16:50:42
 */
public class ClassUtil {
    public static Object getObject(Class<?> clazz, String[] strs) throws Exception {
        Field[] fields = clazz.getDeclaredFields();
        if (strs.length != fields.length) {
            return null;
        }

        Object object = clazz.getDeclaredConstructor().newInstance();
        for (int i = 0; i < fields.length; i++) {
            Class<?> clazzType = fields[i].getType();
            if (clazzType.equals(String.class)) {
                fields[i].setAccessible(true);
                fields[i].set(object, strs[i]);
            } else {
                Object result;
                if (clazzType.equals(Long.class)) {
                    result = Long.parseLong(strs[i]);
                } else if (clazzType.equals(Integer.class)) {
                    result = Integer.parseInt(strs[i]);
                } else {
                    result = Double.parseDouble(strs[i]);
                }

                fields[i].setAccessible(true);
                fields[i].set(object, result);
            }
        }
        return object;
    }

    /**
     * 获取类名
     *
     * @date 2020-04-02 下午7:35
     */
    public static String classname(Class clazz) {
        String name = clazz.getName();
        int index = name.lastIndexOf(".");
        return name.substring(index+1);
    }

    public static Map<String, String> fieldAndTableName(Class clazz) {
        Map<String, String> map = new LinkedHashMap<>();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            String fieldName = field.getName();
            String underlineFieldName = StringUtil.separateByUnderline(field.getName());
            map.put(fieldName, underlineFieldName);
        }

        // TODO 递归遍历多个父类
        Class superClass= clazz.getSuperclass();
        if (!superClass.isAssignableFrom(Object.class)) {
            Field[] superFields = superClass.getDeclaredFields();
            for (Field field : superFields) {
                String fieldName = field.getName();
                String underlineFieldName = StringUtil.separateByUnderline(field.getName());
                map.put(fieldName, underlineFieldName);
            }
        }

        map.remove("serialVersionUID");
        return map;
    }

    public static Map<String, String> fieldType(Class clazz) {
        Map<String, String> map = new LinkedHashMap<>();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            String fieldName = StringUtil.separateByUnderline(field.getName());
            String typeName = field.getType().getName();
            Varchar varChar = field.getAnnotation(Varchar.class);
            if (varChar != null) {
                int size = varChar.value();
                map.put(fieldName, typeName + "," + size);
            } else {
                map.put(fieldName, typeName);
            }
        }

        return map;
    }
}
