package xyz.wbsite.dbtool.javafx.tool;

import xyz.wbsite.dbtool.javafx.po.DocParam;
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 DocResponseReader extends ResponseReader {
    private List<DocParam> fieldDocList;

    public DocResponseReader(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.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);
                    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;
    }
}
