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

import com.lantone.qc.kernel.catalogue.QCCatalogue;
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.entity.Allergy;
import com.lantone.qc.pub.model.label.PastLabel;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 过敏药物未标引号
 * @author: rengb
 * @time: 2020/3/10 14:02
 */
@Component
public class BEH0035 extends QCCatalogue {

    @Override
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if(inputInfo.getBeHospitalizedDoc() == null){
            return;
        }
        PastLabel pastLabel = inputInfo.getBeHospitalizedDoc().getPastLabel();
        List<Allergy> allergies = pastLabel.getAllergies();
        if (ListUtil.isEmpty(allergies)) {
            return;
        }

        List<String> wtAmNames = CatalogueUtil.noInQuotes(
                allergies.stream().filter(
                        i -> i != null && StringUtil.isNotBlank(i.getName())
                                && i.getNegative() == null
                                && i.getAllergyMedicine() != null
                                && StringUtil.isNotBlank(i.getAllergyMedicine().getName())
                                && !i.getAllergyMedicine().getName().equals("药物"))
                        .map(i -> i.getAllergyMedicine().getName()).distinct().collect(Collectors.toList()),
                pastLabel.getText()
        );
        if(wtAmNames.contains("止痛片")){
            return;
        }
        String pastText = pastLabel.getText();
        List<String> wtDiagList = new ArrayList<>();
        if (ListUtil.isNotEmpty(wtAmNames)) {
            for (String wtDiagName : wtAmNames) {
                int index = pastText.indexOf(wtDiagName);
                if (index - 1 > 0) {
                    String markText = pastText.substring(index - 1, index);
                    if (markText.contains("\"") || markText.contains("“")) {
                        continue;
                    }
                }
                if (index + wtDiagName.length() + 1 < pastText.length() && index > 0) {
                    String markText = pastText.substring(index + wtDiagName.length(), index + wtDiagName.length() + 1);
                    if (markText.contains("\"") || markText.contains("”")) {
                        continue;
                    }
                }
                wtDiagList.add(wtDiagName);
            }
        }

        if (ListUtil.isNotEmpty(wtDiagList)) {
            status.set("-1");
            for (String wtAmName : wtDiagList) {
                info.set(info.get() + wtAmName + " ");
            }
        }
    }

}