package xyz.wbsite.dbtool.javafx.tool;

import xyz.wbsite.dbtool.web.frame.utils.FileUtil;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RequestReader {
    private File javaClass;

    private String domainName;
    private String moduleName;
    private List<String> importList;
    private List<String> notesList;
    private Set<String> deptReqList;
    private Set<String> deptEntList;
    private String className;
    private String fatherName;
    private List<String> body;
    private String Tclass = null;

    public RequestReader(File javaClass) {
        if (!javaClass.getName().endsWith("Request.java")) {
            throw new IllegalArgumentException("not a Request");
        }

        this.javaClass = javaClass;
        importList = new ArrayList<>();
        notesList = new ArrayList<>();
        deptReqList = new HashSet<>();
        deptEntList = new HashSet<>();
        body = new ArrayList<>();
        domainName = "";
        read();
    }

    protected void read() {

        List<String> strings = FileUtil.readFileToLines(javaClass);

        Pattern p1 = Pattern.compile("package\\s+(.*)\\.module\\.(.*).req;");
        Pattern p2 = Pattern.compile("import\\s+(.*\\.ent\\..*);");
        Pattern p3 = Pattern.compile("public\\s+class\\s+(.*Request)\\s+extends\\s+(.*Request)\\s*\\{");
        Pattern p4 = Pattern.compile("private\\s+(.*Request)\\s+(.*);");
        Pattern p5 = Pattern.compile("private\\s+(.*\\.)*([^\\\\.]*)\\s+(.*);");
        for (String line : strings) {
            {
                Matcher matcher = p1.matcher(line.trim());
                if (matcher.find()) {
                    domainName = matcher.group(1);
                    moduleName = matcher.group(2);
                    continue;
                }
            }

            if (line.startsWith("import")) {
                Matcher matcher = p2.matcher(line.trim());
                if (matcher.find()) {
                    deptEntList.add(matcher.group(1));
                } else if (line.contains("frame")) {

                } else if (line.contains("javax.validation")) {

                } else if (line.contains("org.hibernate.validator")) {

                } else {
                    importList.add(line);
                }
                continue;
            }


            if ((line.contains("/**") || line.contains("*") || line.contains("*/")) && className == null) {//未读取到类名之前注释为类注释
                notesList.add(line);
            }

            {
                Matcher matcher = p3.matcher(line.trim());
                if (matcher.find()) {
                    className = matcher.group(1);
                    fatherName = matcher.group(2);
                    continue;
                }
            }

            if (className != null) {
                Matcher matcher = p4.matcher(line.trim());
                if (matcher.find()) {
                    deptReqList.add(matcher.group(1));
                }

                if (line.contains("@ColumnName") ||
                        line.contains("@ColumnDescription")
                        ) {
                    continue;
                }

                body.add(line);
            }
        }

        for (int i = body.size() - 1; i >= 0; i--) {
            if (body.get(i).contains("}")) {
                body.set(i, body.get(i).replaceAll("}", ""));
                break;
            }
        }
        if (body.size() > 1 && "".equals(body.get(0))) {
            body.remove(0);
        }
        if (body.size() > 1 && "".equals(body.get(body.size() - 1))) {
            body.remove(body.size() - 1);
        }
    }

    public String getDomainName() {
        return domainName;
    }

    public void setDomainName(String domainName) {
        this.domainName = domainName;
    }

    public String getModuleName() {
        return moduleName;
    }

    public void setModuleName(String moduleName) {
        this.moduleName = moduleName;
    }

    public List<String> getNotesList() {
        return notesList;
    }

    public void setNotesList(List<String> notesList) {
        this.notesList = notesList;
    }

    public List<String> getImportList() {
        return importList;
    }

    public void setImportList(List<String> importList) {
        this.importList = importList;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getFatherName() {
        return fatherName;
    }

    public void setFatherName(String fatherName) {
        this.fatherName = fatherName;
    }

    public List<String> getBody() {
        return body;
    }

    public void setBody(List<String> body) {
        this.body = body;
    }

    public File getJavaClass() {
        return javaClass;
    }

    public void setJavaClass(File javaClass) {
        this.javaClass = javaClass;
    }

    public void setTclass(String tclass) {
        Tclass = tclass;
    }

    public Set<String> getDeptReqList() {
        return deptReqList;
    }

    public void setDeptReqList(Set<String> deptReqList) {
        this.deptReqList = deptReqList;
    }

    public Set<String> getDeptEntList() {
        return deptEntList;
    }

    public void setDeptEntList(Set<String> deptEntList) {
        this.deptEntList = deptEntList;
    }

    public static void main(String[] args) throws IOException {
        RequestReader requestReader = new RequestReader(new File("E:\\wangbing\\source\\dbtool\\target\\project\\wadmin\\src\\main\\java\\xyz\\wbsite\\module\\system\\req\\DictFindRequest.java"));

        System.out.println();
    }
}
