package cn.feast.coding.generator.utils;

import cn.feast.coding.model.generator.Project;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Map;

public class CodeGenerator {
    /**
     * 根据模板生成
     *
     * @param map 数据
     */
    public static void generator(Map<String, Object> map){
        Project project = (Project) map.get("project");
        String fileName = "OpenPark";
        if(StringUtils.isNotEmpty(project.getProjectFileName())){
            fileName = project.getProjectFileName();
        }
        //获取request
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String root = request.getSession().getServletContext().getRealPath("/"+ fileName);
        generator(map, root, request);
    }

    /**
     * 根据模板生成
     *
     * @param map   数据
     * @param path  生成路径
     */
    public static void generator(Map<String, Object> map, String path, HttpServletRequest request) {
        try {
            String templateName = map.get("templateName").toString();
            String filePath = map.get("filePath").toString();
            Configuration cfg = new Configuration();
            cfg.setDefaultEncoding("UTF-8");
            //自定义标签
//            cfg.setSharedVariable("role", myDirectiveModel);
//            cfg.setSharedVariable("modules", modulesDirectiveModel);
            cfg.setServletContextForTemplateLoading(request.getSession().getServletContext(), (String) map.get("templatePath"));
            cfg.setObjectWrapper(new DefaultObjectWrapper());
            // 获取模板文件
            Template template = cfg.getTemplate(templateName, "UTF-8");
//			// 生成输出到控制台
//			Writer out = new OutputStreamWriter(System.out);
//			template.process(map, out);
//			out.flush();

            // 生成输出到文件
            String root = genPackStr(path, filePath);
            File fileDir = new File(root);
            // 创建文件夹，不存在则创建
            FileUtils.forceMkdir(fileDir);
            // 指定生成输出的文件
            File output = new File(fileDir + "/" + map.get("fileName"));
            Writer writer = new PrintWriter(output, "UTF-8");
//            Writer writer = new FileWriter(output);
            template.process(map, writer);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 根据包名获取对应的路径名
     *
     * @param root  根路径
     * @param pack  包名
     * @return
     */
    private static String genPackStr(String root, String pack) {
        String result = root;
        String[] dirs = pack.split("\\.");
        for (String dir : dirs) {
            result += "/" + dir;
        }
        return result;
    }
}
