package com.lantone.qc.kernel.catalogue.behospitalized;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.kernel.structure.ai.BeHospitalizedAI;
import com.lantone.qc.kernel.structure.ai.model.ConflictFinder;
import com.lantone.qc.kernel.structure.ai.model.EntityBlock;
import com.lantone.qc.kernel.structure.ai.model.Lemma;
import com.lantone.qc.kernel.structure.ai.model.Relation;
import com.lantone.qc.kernel.util.CatalogueUtil;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.label.PresentLabel;
import com.lantone.qc.pub.model.label.VitalLabelSpecial;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

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

/**
 * @ClassName : BEH03059
 * @Description : 现病史描述与专科检查相互矛盾
 * @Author : 胡敬
 * @Date: 2020-07-31 11:09
 */
@Component
public class BEH03059 extends QCCatalogue {

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getBeHospitalizedDoc() == null) {
            return;
        }

        Map<String, String> beHospitalStructure = inputInfo.getBeHospitalizedDoc().getStructureMap();
        String present = beHospitalStructure.get("现病史");
        String specialVital = beHospitalStructure.get("专科体格检查");

        if (StringUtil.isBlank(present) || StringUtil.isBlank(specialVital)) {
            return;
        }

        PresentLabel presentLabel = inputInfo.getBeHospitalizedDoc().getPresentLabel();
        VitalLabelSpecial vitalLabelSpecial = inputInfo.getBeHospitalizedDoc().getVitalLabelSpecial();
        if (presentLabel == null || vitalLabelSpecial == null) {
            return;
        }
        JSONObject pCrfOutput = presentLabel.getCrfOutput();
        JSONObject vsCrfOutput = vitalLabelSpecial.getCrfOutput();
        if (pCrfOutput.size() == 0 || vsCrfOutput.size() == 0) {
            return;
        }
        List<Lemma> presentLemmas = getLemma(pCrfOutput);//现病史实体
        List<Lemma> vitalSpecialLemmas = getLemma(vsCrfOutput);//现病史关系
        List<Relation> presentRelations = getRelation(pCrfOutput);//专科检查实体
        List<Relation> vitalSpecialRelations = getRelation(vsCrfOutput);//专科检查关系

        ConflictFinder finder = new ConflictFinder();
        Object[] checkPairs;
        Object[] presentPairs;
        checkPairs = new Object[] { vitalSpecialLemmas, vitalSpecialRelations };
        presentPairs = new Object[] { presentLemmas, presentRelations };

        List<Object[]> positionPairs = finder.findConflictPositions(checkPairs, presentPairs);
        if (positionPairs.size() > 0) {
            status.set("-1");
            String infoStr = "";
            for (int i = 0; i < positionPairs.size(); i++) {
                if (i % 2 != 0) {
                    String words = "";
                    Object[] entityBlocks = positionPairs.get(i);
                    for (Object o : entityBlocks) {
                        EntityBlock entityBlock = (EntityBlock) o;
                        words += entityBlock.getSearchWord() + "->";
                    }
                    infoStr = CatalogueUtil.concatInfo(infoStr, words.substring(0, words.length() - 2));
                }
            }
            info.set(infoStr);
        }
    }

    private List<Lemma> getLemma(JSONObject crfOutput) {
        List<Lemma> lemmaList = new ArrayList<>();
        JSONObject aiOut = crfOutput.getJSONObject(BeHospitalizedAI.entityRelationObject).getJSONObject(BeHospitalizedAI.outputs);
        JSONObject annotation = aiOut.getJSONObject("annotation");
        JSONArray entitys = annotation.getJSONArray("T");
        entitys.remove(0);//第一个为""，这里去除
        Lemma lemma;
        for (Object entityObject : entitys) {
            JSONObject entity = (JSONObject) entityObject;
            lemma = new Lemma();
            lemma.setFrom(entity.getInteger("start"));
            lemma.setTo(entity.getInteger("end"));
            lemma.setProperty(entity.getString("name"));
            lemma.setText(entity.getString("value"));
            lemma.setId(entity.getInteger("id"));
            lemmaList.add(lemma);
        }
        return lemmaList;
    }

    private List<Relation> getRelation(JSONObject crfOutput) {
        List<Relation> relationList = new ArrayList<>();
        JSONObject aiOut = crfOutput.getJSONObject(BeHospitalizedAI.entityRelationObject).getJSONObject(BeHospitalizedAI.outputs);
        JSONObject annotation = aiOut.getJSONObject("annotation");
        JSONArray relations = annotation.getJSONArray("R");
        relations.remove(0);//第一个为""，这里去除
        Relation relation;
        for (Object relationObject : relations) {
            JSONObject entity = (JSONObject) relationObject;
            relation = new Relation();
            relation.setFrom(entity.getInteger("from"));
            relation.setTo(entity.getInteger("to"));
            relation.setRelationName(entity.getString("name"));
            relationList.add(relation);
        }
        return relationList;
    }
}