package xyz.wbsite.dbtool.javafx.tool;

import xyz.wbsite.dbtool.javafx.po.DocParam;
import xyz.wbsite.dbtool.web.frame.utils.FileUtil;
import xyz.wbsite.dbtool.web.frame.utils.MapperUtil;

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

public class DocEntityReader extends EntityReader{
    private List<DocParam> fieldDocList;

    public DocEntityReader(File javaClass) {
        super(javaClass);
        fieldDocList = new ArrayList<>();
        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()) {
                    docParam.setType(matcher.group(1));
                    docParam.setName(matcher.group(2));
                    fieldDocList.add(docParam);
                    System.out.println(MapperUtil.toJson(docParam));
                    docParam = new DocParam();
                }
            }
        }
    }

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

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

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