package xyz.wbsite.dbtool.javafx.tool;

import xyz.wbsite.dbtool.javafx.po.DocParam;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DocRequestReader extends RequestReader {
    private String classNote;
    private List<DocParam> fieldDocList;
    private Map<String, List<DocParam>> ents;

    public DocRequestReader(File javaClass) {
        super(javaClass);
        fieldDocList = new ArrayList<>();
        ents = new HashMap<>();

        List<String> notesList = getNotesList();
        for (String s : notesList) {
            s = s.trim();
            // 提取注释
            if ((s.startsWith("/**") || s.endsWith("*/") || s.startsWith("*")) && !s.contains("@")) {
                s = s.replaceAll("^/\\*\\*", "");
                s = s.replaceAll("^\\*/$", "");
                s = s.replaceAll("^\\*", "");
                classNote = nullF(classNote) + s.trim();
            }
        }

        DocParam docParam = new DocParam();
        for (String s : getBody()) {
            s = s.trim();

            // 提取注释
            if (s.startsWith("/**") || s.endsWith("*/") || s.startsWith("*")) {
                s = s.replaceAll("^/\\*\\*", "");
                s = s.replaceAll("^\\*/$", "");
                s = s.replaceAll("^\\*", "");
                docParam.setNote(nullF(docParam.getNote()) + s.trim());
            }

            // 必填项检查
            if (s.startsWith("@NotBlank") || s.startsWith("@NotNull")) {
                docParam.setRequired(true);
            }

            if (s.matches("private\\s(.*)\\s(.*);")) {
                Pattern compile = Pattern.compile("private\\s(.*)\\s(.*);");
                Matcher matcher = compile.matcher(s);
                if (matcher.find()) {
                    String type = matcher.group(1);
                    docParam.setType(type);
                    if (!Tool.getJavaField().contains(type)) {//读取自定义对象

                        if (type.endsWith("Request")) {
                            String module = null;
                            for (String s1 : getImportList()) {
                                Pattern compile1 = Pattern.compile("import\\s+.*\\.module\\.(.*)\\.req\\." + type);
                                Matcher matcher1 = compile1.matcher(s1);
                                if (matcher1.find()) {
                                    module = matcher1.group(1);
                                }
                            }
                            if (module == null) {// 同目录下查找
                                List<File> files = Tool.find(this.getJavaClass().getParentFile().getParentFile(), type + ".java");
                                if (files.size() == 1) {
                                    DocRequestReader docRequestReader = new DocRequestReader(files.get(0));
                                    ents.put(docRequestReader.getModuleName() + "#" + type, docRequestReader.getFieldDocList());
                                }
                            } else {//其他模块下查找
                                File path = Tool.createPath(this.getJavaClass().getParentFile().getParentFile().getParentFile(), module);
                                List<File> files = Tool.find(path, type + ".java");
                                if (files.size() == 1) {
                                    DocRequestReader docRequestReader = new DocRequestReader(files.get(0));
                                    ents.put(docRequestReader.getModuleName() + "#" + type, docRequestReader.getFieldDocList());
                                }
                            }
                        } else {
                            String module = null;
                            for (String s1 : getImportList()) {
                                Pattern compile1 = Pattern.compile("import\\s+.*\\.module\\.(.*)\\.ent\\." + type);
                                Matcher matcher1 = compile1.matcher(s1);
                                if (matcher1.find()) {
                                    module = matcher1.group(1);
                                }
                            }

                            if (module == null) {// 同目录下查找
                                List<File> files = Tool.find(this.getJavaClass().getParentFile().getParentFile(), type + ".java");
                                if (files.size() == 1) {
                                    DocEntityReader docEntityReader = new DocEntityReader(files.get(0));
                                    ents.put(docEntityReader.getModuleName() + "#" + type, docEntityReader.getFieldDocList());
                                }
                            } else {//其他模块下查找
                                File path = Tool.createPath(this.getJavaClass().getParentFile().getParentFile().getParentFile(), module);
                                List<File> files = Tool.find(path, type + ".java");
                                if (files.size() == 1) {
                                    DocEntityReader docEntityReader = new DocEntityReader(files.get(0));
                                    ents.put(docEntityReader.getModuleName() + "#" + type, docEntityReader.getFieldDocList());
                                }
                            }
                        }
                    }
                    docParam.setName(matcher.group(2));
                    fieldDocList.add(docParam);
                    docParam = new DocParam();
                }
            }
        }
    }

    private String nullF(String value) {
        return value == null ? "" : value;
    }

    public List<DocParam> getFieldDocList() {
        return fieldDocList;
    }

    public void setFieldDocList(List<DocParam> fieldDocList) {
        this.fieldDocList = fieldDocList;
    }

    public String getClassNote() {
        return classNote;
    }

    public void setClassNote(String classNote) {
        this.classNote = classNote;
    }
}
