package cn.feast.coding.generator;

import cn.feast.coding.generator.utils.CodeGenerator;
import cn.feast.coding.model.generator.*;
import cn.feast.coding.service.generator.PageTemplateService;
import cn.feast.coding.tools.Contants;
import cn.feast.coding.tools.JSPPageTemplate;
import cn.feast.coding.tools.PageTemplateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class JSPGenerator extends BaseGenerator implements Generator {
    @Autowired
    private PageTemplateService pageTemplateService;

    public void generateModel(Map<String, Object> map) {
        map.put("templatePath", Contants.SERVLET_TEMPLATE_PATH);
        map.put("templateName", JSPPageTemplate.MODEL_TEMPLATE);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "model"));
        map.put("fileName", ((Table) map.get("table")).getClassName() + ".java");
        map.put("filePath", getPackName(((Project) map.get("project")).getPackName(), "model"));
        CodeGenerator.generator(map);
    }

    public void generateDao(Map<String, Object> map) {
        map.put("templatePath", Contants.SERVLET_TEMPLATE_PATH);
        map.put("templateName", JSPPageTemplate.DAO_TEMPLATE);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "dao"));
        map.put("fileName", ((Table) map.get("table")).getClassName() + "Dao.java");
        map.put("updateClause", updateClause(map));
        map.put("insertClause", insertClause(map));
        map.put("filePath", getPackName(((Project) map.get("project")).getPackName(), "dao"));
        CodeGenerator.generator(map);
    }

    public void generateDaoImpl(Map<String, Object> map) {

    }

    public void generateController(Map<String, Object> map) {
        String pack = getPackName(((Project) map.get("project")).getPackName(), "servlet");
        map.put("templatePath", Contants.SERVLET_TEMPLATE_PATH);
        map.put("pack", pack);
        map.put("filePath", pack);
        String className = ((Table) map.get("table")).getClassName();
        map.put("templateName", JSPPageTemplate.QUERY_SERVLET_TEMPLATE);
        map.put("fileName", "Query" + className + "Servlet.java");
        CodeGenerator.generator(map);
        map.put("templateName", JSPPageTemplate.ADD_SERVLET_TEMPLATE);
        map.put("fileName", "Add" + className + "Servlet.java");
        CodeGenerator.generator(map);
        map.put("templateName", JSPPageTemplate.EDIT_SERVLET_TEMPLATE);
        map.put("fileName", "Edit" + className + "Servlet.java");
        CodeGenerator.generator(map);
        map.put("templateName", JSPPageTemplate.UPDATE_SERVLET_TEMPLATE);
        map.put("fileName", "Update" + className + "Servlet.java");
        CodeGenerator.generator(map);
        map.put("templateName", JSPPageTemplate.DELETE_SERVLET_TEMPLATE);
        map.put("fileName", "Delete" + className + "Servlet.java");
        CodeGenerator.generator(map);

    }

    @Override
    public void generateIService(Map<String, Object> map) {

    }

    public void generateService(Map<String, Object> map) {

    }

    public void generateJsp(Map<String, Object> map) {
        List<Table> tableList = (List<Table>) map.get("tableList");
        for (Table t : tableList) {
            t.setUrl("<%=path %>/query" + t.getClassName() + "Servlet");
        }
        map.put("tableList", tableList);
        PageTemplate pageTemplate = pageTemplateService.queryById(((Project) map.get("project")).getPageStyle());
        map.put("templatePath", pageTemplate.getPagePath());
        String className = ((Table) map.get("table")).getClassName();
        String list_url = "<%=path %>/query" + className + "Servlet";
        String add_url = "<%=path %>/add" + className + "Servlet";
        String edit_url = "<%=path %>/edit" + className + "Servlet";
        String update_url = "<%=path %>/update" + className + "Servlet";
        String delete_url = "<%=path %>/delete" + className + "Servlet";
        map.put("list_url", list_url);
        map.put("add_url", add_url);
        map.put("edit_url", edit_url);
        map.put("update_url", update_url);
        map.put("delete_url", delete_url);
        String pack = getPackName(((Project) map.get("project")).getPackName(), "jsp");
        map.put("pack", pack);
        map.put("filePath", pack);
        //list页面
        map.put("templateName", PageTemplateUtils.LIST_PAGE);
        map.put("fileName", uncapFirst(((Table) map.get("table")).getClassName()) + "_list.jsp");
        CodeGenerator.generator(map);
        //add页面
        map.put("templateName", PageTemplateUtils.ADD_PAGE);
        map.put("fileName", uncapFirst(((Table) map.get("table")).getClassName()) + "_add.jsp");
        CodeGenerator.generator(map);
        //edit页面
        map.put("templateName", PageTemplateUtils.EDIT_PAGE);
        map.put("fileName", uncapFirst(((Table) map.get("table")).getClassName()) + "_edit.jsp");
        CodeGenerator.generator(map);
    }

    public void generateIndex(Map<String, Object> map) {
        List<Table> tableList = (List<Table>) map.get("tableList");
        for (Table t : tableList) {
            t.setUrl("<%=path %>/query" + t.getClassName() + "Servlet");
        }
        map.put("tableList", tableList);
        PageTemplate pageTemplate = pageTemplateService.queryById(((Project) map.get("project")).getPageStyle());
        map.put("templatePath", pageTemplate.getPagePath());
        map.put("templateName", PageTemplateUtils.INDEX_PAGE);
        String pack = getPackName(((Project) map.get("project")).getPackName(), "jsp");
        map.put("pack", pack);
        map.put("fileName", "index.jsp");
        map.put("filePath", pack);
        CodeGenerator.generator(map);

        //添加登录模块支持(登录页面)
        ModuleProject loginModule = (ModuleProject) map.get("loginModule");
        if (null != loginModule) {
            //登陆页面
            map.put("login_url", "<%=path %>/loginServlet");
            map.put("templateName", PageTemplateUtils.LOGIN_PAGE);
            map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "jsp"));
            map.put("fileName", "login.jsp");
            map.put("filePath", pack);
            CodeGenerator.generator(map);
            //登陆servlet
            map.put("templatePath", Contants.SERVLET_TEMPLATE_PATH);
            map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "servlet"));
            map.put("templateName", JSPPageTemplate.LOGIN_SERVLET_TEMPLATE);
            map.put("fileName", "LoginServlet.java");
            map.put("filePath", getPackName(((Project) map.get("project")).getPackName(), "servlet"));
            CodeGenerator.generator(map);
        }
    }

    public void generateUtils(Map<String, Object> map) {
        String pack = getPackName(((Project) map.get("project")).getPackName(), "utils");
        map.put("pack", pack);
        map.put("filePath", pack);
        map.put("templatePath", Contants.SERVLET_TEMPLATE_PATH);
        map.put("templateName", JSPPageTemplate.DBUTILS_TEMPLATE);
        map.put("fileName", "DbUtils.java");
        CodeGenerator.generator(map);
        map.put("templateName", JSPPageTemplate.PAGE_UTILS_TEMPLATE);
        map.put("fileName", "PageUtils.java");
        CodeGenerator.generator(map);
    }

    public void generateCommon(Map<String, Object> map) {
        map.put("templatePath", Contants.SERVLET_TEMPLATE_PATH);
        map.put("templateName", JSPPageTemplate.XML_TEMPLATE);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "xml"));
        map.put("fileName", "web.xml");
        map.put("filePath", getPackName(((Project) map.get("project")).getPackName(), "xml"));
        CodeGenerator.generator(map);
        map.put("templateName", JSPPageTemplate.PAGE_TEMPLATE);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "model"));
        map.put("fileName", "Page.java");
        map.put("filePath", getPackName(((Project) map.get("project")).getPackName(), "model"));
        CodeGenerator.generator(map);
        //添加查询模块支持
        List<ModuleProject> queryList = (List<ModuleProject>) map.get("queryModule");
        if (null != queryList && queryList.size() > 0) {
            for (ModuleProject queryModule : queryList) {
                map.put("templateName", JSPPageTemplate.QUERY_SERVLET_SELECTED_TEMPLATE);
                map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "servlet"));
                map.put("fileName", "query" + queryModule.getTable().getClassName() + "SelectedServlet");
                map.put("filePath", getPackName(((Project) map.get("project")).getPackName(), "servlet"));
                CodeGenerator.generator(map);
            }
        }

    }

    public void generateDefined(Map<String, Object> map) {
    }

    /**
     * dao层 insert语句拼接
     *
     * @param map
     * @return
     */
    public String insertClause(Map<String, Object> map) {
        StringBuilder clause = new StringBuilder();
        clause.append("(");
        List<Colunm> colunmList = (List<Colunm>) map.get("colunmList");
        for (int i = 0; i < colunmList.size(); i++) {
            clause.append(colunmList.get(i).getFiledName());
            if (i != colunmList.size() - 1) {
                clause.append(",");
            }
        }
        clause.append(")").append(Contants.BLACK_SPACE).append("values").append("(");
        for (int i = 0; i < colunmList.size(); i++) {
            Colunm colunm = colunmList.get(i);
            if (colunm.getColunmType().equals(Contants.STRING_TYPE)) {
                clause.append("'").append("\"").append("+");
                clause.append("model.get" + capFirst(colunm.getColunmName()) + "()");
                clause.append("+").append("\"").append("'");
            } else {
                clause.append("\"").append("+");
                clause.append("model.get" + capFirst(colunm.getColunmName()) + "()");
                clause.append("+").append("\"");
            }
            if (i != colunmList.size() - 1) {
                clause.append(",");
            }
        }
        clause.append(")");
        return clause.toString();
    }

    /**
     * dao层 update语句拼接
     *
     * @param map
     * @return
     */
    public String updateClause(Map<String, Object> map) {
        StringBuilder clause = new StringBuilder();
        Colunm primary = (Colunm) map.get("primary");
        List<Colunm> colunmList = (List<Colunm>) map.get("colunmList");
        for (int i = 0; i < colunmList.size(); i++) {
            Colunm colunm = colunmList.get(i);
            if (!colunm.getColunmName().equals(primary.getColunmName())) {
                clause.append(colunm.getFiledName()).append("=");
                if (colunm.getColunmType().equals(Contants.STRING_TYPE)) {
                    clause.append("'").append("\"").append("+");
                    clause.append("model.get" + capFirst(colunm.getColunmName()) + "()");
                    clause.append("+").append("\"").append("'");
                } else {
                    clause.append("\"").append("+");
                    clause.append("model.get" + capFirst(colunm.getColunmName()) + "()");
                    clause.append("+").append("\"");
                }
                if (i != colunmList.size() - 1) {
                    clause.append(",");
                }
            }
        }
        clause.append(Contants.BLACK_SPACE).append("where").append(Contants.BLACK_SPACE);
        clause.append(primary.getFiledName()).append("=");
        if (primary.getColunmType().equals(Contants.STRING_TYPE)) {
            clause.append("'").append("\"").append("+");
            clause.append("model.get" + capFirst(primary.getColunmName()) + "()");
            clause.append("+").append("\"").append("'");
        } else {
            clause.append("\"").append("+");
            clause.append("model.get" + capFirst(primary.getColunmName()) + "()");
            clause.append("+").append("\"");
        }
        return clause.toString();
    }
}
