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

import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.entity.GeneralDesc;
import com.lantone.qc.pub.model.entity.Medicine;
import com.lantone.qc.pub.model.label.PresentLabel;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

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

/**
 * @ClassName : BEH02909
 * @Description : 目前使用药物情况与现病史描述不一致
 * 两者都不为空
 *现病史中出现的药去目前使用药物情况找，如果找不到就报
 * @Author : 胡敬
 * @Date: 2020-06-11 16:10
 */
@Component
public class BEH02909 extends QCCatalogue {

    @Override
    protected void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getBeHospitalizedDoc() == null) {
            status.set("0");
            return;
        }
        String drugsCurrentlyInUse = inputInfo.getBeHospitalizedDoc().getStructureMap().get("长期用药");
        drugsCurrentlyInUse += inputInfo.getBeHospitalizedDoc().getStructureMap().get("外院用药");
        if (StringUtil.isBlank(drugsCurrentlyInUse)) {
            status.set("0");
            return;
        }

        PresentLabel presentLabel = inputInfo.getBeHospitalizedDoc().getPresentLabel();
        if (presentLabel == null) {
            status.set("0");
            return;
        }
        List<String> drugFromPresent = new ArrayList<>();
        List<GeneralDesc> generals = presentLabel.getGenerals();
        if (generals.size() > 0) {
            String presentText = presentLabel.getText();
            List<Medicine> medicines = presentLabel.getMedicines();
            /* 取现病史中一般情况之后的药品名称 */
            if (StringUtil.isNotBlank(presentText) && medicines != null && medicines.size() > 0) {
                String lastGeneral = generals.get(generals.size() - 1).getName();
                int lastGeneralIndex = presentText.lastIndexOf(lastGeneral);
                for (Medicine medicine : medicines) {
                    /* 现病史中一般情况之后的药品名称,并且不包含不详 */
                    int medicine_index = presentText.indexOf(medicine.getName());
                    if (medicine_index > lastGeneralIndex && !medicine.getName().contains("不详")) {
                        drugFromPresent.add(medicine.getName().replaceAll("[“”药物晨晚]",""));
                    }
                }
            }
        }
        if (drugFromPresent.size() == 0) {
            status.set("0");
        } else {
            String infoStr = "";
            int matchSum = 0;
            for (String drug : drugFromPresent) {
                if (drugsCurrentlyInUse.contains(drug)) {
                    matchSum++;
                } else {
                    infoStr = concatInfo(infoStr,drug);
                }
            }
            info.set(infoStr);
            if (matchSum == drugFromPresent.size()) {
                status.set("0");
            }
        }
    }

    private String concatInfo(String infoStr, String drug) {
        if (StringUtil.isBlank(infoStr)) {
            infoStr += drug;
        } else {
            if (!infoStr.contains(drug)) {
                infoStr += "," + drug;
            }
        }
        return infoStr;
    }

    private List<String> getDrug(String drugsCurrentlyInUse) {
        List<String> drugs = new ArrayList<>();
        String medicine = "药物名称", usage = "用法", continueUse = "本次住院是否继续使用";
        while (drugsCurrentlyInUse.length() > 0) {
            if (drugsCurrentlyInUse.contains(medicine) && drugsCurrentlyInUse.contains(usage) && drugsCurrentlyInUse.contains(continueUse)) {
                int medicineIndex = drugsCurrentlyInUse.indexOf(medicine);
                int usageIndex = drugsCurrentlyInUse.indexOf(usage);
                int continueUseIndex = drugsCurrentlyInUse.indexOf(continueUse);
                String drug = drugsCurrentlyInUse.substring(medicineIndex + medicine.length() + 1, usageIndex);
                if (StringUtil.isNotBlank(drug)) {
                    drug = StringUtil.removeBlank(drug);
                    drugs.add(drug);
                }
                drugsCurrentlyInUse = drugsCurrentlyInUse.substring(continueUseIndex + continueUse.length() + 1);
            } else {
                break;
            }
        }
        return drugs;
    }

    private List<String> getDrugFromPresent(List<Medicine> medicines) {
        List<String> drugs = new ArrayList<>();
        String name;
        for (Medicine medicine : medicines) {
            name = medicine.getName();
            if (StringUtil.isNotBlank(name)) {
                drugs.add(name.replaceAll("[“”\"]", ""));
            }
        }
        return drugs;
    }
}