package com.eversec.kit.creator;

import com.eversec.common.FileKit;
import com.eversec.common.Kv;
import com.eversec.kit.creator.impl.*;
import com.eversec.kit.creator.impl.page.*;
import com.eversec.service.CfgBean;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: liangxianyou at 2018/8/30 11:31.
 */
public class Runner {
    private static List<ICreator> creators(CfgBean cfgBean){
        List<ICreator> creators = new ArrayList<>();

        List<String> ca = cfgBean.getCa();
        ca.forEach(x -> {
            if ("service".equalsIgnoreCase(x)) {
                creators.add(new JavaBeanKit());// JavaBean
                creators.add(new XmlSqlKit());// 生成mysql 版本的xml配置文件， 如果要生成Es使用对应的实例对象即可
                creators.add(new ControllerKit());// Controller
                creators.add(new ServiceKit());//service
                creators.add(new ServiceImplKit());//ServiceImpl
                creators.add(new DaoMapper());// Mapper.java文件 针对使用Mybites创建对应的xxxMapper.java
            }
            if ("ddl".equalsIgnoreCase(x)) {
                creators.add(new SqlKit());// Mysql 建表语句，【提示Es或者其他建表语句可自行创建对应的实现】
            }
            if ("front".equalsIgnoreCase(x)) {
                creators.add(new CtrlKit());
                creators.add(new ListKit());
                creators.add(new EditKit());
                creators.add(new EditCtrlKit());
                creators.add(new ConditionKit());
                creators.add(new DetailKit());
                creators.add(new DetailCtrlKit());
            }
        });
        return creators;
    }

    public static String doxx(List<ICreator> creators) throws IOException {
        StringBuffer infoBuf = new StringBuffer();

        StringBuilder sqlBuf = new StringBuilder();
        List<Kv> ctrList = new ArrayList<>();

        infoBuf.append("------------ start ... ------------\n");
        infoBuf.append("create service code path:" + ICreator.dirPath + "\n");
        infoBuf.append("create page code path:" + ICreator.pageDirPath + "\n");
        ICreator.sheetNames().forEach(k->{
            try {
                List<Map> list = ICreator.readExcelSheet(k);//map{field_A:v_A,field_B:v_B}
                dealData(list);//读取到的excel数据自定义处理

                list.remove(1);//list[0] head info
                Map rowHead = list.remove(0);
                String remark = ICreator.getRemark(rowHead);//list[1] comment,
                String verify = ICreator.getVerify(rowHead);
                String[] condition = ICreator.getCondition(rowHead);
                String clazz = ICreator.getClazzName(rowHead);
                Kv para = Kv.of("remark", remark).set("verify", verify).set("condition", condition).set("table", k);

                creators.forEach(x->{
                    try {
                        if (x instanceof CtrlExtKit){
                            Kv kv = ((CtrlExtKit) x).createStrExt(list);

                            //kv.set("bean", ICreator.toLowerCaseFirst(clazz));
                            kv.set("beanFL", ICreator.toLowerCaseFirst(clazz));
                            kv.set("beanL", clazz.toLowerCase());
                            kv.set("remark", remark);

                            ctrList.add(kv);
                        }
                        else if (x instanceof SqlKit){
                            sqlBuf.append(x.createStr(clazz, list, para));
                        }
                        else {
                            x.createFile(x.createStr(clazz, list, para), clazz);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });

                infoBuf.append("\t\t==> "+ k + "\n");
            }catch (Exception e){
                e.printStackTrace();
            }
        });

        if (sqlBuf.length() > 0){
            File file = SqlKit.getFile();
            FileKit.strToFile(sqlBuf.toString(), file, true);
            infoBuf.append("sql.ddl:"+ ICreator.dirPath +"resources/ddl/\n\t\t==>"+ file.getName() + "\n");
        }

        if (ctrList.size() > 0){
            String strCtr = ICreator.engine.getTemplate(ICreator.tplPath + "front/ext/CtrlExtTpl.js").renderToString(Kv.of("data", ctrList));
            FileKit.strToFile(strCtr, new File("tmp/listCtrl.js"));

            String strList = ICreator.engine.getTemplate(ICreator.tplPath + "front/ext/listExtTpl.html").renderToString(Kv.of("data", ctrList));
            FileKit.strToFile(strList, new File("tmp/list.html"));
        }
        infoBuf.append("------------ complete! -----------\n");

        System.out.println(infoBuf.toString());
        return infoBuf.toString();
    }

    //自定义数据处理
    private static void dealData(List<Map> list) {
        list.stream().filter(x-> "tru".equalsIgnoreCase(x.get("field")+"")).forEach(x->{
            x.put("filter", "like");
        });
    }

    public static String run(CfgBean cfgBean) throws IOException {
        ICreator.setCfgBean(cfgBean);

        List<ICreator> creators = creators(cfgBean);
        return doxx(creators);
    }
}
