package com.yxcheng.token.gen;

import com.yxcheng.token.utils.MapUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.Data;
import lombok.Getter;
import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class GenPlus {

    private static final String TEMPLATE_PATH = "tax-common/src/main/resources/ftl/plus";

    @Getter private final Configuration configuration;

    public GenPlus() {
        this.configuration =  new Configuration();
        try {
            configuration.setDirectoryForTemplateLoading(new File(TEMPLATE_PATH));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws IllegalAccessException {
        GenEntity entity = new GenEntity();
        entity.setModule("file");
        entity.setEntityName("FileToken");
        System.out.println(entity);

        new GenPlus().generate(entity);
    }

    private void generate(GenEntity entity) throws IllegalAccessException {
        Map<String, Object> data = MapUtil.objectToMap(entity);

        Map<String, String> ftlPathMap = this.getFtlPathMap(entity);

        ftlPathMap.keySet().stream().forEach(flt->{
            Template template = null;
            Writer out = null;
            try {
                template = this.getConfiguration().getTemplate(flt);
                // step5 生成数据
                String javaName = entity.getEntityName() + flt.substring(0,flt.indexOf(".")) + ".java";
                File docFile = new File(ftlPathMap.get(flt) + File.separator + javaName);
                out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
                // step6 输出文件
                template.process(data, out);
                System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^"+javaName+" 文件创建成功 !\npath:" + docFile.getAbsolutePath());
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {

                }
            }

        });
    }


    public Map<String, String> getFtlPathMap(GenEntity entity){
        if(entity==null || StringUtils.isBlank(entity.getModule())){
            throw new IllegalArgumentException("please input module");
        }

        String path = "";
        Map<String, String> data = new HashMap<String, String>();
        String mapperPath = String.format(path +"tax-%s/%s-dao/src/main/java/com/tax/%s/mapper",entity.getModule(),entity.getModule(),entity.getModule());
        String serivcePath = String.format(path +"tax-%s/%s-service/src/main/java/com/tax/%s/service/api",entity.getModule(),entity.getModule(),entity.getModule());
        String serviceImplPath = String.format(path +"tax-%s/%s-service/src/main/java/com/tax/%s/service/impl",entity.getModule(),entity.getModule(),entity.getModule());
        data.put("Mapper.ftl",mapperPath);
        data.put("Service.ftl",serivcePath);
        data.put("ServiceImpl.ftl",serviceImplPath);
        return data;
    }



    @Data
    private static class GenEntity{
        private String module;
        private String entityName;
    }

}
