package xyz.wbsite.dbtool.javafx.manger.callable;

import xyz.wbsite.dbtool.javafx.manger.FreeMarkerManager;
import xyz.wbsite.dbtool.javafx.manger.ManagerFactory;
import xyz.wbsite.dbtool.javafx.po.Api;
import xyz.wbsite.dbtool.javafx.tool.EntityReader;
import xyz.wbsite.dbtool.javafx.tool.RequestReader;
import xyz.wbsite.dbtool.javafx.tool.ResponseReader;
import xyz.wbsite.dbtool.javafx.tool.Tool;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ApiCallable implements Callable {

    private File module;
    private File apiFile;
    private List<Api> apiList;
    private List<String> domainList;
    private Tool tool = new Tool();
    File apiDomain;
    File apiModule;
    File apiFrame;
    File testApiDomain;
    private FreeMarkerManager freeMarkerManager;

    public ApiCallable(File module, File apiFile, List<String> domainList, List<Api> apiList) {
        this.module = module;
        this.apiFile = apiFile;
        this.apiList = apiList;
        this.domainList = domainList;
        this.freeMarkerManager = ManagerFactory.getFreeMarkerManager();
    }

    public Boolean call() throws Exception {
        if (!apiFile.exists()) {
            apiFile.mkdir();
        } else {
            Tool.clear(apiFile);
        }

        StringBuffer sbmain = new StringBuffer("");
        sbmain.append(this.apiFile.getPath() + File.separator);
        sbmain.append("src" + File.separator);
        sbmain.append("main" + File.separator);
        sbmain.append("java" + File.separator);

        StringBuffer sbtest = new StringBuffer("");
        sbtest.append(this.apiFile.getPath() + File.separator);
        sbtest.append("src" + File.separator);
        sbtest.append("test" + File.separator);
        sbtest.append("java" + File.separator);

        for (String s1 : domainList) {
            sbmain.append(s1 + File.separator);
            sbtest.append(s1 + File.separator);
        }

        apiDomain = Tool.createPath(sbmain.toString());
        apiModule = Tool.createPath(sbmain.toString(), "module");
        apiFrame = Tool.createPath(sbmain.toString(), "frame");
        testApiDomain = Tool.createPath(sbtest.toString());

        for (Api api : apiList) {
            if (api.isCheck()) {
                RequestReader requestReader = new RequestReader(api.getRequestFile());
                generateRequest(requestReader);

                ResponseReader responseReader = new ResponseReader(api.getResponseFile());
                generateResponse(responseReader);
            }
        }

        {
            System.out.println("生成模块:Pom");
            HashMap<String, Object> ctx = new HashMap<String, Object>();
            ctx.put("domain", String.join(".", domainList));
            ctx.put("projectName", apiFile.getName());
            File file = new File(apiFile, "pom.xml");
            freeMarkerManager.outputTemp(file, "Java_api/pom.xml", ctx);
        }

        {
            HashMap<String, Object> ctx = new HashMap<String, Object>();
            ctx.put("domain", String.join(".", domainList));
            File file = new File(apiDomain, "ApiClient.java");
            freeMarkerManager.outputTemp(file, "/Java_api/ApiClient.java", ctx);
        }

        {
            System.out.println("生成基础类");
            HashMap<String, Object> ctx = new HashMap<String, Object>();
            ctx.put("domain", String.join(".", domainList));
            {
                File file = new File(apiFrame, "ApiEntity.java");
                freeMarkerManager.outputTemp(file, "/Java_api/frame/ApiEntity.java", ctx);
            }
            {
                File file = new File(apiFrame, "AESUtil.java");
                freeMarkerManager.outputTemp(file, "/Java_api/frame/AESUtil.java", ctx);
            }
            {
                File file = new File(apiFrame, "ApiRequest.java");
                freeMarkerManager.outputTemp(file, "/Java_api/frame/ApiRequest.java", ctx);
            }
            {
                File file = new File(apiFrame, "ApiFindRequest.java");
                freeMarkerManager.outputTemp(file, "/Java_api/frame/ApiFindRequest.java", ctx);
            }
            {
                File file = new File(apiFrame, "ApiSearchRequest.java");
                freeMarkerManager.outputTemp(file, "/Java_api/frame/ApiSearchRequest.java", ctx);
            }
            {
                File file = new File(apiFrame, "ApiResponse.java");
                freeMarkerManager.outputTemp(file, "/Java_api/frame/ApiResponse.java", ctx);
            }
            {
                File file = new File(apiFrame, "ApiFindResponse.java");
                freeMarkerManager.outputTemp(file, "/Java_api/frame/ApiFindResponse.java", ctx);
            }
            {
                File file = new File(apiFrame, "Base64Util.java");
                freeMarkerManager.outputTemp(file, "/Java_api/frame/Base64Util.java", ctx);
            }
            {
                File file = new File(apiFrame, "DownloadUtil.java");
                freeMarkerManager.outputTemp(file, "/Java_api/frame/DownloadUtil.java", ctx);
            }
            {
                File file = new File(apiFrame, "Error.java");
                freeMarkerManager.outputTemp(file, "/Java_api/frame/Error.java", ctx);
            }
            {
                File file = new File(apiFrame, "ErrorType.java");
                freeMarkerManager.outputTemp(file, "/Java_api/frame/ErrorType.java", ctx);
            }
            {
                File file = new File(apiFrame, "MapperUtil.java");
                freeMarkerManager.outputTemp(file, "/Java_api/frame/MapperUtil.java", ctx);
            }
            {
                File file = new File(apiFrame, "MD5Util.java");
                freeMarkerManager.outputTemp(file, "/Java_api/frame/MD5Util.java", ctx);
            }
            {
                File file = new File(apiFrame, "RSAUtil.java");
                freeMarkerManager.outputTemp(file, "/Java_api/frame/RSAUtil.java", ctx);
            }
            {
                File file = new File(apiFrame, "ProgressRequestBody.java");
                freeMarkerManager.outputTemp(file, "/Java_api/frame/ProgressRequestBody.java", ctx);
            }

            {
                File file = new File(apiFrame, "SortType.java");
                freeMarkerManager.outputTemp(file, "/Java_api/frame/SortType.java", ctx);
            }

            {
                File file = new File(apiFrame, "StringUtils.java");
                freeMarkerManager.outputTemp(file, "/Java_api/frame/StringUtils.java", ctx);
            }

            //4.0.1 去掉验证 减少体积
//        {
//            File file = new File(frameWork , "ValidationUtil.java");
//            freeMarkerManager.outputTemp(file, "/Java_api/frame/ValidationUtil.java", ctx);
//        }

        }

        generateTest();

        System.out.println("finish");
        return true;
    }


    private void generateTest() {
        Set<String> modules = new HashSet<>();
        for (Api api : apiList) {
            modules.add(api.getModule());
        }
        for (String m : modules) {

            ArrayList<Api> apis = new ArrayList<>();
            for (Api api : apiList) {
                if (m.equals(api.getModule())) {
                    apis.add(api);
                }
            }

            //生成Test
            System.out.println("生成模块:Test");
            HashMap<String, Object> ctx = new HashMap<String, Object>();
            ctx.put("tool", tool);
            ctx.put("domain", String.join(".", domainList));

            HashSet<String> hashSet = new HashSet<>();
            for (Api api : apiList) {
                hashSet.add(api.getModule());
            }
            ctx.put("module", m);
            ctx.put("apiList", apis);
            ctx.put("className", Tool.abb2Abb(m) + "ApiTest");
            File api = Tool.createFile(testApiDomain.getAbsolutePath(), Tool.abb2Abb(m) + "ApiTest.java");
            freeMarkerManager.outputTemp(api, "Java_api/ApiTest.java", ctx);
        }
    }

    private void generateRequest(RequestReader reader) {
        String reqName = reader.getClassName();

        // 查找是否存在对应的Api,有则为公开的请求方法
        Api api = null;
        for (Api api1 : apiList) {
            if (api1.isCheck() && api1.getRequest().equals(reqName)) {
                api = api1;
            }
        }

        List<String> entNames = new ArrayList<>();
        for (String entName : reader.getDeptEntList()) {
            if (entName.matches("(.*\\.ent)\\.(.*)")) {
                Pattern compile = Pattern.compile("(.*\\.ent)\\.(.*)");
                Matcher matcher = compile.matcher(entName);

                if (matcher.find()) {
                    String EntName = matcher.group(2);
                    entNames.add(EntName);

                    List<File> files = Tool.findEntity(this.module, EntName + ".java");
                    for (File file : files) {
                        EntityReader entReader = new EntityReader(file);
                        generateEntity(entReader);
                    }
                }
            } else {
                List<File> files = Tool.findEntity(this.module, entName + ".java");
                for (File file : files) {
                    EntityReader entReader = new EntityReader(file);
                    generateEntity(entReader);
                }
            }

        }

        List<String> reqNames = new ArrayList<>();
        for (String depReqName : reader.getDeptReqList()) {
            if (depReqName.matches("(.*\\.req)\\.(.*)")) {
                Pattern compile = Pattern.compile("(.*\\.req)\\.(.*)");
                Matcher matcher = compile.matcher(depReqName);
                if (matcher.find()) {
                    entNames.add(depReqName);
                    List<File> files = Tool.findRequest(this.module, depReqName + ".java");
                    for (File file : files) {
                        RequestReader reqReader = new RequestReader(file);
                        generateRequest(reqReader);
                    }
                }
            } else {
                List<File> files = Tool.findRequest(this.module, depReqName + ".java");
                for (File file : files) {
                    generateRequest(new RequestReader(file));
                }
            }
        }

        {
            HashMap<String, Object> ctx = new HashMap<String, Object>();
            ctx.put("domain", reader.getDomainName());
            ctx.put("module", reader.getModuleName());
            ctx.put("isTempReq", api == null);
            if (api != null) {
                ctx.put("target", api.getTarget());
                ctx.put("method", api.getMethod());
            } else {
                ctx.put("target", "");
                ctx.put("method", "");
            }
            ctx.put("importList", reader.getImportList());
            ctx.put("entNames", entNames);
            ctx.put("reqNames", reqNames);
            ctx.put("annotation", reader.getNotesList());
            ctx.put("className", reader.getClassName());
            ctx.put("rspClassName", reader.getClassName().replaceAll("Request", "Response"));
            ctx.put("body", reader.getBody());
            ctx.put("tool", tool);
            ctx.put("hasList", reader.isHasList());
            ctx.put("findOrSearchflag", reader.getFindOrSearchflag());

            File req = Tool.createPath(apiModule.getAbsolutePath(), reader.getModuleName(), "req");
            File file = new File(req, reader.getClassName() + ".java");

            freeMarkerManager.outputTemp(file, "Java_api/module/req/request.java", ctx);
            System.out.println("生成文件" + file.getName() + "成功");
        }
    }

    private void generateResponse(ResponseReader reader) {
        List<String> entNames = new ArrayList<>();
        for (String s : reader.getDeptEntList()) {
            Pattern compile = Pattern.compile("(.*\\.ent)\\.(.*)");
            Matcher matcher = compile.matcher(s);

            if (matcher.find()) {
                String Package = matcher.group(1);
                String EntName = matcher.group(2);
                entNames.add(EntName);

                List<File> files = Tool.findEntity(this.module, EntName + ".java");

                for (File file : files) {
                    EntityReader entReader = new EntityReader(file);
                    generateEntity(entReader);
                }
            }
        }

        {
            HashMap<String, Object> ctx = new HashMap<String, Object>();
            ctx.put("domain", reader.getDomainName());
            ctx.put("module", reader.getModuleName());
            ctx.put("importList", reader.getImportList());
            ctx.put("entNames", entNames);
            ctx.put("annotation", reader.getNotesList());
            ctx.put("className", reader.getClassName());
            ctx.put("body", reader.getBody());
            ctx.put("tool", tool);
            ctx.put("hasList", reader.isHasList());
            ctx.put("Tclass", reader.getTclass());

            File rsp = Tool.createPath(apiModule.getAbsolutePath(), reader.getModuleName(), "rsp");
            File file = new File(rsp, reader.getClassName() + ".java");

            freeMarkerManager.outputTemp(file, "Java_api/module/rsp/response.java", ctx);
            System.out.println("生成文件" + reader.getClassName() + "成功");
        }
    }

    private void generateEntity(EntityReader reader) {
        HashMap<String, Object> ctx = new HashMap<String, Object>();
        ctx.put("domain", reader.getDomainName());
        ctx.put("module", reader.getModuleName());
        ctx.put("importList", reader.getImportList());
        ctx.put("annotation", reader.getNotesList());
        ctx.put("className", reader.getClassName());
        ctx.put("body", reader.getBody());
        ctx.put("tool", tool);
        File ent = Tool.createPath(apiModule.getAbsolutePath(), reader.getModuleName(), "ent");
        File EntFile = new File(ent, reader.getClassName() + ".java");
        freeMarkerManager.outputTemp(EntFile, "Java_api/module/ent/entity.java", ctx);
        System.out.println("生成文件" + reader.getClassName() + "成功");
    }
}
