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

import com.alibaba.fastjson.JSONArray;
import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.kernel.client.ChiefPresentSimilarityServiceClient;
import com.lantone.qc.kernel.structure.ai.ModelAI;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.entity.Diag;
import com.lantone.qc.pub.model.entity.GeneralDesc;
import com.lantone.qc.pub.model.entity.Negative;
import com.lantone.qc.pub.model.label.DiagLabel;
import com.lantone.qc.pub.model.label.PastLabel;
import com.lantone.qc.pub.model.label.PresentLabel;
import com.lantone.qc.pub.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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


/**
 * @ClassName : BEH02980
 * @Description : 病历前后描述不一致
 * @Author : Mark
 * @Date: 2020-06-23 11:02
 */
@Component
public class BEH02980 extends QCCatalogue {
    @Autowired
    ChiefPresentSimilarityServiceClient chiefPresentSimilarityServiceClient;

    private List<String> containList = Arrays.asList("脑萎缩", "慢性", "纤颤", "高血压", "糖尿", "冠状", "冠心病", "支架", "起搏器", "房颤", "风湿");
    private List<String> filterList = Arrays.asList("心脏病", "低血糖", "急性", ";", "门脉高压症", "高尿酸血症", "高脂血症");

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        PresentLabel presentLabel = inputInfo.getBeHospitalizedDoc().getPresentLabel();
        DiagLabel initialDiagLabel = inputInfo.getBeHospitalizedDoc().getInitialDiagLabel();
        PastLabel pastLabel = inputInfo.getBeHospitalizedDoc().getPastLabel();

        List<String> pos_diags = new ArrayList<>();
        List<String> neg_diags = new ArrayList<>();
        //现病史需要取一般情况之后疾病
        if (presentLabel != null) {
            List<GeneralDesc> generals = presentLabel.getGenerals();
            if (generals.size() > 0) {
                String presentText = presentLabel.getText();
                List<Diag> presentDiags = presentLabel.getDiags();
                /* 取现病史中一般情况之后的疾病名称 */
                if (StringUtil.isNotBlank(presentText) && presentDiags.size() > 0) {
                    String lastGeneral = generals.get(generals.size() - 1).getName();
                    int lastGeneralIndex = presentText.indexOf(lastGeneral);
                    for (Diag presentDiag : presentDiags) {
                        if (presentDiag.getNegative() != null || presentDiag.getHospitalDiagName().contains("否认")) {
                            continue;
                        }
                        /* 现病史中一般情况之后的疾病名称 */
                        if (presentText.indexOf(presentDiag.getHospitalDiagName()) > lastGeneralIndex) {
                            if (isContains(presentDiag.getHospitalDiagName()) && !isFilter(presentDiag.getHospitalDiagName())) {
                                String dgname = presentDiag.getHospitalDiagName();
                                if (presentDiag.getNegative()==null) {
                                    if (!pos_diags.contains(dgname)) {
                                        pos_diags.add(dgname);
                                    }
                                }
                                else {
                                    if (!neg_diags.contains(dgname)) {
                                        neg_diags.add(dgname);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }


        if (pastLabel != null && StringUtils.isNotEmpty(pastLabel.getText())) {
            extract_diags(pastLabel.getDiags(), pos_diags, neg_diags);
        } else {
            //例如邵逸夫 台州这种结构化数据，判断高血压 和 糖尿病是否有冲突
            List<Diag> pastDiags = new ArrayList<>();
            Map<String, String> structureMap = inputInfo.getBeHospitalizedDoc().getStructureMap();
            String a1 = structureMap.get("高血压");
            Diag diag = new Diag();
            diag.setName("高血压");
            diag.setHospitalDiagName("高血压");
            if (StringUtils.isNotEmpty(a1) && "否认".equals(a1)) {
                Negative negative = new Negative();
                negative.setName("否认");
                diag.setNegative(negative);
                pastDiags.add(diag);
            } else if (StringUtils.isNotEmpty(a1) && !"否认".equals(a1)) {
                pastDiags.add(diag);
            }

            a1 = structureMap.get("糖尿病");
            diag = new Diag();
            diag.setName("糖尿病");
            diag.setHospitalDiagName("糖尿病");
            if  (StringUtils.isNotEmpty(a1) && "否认".equals(a1)) {
                Negative negative = new Negative();
                negative.setName("否认");
                diag.setNegative(negative);
                pastDiags.add(diag);
            } else if (StringUtils.isNotEmpty(a1) && !"否认".equals(a1)) {
                pastDiags.add(diag);
            }

            extract_diags(pastDiags, pos_diags, neg_diags);
        }
        if (initialDiagLabel != null) {
            extract_diags(initialDiagLabel.getDiags(), pos_diags, neg_diags);
        }

        String infoStr = "";
        int matchSum = 0;
        ModelAI modelAI = new ModelAI();

        for (String negdiag : neg_diags) {
            JSONArray jsonArray = modelAI.loadChiefPresentSimilarAI(negdiag, pos_diags, false
                    , "diagnose", chiefPresentSimilarityServiceClient);
            if (jsonArray.size() == 2) {
                /* 相似度最高症状 */
                String dgname = jsonArray.getString(0);
                /* 相似度分数 */
                double likeRate = jsonArray.getDoubleValue(1);
                if (likeRate > 0.95) {
                    matchSum++;
                    if (StringUtils.isEmpty(infoStr)) {
                        infoStr = negdiag;
                    } else {
                        infoStr = infoStr + "," + negdiag;
                    }
                    break;
                }
            }
        }
        if (matchSum > 0) {
            status.set("-1");
            info.set(infoStr);
        }

    }


    private List<String> extract_diags(List<Diag> diags, List<String> pos_diags, List<String> neg_diags ) {
        List<String> dgs = new ArrayList<>();
        for (Diag dg:diags) {
            if (isFilter(dg.getHospitalDiagName())) {
                String dgname = dg.getHospitalDiagName();
                if (dg.getNegative() == null) {
                    if (!pos_diags.contains(dgname)) {
                        pos_diags.add(dgname);
                    }
                } else {
                    if (!neg_diags.contains(dgname)) {
                        neg_diags.add(dgname);
                        dgs.add(dgname);
                    }
                }
            }
        }

        return dgs;
    }

    private boolean isContains(String diagName) {
        for (String c : containList) {
            if (diagName.contains(c)) {
                return true;
            }
        }
        return false;
    }

    private boolean isFilter(String diagName) {
        for (String c : filterList) {
            if (diagName.contains(c)) {
                return true;
            }
        }
        return false;
    }

}