package cn.cslg.pas.common.utils.handler;

import cn.cslg.pas.common.utils.StringUtils;

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

public class IncoPatPatentAffairHandler implements IProcessData {
    public IncoPatPatentAffairHandler() {
    }

    @Override
    public Object processData(Object data, Object code) {
        String processedData = data.toString().replaceAll("\r\n", "");
        List<String> contentList = StringUtils.changeStringToString(processedData, "([\\u4e00-\\u9fa5]+)：");
        contentList.remove(0);

        List<String> tableList = new ArrayList<>();
        Pattern p = Pattern.compile("([\\u4e00-\\u9fa5]+)：");
        Matcher m = p.matcher(data.toString());
        while (m.find()) {
            tableList.add(m.group(1));
        }

        StringBuilder all = new StringBuilder();
        StringBuilder one = new StringBuilder();
        for (int i = 0; i < tableList.size(); i++) {
            if (tableList.get(i).contains("法律状态公告日")) {
                if (!one.toString().equals("")) {
                    if (!all.toString().equals("")) {
                        all.append(",{").append(one).append("]").append("}");
                    } else {
                        all = new StringBuilder("{" + one + "]}");
                    }
                }
                one = new StringBuilder();
            }

            if (one.toString().equals("")) {
                one = new StringBuilder("\"inpadocData\"" + ":[" + "{" + "\"name\"" + ":" + "\"" + tableList.get(i) + "\"" + ",\"" + "content\"" + ":" + "\"" + contentList.get(i).replaceAll(";", "") + "\"" + "}");
            } else {
                one.append(",").append("{" + "\"name\"" + ":").append("\"").append(tableList.get(i)).append("\"" + "," + "\"" + "content\"" + ":" + "\"").append(contentList.get(i).replaceAll(";", "")).append("\"" + "}");
            }

        }
        if (!one.toString().equals("")) {
            if (!all.toString().equals("")) {
                all.append(",{").append(one).append("]").append("}");
            } else {
                all = new StringBuilder("{" + one + "]}");
            }
        }
        all = new StringBuilder("[" + all + "]");
        return all.toString();
    }
}
