package com.lantone.qc.kernel.structure.ai.process;

import com.alibaba.fastjson.JSONObject;
import com.lantone.qc.kernel.structure.ai.model.EntityEnum;
import com.lantone.qc.kernel.structure.ai.model.Lemma;
import com.lantone.qc.pub.model.entity.Clinical;
import com.lantone.qc.pub.model.entity.Diag;
import com.lantone.qc.pub.model.entity.Vital;
import com.lantone.qc.pub.model.label.OperationDiscussionLabel;
import com.lantone.qc.pub.model.label.ThreeLevelWardLabel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : EntityProcessOperationDiscussion
 * @Description : 术后首次病程及谈话记录
 * @Author : 胡敬
 * @Date: 2020-03-24 10:48
 */
public class EntityProcessOperationDiscussion extends EntityProcess {
    private List<String> titleList = Arrays.asList(
            EntityEnum.TITLE.toString(),
            EntityEnum.TITLE_OF_OPERATIVE_FINDINGS.toString(),
            EntityEnum.TITLE_OF_MEASURES_AFTER_OP.toString(),
            EntityEnum.TITLE_OF_RISK_AFTER_OP.toString(),
            EntityEnum.TITLE_OF_ATTENTION_AFTER_OP.toString());

    public OperationDiscussionLabel extractEntity(JSONObject aiOut) {
        OperationDiscussionLabel operationDiscussionLabel = new OperationDiscussionLabel();
        String content = aiOut.getString("content");

        Map<String, List<String>> titleText = subWithTitle(aiOut);
        //手术经过文本(术中所见)
        if (titleText.get(EntityEnum.TITLE_OF_OPERATIVE_FINDINGS.toString()) != null) {
            List<String> operativeFindingsList = titleText.get(EntityEnum.TITLE_OF_OPERATIVE_FINDINGS.toString());
            String text = textJoin(content, operativeFindingsList);
            operationDiscussionLabel.setOperativeFindings(text);
        }

        //术后风险文本
        if (titleText.get(EntityEnum.TITLE_OF_RISK_AFTER_OP.toString()) != null) {
            List<String> riskAfterOpList = titleText.get(EntityEnum.TITLE_OF_RISK_AFTER_OP.toString());
            String text = textJoin(content, riskAfterOpList);
            operationDiscussionLabel.setRiskAfterOp(text);
        }

        //术后注意事项文本(处理及注意事项)
        if (titleText.get(EntityEnum.TITLE_OF_ATTENTION_AFTER_OP.toString()) != null) {
            List<String> attentionAfterOpList = titleText.get(EntityEnum.TITLE_OF_ATTENTION_AFTER_OP.toString());
            String text = textJoin(content, attentionAfterOpList);
            operationDiscussionLabel.setAttentionAfterOp(text);
        }

        //术后处理措施文本(处理及注意事项)
        if (titleText.get(EntityEnum.TITLE_OF_MEASURES_AFTER_OP.toString()) != null) {
            List<String> measuresAfterOpList = titleText.get(EntityEnum.TITLE_OF_MEASURES_AFTER_OP.toString());
            String text = textJoin(content, measuresAfterOpList);
            operationDiscussionLabel.setMeasuresAfterOp(text);
        }

        //生命体征
        List<Lemma> vitalLemmas = createEntityTree(aiOut, EntityEnum.VITAL.toString());
        List<Vital> vitals = new ArrayList<>();
        for (Lemma lemma : vitalLemmas) {
            Vital vital = new Vital();
            vital.setValue(lemma.getText());
            vitals.add(vital);
        }
        operationDiscussionLabel.setVitals(vitals);
        return operationDiscussionLabel;
    }

    private String textJoin(String content, List<String> operativeFindingsList) {
        StringBuffer text = new StringBuffer();
        for (String titleTextIndex : operativeFindingsList) {
            String[] titleTextIndexSplit = titleTextIndex.split(",");
            text.append(content.substring(Integer.parseInt(titleTextIndexSplit[0]),
                    Integer.parseInt(titleTextIndexSplit[1])));
        }
        return text.toString();
    }

    /**
     * 根据各标题截取文本index
     *
     * @param aiOut
     * @return
     */
    private Map<String, List<String>> subWithTitle(JSONObject aiOut) {
        Map<String, List<String>> titleText = new LinkedHashMap<>();
        String content = aiOut.getString("content");
        List<Lemma> lemmaList = loadAllLemmaList(aiOut.getJSONObject("annotation").getJSONArray("T"));
        Lemma lemma;
        int start = 0;
        String subContentIndex = "", title = "";
        for (int i = 0; i < lemmaList.size(); i++) {
            lemma = lemmaList.get(i);
            if (!titleList.contains(lemma.getProperty()) && i != lemmaList.size() - 1) {
                continue;
            }
            if (i != lemmaList.size() - 1) {
                if (start == 0) {
                    start = Integer.parseInt(lemma.getPosition());
                    title = lemma.getProperty();//截取的这一段文本的标题
                } else {
                    /*subContentIndex = start + lemma.getText().length() + 1 + "," + Integer.parseInt(lemma.getPosition());*/
                    subContentIndex = start + "," + Integer.parseInt(lemma.getPosition());
                    putSubContent(titleText, title, subContentIndex);
                    start = Integer.parseInt(lemma.getPosition());
                    title = lemma.getProperty();//截取的这一段文本的标题
                }
            } else {
                //将倒数第二个标题存入结构
                subContentIndex = start + "," + Integer.parseInt(lemma.getPosition());
                putSubContent(titleText, title, subContentIndex);
                //如果是最后一个Lemma，文本就从当前lemma的position开始取，取到结束
                title = lemma.getProperty();//截取的这一段文本的标题
                if (titleList.contains(title)) {
                    start = Integer.parseInt(lemma.getPosition());
                    int lastIndex = content.length() - 1;
                    subContentIndex = start + "," + lastIndex;
                    putSubContent(titleText, title, subContentIndex);
                }
            }
        }
        return titleText;
    }

    private void putSubContent(Map<String, List<String>> titleText, String text, String subContent) {
        List<String> textList;
        if (titleText.containsKey(text)) {
            titleText.get(text).add(subContent);
        } else {
            textList = new ArrayList<>();
            textList.add(subContent);
            titleText.put(text, textList);
        }
    }
}