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.SSMMavenPageTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

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

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

    public void generateDao(Map<String, Object> map) {
        map.put("templatePath", Contants.SSM_MAVEN_TEMPLATE_PATH);
        map.put("templateName", SSMMavenPageTemplate.MAPPER_TEMPLATE);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "mapper"));
        map.put("fileName", ((Table) map.get("table")).getClassName() + "Mapper.java");
        map.put("filePath", "src.main.java."+getPackName(((Project) map.get("project")).getPackName(), "mapper"));
        CodeGenerator.generator(map);
    }

    public void generateDaoImpl(Map<String, Object> map) {
        map.put("templatePath", Contants.SSM_MAVEN_TEMPLATE_PATH);
        map.put("templateName", SSMMavenPageTemplate.MAPPER_XML_TEMPLATE);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "mapping"));
        map.put("fileName", ((Table) map.get("table")).getClassName() + "Mapper.xml");
        map.put("filePath", "src.main.resources.mapping");
        CodeGenerator.generator(map);

    }

    public void generateController(Map<String, Object> map) {
        map.put("templatePath", Contants.SSM_MAVEN_TEMPLATE_PATH);
        map.put("templateName", SSMMavenPageTemplate.CONTROLLER_TEMPLATE);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "controller"));
        map.put("fileName", ((Table) map.get("table")).getClassName() + "Controller.java");
        map.put("filePath", "src.main.java."+getPackName(((Project) map.get("project")).getPackName(), "controller"));
        CodeGenerator.generator(map);
    }

    @Override
    public void generateIService(Map<String, Object> map) {
        map.put("templatePath", Contants.SSM_MAVEN_TEMPLATE_PATH);
        map.put("templateName", SSMMavenPageTemplate.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", "src.main.java."+getPackName(((Project) map.get("project")).getPackName(), "service"));
        CodeGenerator.generator(map);
    }

    public void generateService(Map<String, Object> map) {
        map.put("templatePath", Contants.SSM_MAVEN_TEMPLATE_PATH);
        map.put("templateName", SSMMavenPageTemplate.SERVICE_IMPL_TEMPLATE);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "service.impl"));
        map.put("fileName", ((Table) map.get("table")).getClassName() + "ServiceImpl.java");
        map.put("filePath", "src.main.java."+getPackName(((Project) map.get("project")).getPackName(), "service.impl"));
        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()) + "/list");
        }
        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 + "/list";
        String add_url = "<%=path %>/" + className + "/add";
        String update_url = "<%=path %>/" + className + "/update";
        String delete_url = "<%=path %>/" + className + "/delete";
        String detail_url = "<%=path %>/" + className + "/detail";
        map.put("list_url", list_url);
        map.put("add_url", add_url);
        map.put("edit_url", update_url);
        map.put("update_url", update_url);
        map.put("delete_url", delete_url);
        map.put("detail_url", detail_url);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "jsp"));
        map.put("filePath", "src.main.webapp.views");
        //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);
        //detail页面
        map.put("templateName", PageTemplateUtils.DETAIL_PAGE);
        map.put("fileName", uncapFirst(((Table) map.get("table")).getClassName()) + "_detail.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()) + "/list");
        }
        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("fileName", "index.jsp");
        map.put("filePath", "src.main.webapp.views");
        CodeGenerator.generator(map);

        //欢迎页面
        if(pageTemplate.getPagePath().equals("/template/page_maven") || pageTemplate.getPagePath().equals("/template/page_maven1")){
            map.put("templateName", PageTemplateUtils.WELCOME_PAGE);
            map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "jsp"));
            map.put("fileName", "welcome.jsp");
            CodeGenerator.generator(map);
        }

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

        //添加注册模块支持(注册页面)
        ModuleProject registerModule = (ModuleProject) map.get("registerModule");
        if (null != registerModule) {
            map.put("register_url", "<%=path %>/register");
            map.put("templateName", PageTemplateUtils.REGISTER_PAGE);
            map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "jsp"));
            map.put("fileName", "register.jsp");
            CodeGenerator.generator(map);
        }

        //添加修改密码模块支持(修改密码页面)
        ModuleProject passwordModule = (ModuleProject) map.get("passwordModule");
        if (null != passwordModule) {
            map.put("password_url", "<%=path %>/password");
            map.put("templateName", PageTemplateUtils.PASSWORD_PAGE);
            map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "jsp"));
            map.put("fileName", "password.jsp");
            CodeGenerator.generator(map);
        }
    }

    public void generateUtils(Map<String, Object> map) {
        map.put("templatePath", Contants.SSM_MAVEN_TEMPLATE_PATH);
        map.put("templateName", SSMMavenPageTemplate.PAGEUTILS_TEMPLATE);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "utils"));
        map.put("fileName", "PageUtils.java");
        map.put("filePath", "src.main.java."+getPackName(((Project) map.get("project")).getPackName(), "utils"));
        CodeGenerator.generator(map);
    }

    public void generateCommon(Map<String, Object> map) {
        //mapper接口
        map.put("templatePath", Contants.SSM_MAVEN_TEMPLATE_PATH);
        map.put("templateName", SSMMavenPageTemplate.IMAPPER_TEMPLATE);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "mapper"));
        map.put("fileName", "IMapper.java");
        map.put("filePath", "src.main.java."+getPackName(((Project) map.get("project")).getPackName(), "mapper"));
        CodeGenerator.generator(map);
        //service接口
        map.put("templateName", SSMMavenPageTemplate.ISERVICE_TEMPLATE);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "service"));
        map.put("fileName", "IService.java");
        map.put("filePath", "src.main.java."+getPackName(((Project) map.get("project")).getPackName(), "service"));
        CodeGenerator.generator(map);
        //baseController
        map.put("templateName", SSMMavenPageTemplate.BASE_CONTROLLER_TEMPLATE);
        map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "controller"));
        map.put("fileName", "BaseController.java");
        map.put("filePath", "src.main.java."+getPackName(((Project) map.get("project")).getPackName(), "controller"));
        CodeGenerator.generator(map);
        //pom.xml
        map.put("templateName", SSMMavenPageTemplate.POM_XML_TEMPLATE);
        map.put("pack", ((Project) map.get("project")).getPackName());
        map.put("fileName", "pom.xml");
        map.put("filePath", "");
        CodeGenerator.generator(map);
        //web.xml
        map.put("templateName", SSMMavenPageTemplate.WEB_XML_TEMPLATE);
        map.put("pack", ((Project) map.get("project")).getPackName());
        map.put("fileName", "web.xml");
        map.put("filePath", "src.main.webapp.WEB-INF");
        CodeGenerator.generator(map);
        //spring-core.xml
        map.put("templateName", SSMMavenPageTemplate.SPRING_CORE_XML_TEMPLATE);
        map.put("pack", ((Project) map.get("project")).getPackName());
        map.put("fileName", "spring-core.xml");
        map.put("filePath", "src.main.resources");
        CodeGenerator.generator(map);
        //spring-mvc.xml
        map.put("templateName", SSMMavenPageTemplate.SPRING_WEB_XML_TEMPLATE);
        map.put("pack", ((Project) map.get("project")).getPackName());
        map.put("fileName", "spring-mvc.xml");
        map.put("filePath", "src.main.resources");
        CodeGenerator.generator(map);
        //mybatis-config.xml
        map.put("templateName", SSMMavenPageTemplate.MYBATIS_CONFIG_XML_TEMPLATE);
        map.put("pack", ((Project) map.get("project")).getPackName());
        map.put("fileName", "mybatis-config.xml");
        map.put("filePath", "src.main.resources");
        CodeGenerator.generator(map);
        //jdbc.properties
        map.put("templateName", SSMMavenPageTemplate.JDBC_PROPERTIES_TEMPLATE);
        map.put("pack", ((Project) map.get("project")).getPackName());
        map.put("fileName", "jdbc.properties");
        map.put("filePath", "src.main.resources");
        CodeGenerator.generator(map);
        //logback-spring.xml
        map.put("templateName", SSMMavenPageTemplate.LOGBACK_XML_TEMPLATE);
        map.put("pack", ((Project) map.get("project")).getPackName());
        map.put("fileName", "logback-spring.xml");
        map.put("filePath", "src.main.resources");
        CodeGenerator.generator(map);
        //添加登录模块支持(登录拦截器)
        ModuleProject loginModule = (ModuleProject) map.get("loginModule");
        if (null != loginModule) {
            map.put("templateName", SSMMavenPageTemplate.LOGIN_INTERCEPTOR_TEMPLATE);
            map.put("pack", getPackName(((Project) map.get("project")).getPackName(), "interceptor"));
            map.put("fileName", "LoginInterceptor.java");
            map.put("filePath", "src.main.java."+getPackName(((Project) map.get("project")).getPackName(), "interceptor"));
            CodeGenerator.generator(map);
        }
    }

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