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

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

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

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

    @Override
    public Object processData(Object data, Object code) {
        List<String> splitStr = StringUtils.changeStringToString(data.toString(), "\n");
        splitStr.removeIf(item -> item.equals(""));

        List<String> processList = splitStr.stream().map(item -> item.replaceAll(" ", "\\|")).collect(Collectors.toList());

        StringBuilder all = new StringBuilder();
        StringBuilder one = new StringBuilder();
        List<String> messageList;
        Pattern p = Pattern.compile("[\\d]*");
        int flag1 = 0;
        for (int j = 0; j < processList.size(); j++) {
            if (j < processList.size() - 1) {
                Matcher m = p.matcher(processList.get(j).substring(0, 4));
                if (m.matches()) {
                    flag1 = j;
                }
                if (!m.matches()) {
                    String replace = processList.get(j).replaceAll("\\|", " ");
                    processList.set(flag1, processList.get(flag1) + "<br>" + replace);
                    processList.set(j, replace);
                }
            }
        }

        for (String s : processList) {
            messageList = StringUtils.changeStringToString(s, "\\|");

            if (messageList.size() == 4 && messageList.contains("授权")) {
                messageList.remove(0);
            } else if (messageList.size() % 3 != 0) {
                continue;
            }

            for (int i = 0; i < messageList.size(); i += 3) {
                if (one.toString().equals("")) {
                    one = new StringBuilder("\"inpadocData\"" + ":[" + "{" + "\"name\"" + ":" + "\"发生日期\"" + "," + "\"content\"" + ":" + "\"" + messageList.get(i).trim() + "\"" + "}");
                } else {
                    one.append("," + "{" + "\"name\"" + ":" + "\"发生日期\"" + "," + "\"content\"" + ":" + "\"").append(messageList.get(i).trim()).append("\"").append("}");
                }
                one.append(",{").append("\"name\":\"法律状态\"").append(",").append("\"content\":").append("\"").append(messageList.get(i + 1).trim()).append("\"").append("}");
                one.append("," + "{" + "\"name\"" + ":" + "\"描述信息\"" + "," + "\"content\"" + ":" + "\"").append(messageList.get(i + 2).trim()).append("\"").append("}]");

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

        }
        all = new StringBuilder("[" + all + "]");
        return all.toString();
    }
}
