package com.lantone.structure.ai;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.lantone.structure.client.CRFServiceClient;
import com.lantone.structure.ai.process.EntityProcessDiag;
import com.lantone.structure.ai.process.EntityProcessDrug;
import com.lantone.structure.ai.process.EntityProcessLeaveHospital;
import com.lantone.common.util.CatalogueUtil;
import com.lantone.structure.model.Content;
import com.lantone.structure.model.InputInfo;
import com.lantone.structure.model.doc.LeaveHospitalDoc;
import com.lantone.structure.model.entity.Diag;
import com.lantone.structure.model.entity.Drug;
import com.lantone.structure.model.label.DiagLabel;
import com.lantone.structure.model.label.LeaveHospitalLabel;
import com.lantone.common.util.StringUtil;
import org.apache.commons.lang3.StringUtils;

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

/**
 * @ClassName : DischargeAI
 * @Description :
 * @Author : 胡敬
 * @Date: 2020-03-20 11:00
 */
public class LeaveHospitalAI extends ModelAI {
    /**
     *
     */
    public static List<String> medicalTextType = Arrays.asList("DischargeQA_cx", "Diagnoses_cx", "CourseRecordSRR");
    public static String entityRelationObject = "entity_relation_object";
    public static String outputs = "outputs";
    public static String content = "content";

    public void medrec(InputInfo inputInfo, CRFServiceClient crfServiceClient) {
        JSONArray crfContent = new JSONArray();
        LeaveHospitalDoc leaveHospitalDoc = inputInfo.getLeaveHospitalDoc();
        if (leaveHospitalDoc != null) {
            if (StringUtils.isNotEmpty(leaveHospitalDoc.getText())) {
                //出院小结全部文本(之后考虑删除传全部文本，改为传相应结构化文本)
                putContent(crfContent, medicalTextType.get(0), leaveHospitalDoc.getText(), Content.discharge);
            }

            Map<String, String> leaveHospitalStructureMap = leaveHospitalDoc.getStructureMap();
            //入院情况
            //if (leaveHospitalStructureMap.get(Content.inStatus) != null) {
            //    putContent(crfContent, medicalTextType.get(1), leaveHospitalStructureMap.get(Content.inStatus), Content.inStatus);
            //}
            //出院诊断
            if (StringUtil.isNotBlank(leaveHospitalStructureMap.get(Content.dischargeDiag))) {
                String dischargeDiag = leaveHospitalStructureMap.get(Content.dischargeDiag);
                    /*if (CatalogueUtil.numberExist(dischargeDiag)) {
                        dischargeDiag = CatalogueUtil.removeBetweenWordSpace(dischargeDiag);
                    }*/
                putContent(crfContent, medicalTextType.get(1), dischargeDiag, Content.dischargeDiag);
            }
            if (leaveHospitalDoc.getLeaveHospitalDoctorAdviceLabel() != null
                    && StringUtils.isNotEmpty(leaveHospitalDoc.getLeaveHospitalDoctorAdviceLabel().getAiText())) {
                putContent(crfContent, medicalTextType.get(0), leaveHospitalDoc.getLeaveHospitalDoctorAdviceLabel().getAiText(), Content.leaveHospitalDoctorAdvice);
            }
            //药品
            String drugContent = CatalogueUtil.structureMapJoin(leaveHospitalStructureMap, Lists.newArrayList("诊治经过", "出院带药"));
            putContent(crfContent, medicalTextType.get(2), drugContent, "药品");

            JSONObject midData = loadAI(crfContent, crfServiceClient);//crf返回数据
            if (midData.containsKey(Content.discharge)) {
                putDischargeCrfData(midData.getJSONObject(Content.discharge), inputInfo);//出院小结
            }
            if (midData.containsKey(Content.dischargeDiag)) {
                putDiagCrfData(midData.getJSONObject(Content.dischargeDiag), inputInfo);//出院诊断
            }
            if (midData.containsKey(Content.leaveHospitalDoctorAdvice)) {
                //putLeaveHospitalDoctorAdviceCrfData(midData.getJSONObject(Content.leaveHospitalDoctorAdvice), inputInfo);//出院医嘱
            }
            if (midData.containsKey("药品")) {
                putDrugCrfData(midData.getJSONObject("药品"), inputInfo);
            }
        }
    }


    /**
     * 病历特点
     *
     * @param jsonObject
     * @param inputInfo
     */
    public void putDischargeCrfData(JSONObject jsonObject, InputInfo inputInfo) {
        JSONObject aiOut = loadEntity(jsonObject, entityRelationObject, outputs, content);
        if (aiOut == null) {
            return;
        }
        EntityProcessLeaveHospital entityProcessLeaveHospital = new EntityProcessLeaveHospital();
        LeaveHospitalLabel leaveHospitalLabel = entityProcessLeaveHospital.extractEntity(aiOut);
        inputInfo.getLeaveHospitalDoc().setLeaveHospitalLabel(leaveHospitalLabel);
    }

    /**
     * 存放抓取的疾病
     *
     * @param jsonObject
     * @param inputInfo
     */
    public void putDiagCrfData(JSONObject jsonObject, InputInfo inputInfo) {
        JSONObject aiOut = loadEntity(jsonObject, entityRelationObject, outputs, content);
        if (aiOut == null) {
            return;
        }
        EntityProcessDiag entityProcessDiag = new EntityProcessDiag();
        List<Diag> diags = entityProcessDiag.extractEntity(aiOut);
        DiagLabel diagLabel = new DiagLabel();
        diagLabel.setDiags(diags);
        inputInfo.getLeaveHospitalDoc().setLeaveDiagLabel(diagLabel);
    }

    /**
     * 存放抓取的药品
     *
     * @param jsonObject
     * @param inputInfo
     */
    public void putDrugCrfData(JSONObject jsonObject, InputInfo inputInfo) {
        JSONObject aiOut = loadEntity(jsonObject, entityRelationObject, outputs, content);
        if (aiOut == null) {
            return;
        }
        EntityProcessDrug entityProcessDrug = new EntityProcessDrug();
        List<Drug> drugs = entityProcessDrug.extractEntity(aiOut);
        LeaveHospitalDoc leaveHospitalDoc = inputInfo.getLeaveHospitalDoc();
        if (leaveHospitalDoc.getLeaveHospitalLabel() == null) {
            LeaveHospitalLabel label = new LeaveHospitalLabel();
            label.setDrugs(drugs);
            leaveHospitalDoc.setLeaveHospitalLabel(label);
        } else {
            leaveHospitalDoc.getLeaveHospitalLabel().setDrugs(drugs);
        }
    }

    /**
     * 出院医嘱
     *
     * @param jsonObject
     * @param inputInfo
     */
    public void putLeaveHospitalDoctorAdviceCrfData(JSONObject jsonObject, InputInfo inputInfo) {
    }
}