package xyz.wbsite.dbtool.javafx.tool;

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ResponseReader {
    private File javaClass;

    private String packageName;
    private String domainName;
    private String moduleName;
    private List<String> annotationList;
    private List<String> importList;
    private Set<String> deptReqList;
    private Set<String> deptEntList;
    private String className;
    private String fatherName;
    private List<String> body;
    private boolean hasList = false;
    private String findOrSearchflag = "0";
    private String Tclass = null;

    public ResponseReader(File javaClass) throws IOException {
        this.javaClass = javaClass;
        importList = new ArrayList<>();
        annotationList = new ArrayList<>();
        deptReqList = new HashSet<>();
        deptEntList = new HashSet<>();
        domainName = "";
        read();
    }

    private void read() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(javaClass), "utf-8"));

        int step = 1;//1-读取package，2-读取import，3-读取类注释，4-读取类名、父类，5-读取类体

        String line = null;
        StringBuffer zs = new StringBuffer();
        StringBuffer sb = new StringBuffer();
        while ((line = bufferedReader.readLine()) != null) {
            if (line.matches("\\s*package\\s(.*);")) {
                {
                    Pattern compile = Pattern.compile("\\s*package\\s(.*);");
                    Matcher matcher = compile.matcher(line);
                    if (matcher.find()) {
                        packageName = matcher.group(1);
                    }
                }

                {
                    Pattern compile = Pattern.compile("\\s*package\\s(.*)\\.module\\.(.*).rsp;");
                    Matcher matcher = compile.matcher(line);
                    if (matcher.find()) {
                        domainName = matcher.group(1);
                        moduleName = matcher.group(2);
                    }
                }
                continue;
            }
            if (line.matches("import\\s.*")) {
                if (line.contains(".ent.")) {
                    Pattern compile = Pattern.compile("import\\s+(.*\\.ent\\..*);");
                    Matcher matcher = compile.matcher(line);
                    if (matcher.find()) {
                        String group = matcher.group(1);
                        deptEntList.add(group);
                    }
                } else if (line.contains(".req.")) {
                    Pattern compile = Pattern.compile("import\\s+(.*\\.req\\..*);");
                    Matcher matcher = compile.matcher(line);
                    if (matcher.find()) {
                        String group = matcher.group(1);
                        deptReqList.add(group);
                    }
                } else if (!line.contains("frame")) {
                    importList.add(line);
                }
                continue;
            }
            if (line.contains("/**") || line.contains("*") || line.contains("*/")) {
                if (zs != null) {
                    zs.append(line + "\n");
                    annotationList.add(line);
                    if (line.contains("*/")) {
                        zs = null;
                    }
                }
            }
            if (line.contains("class")) {
                line = line.replaceAll("\\{", " { ");
                line = line.replaceAll("}", " } ");
                line = line.replaceAll("\\s+", " ");
                String[] split = line.split("\\s");
                for (int i = 0; i < split.length; i++) {
                    if ("class".equals(split[i])) {
                        className = split[i + 1];
                    }
                    if ("extends".equals(split[i])) {
                        fatherName = split[i + 1];

                        if (fatherName.contains("FindResponse") || fatherName.contains("GetAllResponse")) {
                            hasList = true;

                            Pattern pattern = Pattern.compile("<(.*?)>");
                            Matcher matcher = pattern.matcher(fatherName);
                            if (matcher.find()) {
                                String group = matcher.group(1);
                                Tclass = group;
                            }
                        }
                        if (fatherName.contains("FindRequest")) {
                            findOrSearchflag = "1";
                        } else if (fatherName.contains("SearchRequest")) {
                            findOrSearchflag = "2";
                        }
                    }
                }
            }
            sb.append(line + "\n");
        }

        body = new ArrayList<>(Arrays.asList(sb.substring(sb.indexOf("{")).split("\n")));

        for (int i = 0; i < body.size(); i++) {
            if (body.get(i).contains("{")) {
                body.set(i, body.get(i).replaceAll("\\{", ""));
                String s = body.get(i);
                if (s.length() <= 1) {
                    body.remove(i);
                }
                break;
            }
        }
        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);
        }

        bufferedReader.close();
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    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> getAnnotationList() {
        return annotationList;
    }

    public void setAnnotationList(List<String> annotationList) {
        this.annotationList = annotationList;
    }

    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 boolean isHasList() {
        return hasList;
    }

    public void setHasList(boolean hasList) {
        this.hasList = hasList;
    }

    public String getTclass() {
        return Tclass;
    }

    public String getFindOrSearchflag() {
        return findOrSearchflag;
    }

    public void setFindOrSearchflag(String findOrSearchflag) {
        this.findOrSearchflag = findOrSearchflag;
    }

    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 {
        ResponseReader requestReader = new ResponseReader(new File("D:\\wangbing\\Project\\dbtool\\target\\project\\EXAMPLE-WEB\\src\\main\\java\\com\\example\\module\\system\\rsp\\DictFindResponse.java"));

        System.out.println();
    }
}
