package com.cool.bang.modules.generate.utils;

import com.cool.bang.common.constant.GenConstant;
import com.cool.bang.common.tools.string.StringUtil;
import com.cool.bang.modules.generate.domain.po.TableColumnInfo;
import com.cool.bang.modules.generate.domain.po.TableInfo;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Freemarker Process
 *
 * @author PeiZhi Guo
 * 2021-4-13 15:27
 */
public class FreeMarkerProcess {

    /**
     * 项目空间路径
     */
    private static final String PROJECT_PATH = "src/main/java";

    /**
     * mybatis空间路径
     */
    private static final String MYBATIS_PATH = "src/main/resources/mapper";

    /**
     * html空间路径
     */
    private static final String TEMPLATES_PATH = "web/admin/view";


    public static Map<String, Object> prepareContext(TableInfo table) {
        Map<String, Object> map = new HashMap<>();
        map.put("packageName", table.getPackageName());
        map.put("tableName", table.getTableName());
        map.put("functionName", StringUtil.isNotEmpty(table.getFunctionName()) ? table.getFunctionName() : "");
        map.put("ClassName", table.getClassName());
        map.put("className", StringUtil.uncapitalize(table.getClassName()));
        map.put("moduleName", table.getModuleName());
        map.put("businessName", table.getBusinessName());
        map.put("basePackage", getPackagePrefix(table.getPackageName()));
        map.put("author", table.getFunctionAuthor());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        map.put("datetime", sdf.format(new Date()));
        map.put("importList", getImportList(table));
        map.put("permissionPrefix", getPermissionPrefix(table.getModuleName(), table.getBusinessName()));
        map.put("columns", table.getColumns());
        map.put("table", table);
        return map;
    }

    /**
     * 获取模版
     *
     * @param tplCategory
     * @return
     */
    public static List<String> getTemplateList(String tplCategory) {
        List<String> templates = new ArrayList<String>();
        templates.add("controller.ftl");
        templates.add("service.ftl");
        templates.add("serviceImpl.ftl");
        templates.add("mapper.ftl");
        templates.add("domain.ftl");
        templates.add("mapper.xml.ftl");
        templates.add("main.html.ftl");
        templates.add("add.html.ftl");
        return templates;
    }

    /**
     * 获取文件名
     */
    public static String getFileName(String template, TableInfo table) {
        // 文件名称
        String fileName = "";
        // 包路径
        String packageName = table.getPackageName();
        // 模块名
        String moduleName = table.getModuleName();
        // 大写类名
        String className = table.getClassName();
        // 业务名称
        String businessName = table.getBusinessName();

        String javaPath = PROJECT_PATH + "/" + StringUtil.replace(packageName + "." + moduleName, ".", "/");
        String mybatisPath = MYBATIS_PATH + "/" + moduleName;
        String htmlPath = TEMPLATES_PATH + "/" + moduleName + "/" + businessName;

        if (template.contains("domain.ftl")) {
            fileName = StringUtil.format("{}/domain/{}.java", javaPath, className);
        } else if (template.contains("mapper.ftl")) {
            fileName = StringUtil.format("{}/mapper/{}Mapper.java", javaPath, className);
        } else if (template.contains("service.ftl")) {
            fileName = StringUtil.format("{}/service/I{}Service.java", javaPath, className);
        } else if (template.contains("serviceImpl.ftl")) {
            fileName = StringUtil.format("{}/service/impl/{}ServiceImpl.java", javaPath, className);
        } else if (template.contains("controller.ftl")) {
            fileName = StringUtil.format("{}/controller/{}Controller.java", javaPath, className);
        } else if (template.contains("mapper.xml.ftl")) {
            fileName = StringUtil.format("{}/{}Mapper.xml", mybatisPath, className);
        } else if (template.contains("main.html.ftl")) {
            fileName = StringUtil.format("{}/main.html", htmlPath);
        } else if (template.contains("add.html.ftl")) {
            fileName = StringUtil.format("{}/add.html", htmlPath);
        }
        return fileName;
    }

    /**
     * 获取包前缀
     *
     * @param packageName 包名称
     * @return 包前缀名称
     */
    public static String getPackagePrefix(String packageName) {
        int lastIndex = packageName.lastIndexOf(".");
        String basePackage = StringUtil.substring(packageName, 0, lastIndex);
        return basePackage;
    }

    /**
     * 根据列类型获取导入包
     *
     * @param table 业务表对象
     * @return 返回需要导入的包列表
     */
    public static Set<String> getImportList(TableInfo table) {
        List<TableColumnInfo> columns = table.getColumns();
        Set<String> importList = new HashSet<>();
        for (TableColumnInfo column : columns) {
            if (!column.isSuperColumn() && GenConstant.TYPE_DATE.equals(column.getJavaType())) {
                importList.add("java.util.Date");
            } else if (!column.isSuperColumn() && GenConstant.TYPE_BIG_DECIMAL.equals(column.getJavaType())) {
                importList.add("java.math.BigDecimal");
            }
        }
        return importList;
    }

    /**
     * 获取权限前缀
     *
     * @param moduleName   模块名称
     * @param businessName 业务名称
     * @return 返回权限前缀
     */
    public static String getPermissionPrefix(String moduleName, String businessName) {
        return StringUtil.format("{}:{}", moduleName, businessName);
    }
}
