package cn.feast.coding.generator;

import cn.feast.coding.generator.utils.CodeGenerator;
import cn.feast.coding.model.generator.ModuleProject;
import cn.feast.coding.model.generator.PageTemplate;
import cn.feast.coding.model.generator.Project;
import cn.feast.coding.model.generator.Table;
import cn.feast.coding.service.generator.PageTemplateService;
import cn.feast.coding.tools.Contants;
import cn.feast.coding.tools.PageTemplateUtils;
import cn.feast.coding.tools.SSHPageTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class SSHGenerator extends BaseGenerator implements Generator {
    @Autowired
    private PageTemplateService pageTemplateService;
    
    public void generateModel(Map<String, Object> map) {
        map.put("templatePath", Contants.SSH_TEMPLATE_PATH);
        map.put("templateName", SSHPageTemplate.MODEL_TEMPLATE);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "bean"));
        map.put("fileName", ((Table) map.get("table")).getClassName() + ".java");
        map.put("filePath", getPackName(((Project) map.get("project")).getPackName(), "bean"));
        CodeGenerator.generator(map);
    }

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

    public void generateDaoImpl(Map<String, Object> map) {
        map.put("templatePath", Contants.SSH_TEMPLATE_PATH);
        map.put("templateName", SSHPageTemplate.MAPPER_XML_TEMPLATE);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "daoImpl"));
        map.put("fileName", ((Table) map.get("table")).getClassName() + "DaoImpl.java");
        map.put("filePath", getPackName(((Project) map.get("project")).getPackName(), "daoImpl"));
        CodeGenerator.generator(map);
    }

    public void generateController(Map<String, Object> map) {
        map.put("templatePath", Contants.SSH_TEMPLATE_PATH);
        map.put("templateName", SSHPageTemplate.CONTROLLER_TEMPLATE);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "action"));
        map.put("fileName", ((Table) map.get("table")).getClassName() + "Action.java");
        map.put("filePath", getPackName(((Project) map.get("project")).getPackName(), "action"));
        CodeGenerator.generator(map);
    }

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

    }

    public void generateService(Map<String, Object> map) {
        map.put("templatePath", Contants.SSH_TEMPLATE_PATH);
        map.put("templateName", SSHPageTemplate.SERVICE_TEMPLATE);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "service"));
        map.put("fileName", ((Table) map.get("table")).getClassName() + "Service.java");
        map.put("filePath", getPackName(((Project) map.get("project")).getPackName(), "service"));
        CodeGenerator.generator(map);
    }

    public void generateJsp(Map<String, Object> map) {
        List<Table> tableList = (List<Table>) map.get("tableList");
        for (Table t : tableList) {
            t.setUrl("<%=path %>/" + uncapFirst(t.getClassName()) + "/" + uncapFirst(t.getClassName()) + "-list.action");
        }
        map.put("tableList", tableList);
        PageTemplate pageTemplate = pageTemplateService.queryById(((Project) map.get("project")).getPageStyle());
        map.put("templatePath", pageTemplate.getPagePath());
        String className = uncapFirst(((Table) map.get("table")).getClassName());
        String list_url = "<%=path %>/" + className + "/" + className + "-list.action";
        String add_url = "<%=path %>/" + className + "/" + className + "-add.action";
        String edit_url = "<%=path %>/" + className + "/" + className + "-edit.action";
        String update_url = "<%=path %>/" + className + "/" + className + "-update.action";
        String delete_url = "<%=path %>/" + className + "/" + className + "-delete.action";
        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);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "jsp"));
        map.put("filePath", getPackName(((Project) map.get("project")).getPackName(), "jsp"));
        //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 %>/" + uncapFirst(t.getClassName()) + "/" + uncapFirst(t.getClassName()) + "-list.action");
        }
        map.put("tableList", tableList);
        PageTemplate pageTemplate = pageTemplateService.queryById(((Project) map.get("project")).getPageStyle());
        map.put("templatePath", pageTemplate.getPagePath());
        map.put("templateName", PageTemplateUtils.INDEX_PAGE);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "jsp"));
        map.put("filePath", getPackName(((Project) map.get("project")).getPackName(), "jsp"));
        map.put("fileName", "index.jsp");
        CodeGenerator.generator(map);

        //添加登录模块支持(登录页面)
        ModuleProject loginModule = (ModuleProject) map.get("loginModule");
        if (null != loginModule) {
            map.put("login_url", "<%=path %>/" + uncapFirst(loginModule.getTable().getClassName()) + "/" + uncapFirst(loginModule.getTable().getClassName()) + "-login.action");
            map.put("templateName", PageTemplateUtils.LOGIN_PAGE);
            map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "jsp"));
            map.put("fileName", "login.jsp");
            CodeGenerator.generator(map);
        }
    }

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

    public void generateCommon(Map<String, Object> map) {
        map.put("templatePath", Contants.SSH_TEMPLATE_PATH);
        map.put("templateName", SSHPageTemplate.STRUTS_TEMPLATE);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "struts"));
        map.put("fileName", "struts.xml");
        map.put("filePath", getPackName(((Project) map.get("project")).getPackName(), "struts"));
        CodeGenerator.generator(map);
        map.put("templateName", SSHPageTemplate.PAGE_TEMPLATE);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "bean"));
        map.put("fileName", "Page.java");
        map.put("filePath", getPackName(((Project) map.get("project")).getPackName(), "bean"));
        CodeGenerator.generator(map);
    }

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