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

import com.google.common.collect.Lists;
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.doc.BeHospitalizedDoc;
import com.lantone.qc.pub.model.entity.Family;
import com.lantone.qc.pub.model.label.FamilyLabel;
import com.lantone.qc.pub.model.label.MaritalLabel;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @Description: 父母健康状况未描述
 * @author: rengb
 * @time: 2020/3/10 14:02
 */
@Component
public class BEH0397 extends QCCatalogue {

    @Override
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getBeHospitalizedDoc() == null) {
            status.set("0");
            return;
        }
        BeHospitalizedDoc beHospitalizedDoc = inputInfo.getBeHospitalizedDoc();
        Map<String, String> structureMap = beHospitalizedDoc.getStructureMap();
        if (StringUtils.isNotEmpty(structureMap.get("家庭成员健康情况"))) {
            status.set("0");
            return;
        }
        FamilyLabel familyLabel = beHospitalizedDoc.getFamilyLabel();
        MaritalLabel maritalLabel = beHospitalizedDoc.getMaritalLabel();

        String familyText = "", maritalText = "";
        List<Family> familiesFl = null;
        List<Family> familiesMl = null;
        if (familyLabel != null) {
            familyText = familyLabel.getText();
            familiesFl = familyLabel.getFamilies();
        }
        if (maritalLabel != null) {
            maritalText = maritalLabel.getText();
            familiesMl = maritalLabel.getFamily();
        }
        if (StringUtil.isNotBlank(familyText)){
            familyText = StringUtil.removeBlank(familyText);
        }
        if ((StringUtil.isNotBlank(familyText) && (familyText.contains("父母") || familyText.contains("详见原病历")))
                || (StringUtil.isNotBlank(maritalText) && (maritalText.contains("父母") || maritalText.contains("详见原病历")))) {
            status.set("0");
            return;
        }
        List<Family> familyList = Lists.newArrayList();
        if (familiesFl != null && familiesFl.size() > 0) {
            familyList.addAll(familiesFl);
        }
        if (familiesMl != null && familiesMl.size() > 0) {
            familyList.addAll(familiesMl);
        }

        Pattern p = Pattern.compile("[父母爸妈]");
        List<Family> filterFamilies = familyList
                .stream()
                .filter(
                        i -> i != null
                                && StringUtil.isNotBlank(i.getName())
                                && p.matcher(i.getName()).find()).collect(Collectors.toList());
        for (Family family : filterFamilies) {
            if (family.getDead() != null) {
                status.set("0");
                return;
            } else if (family.getHealthCondition() != null && StringUtil.isNotBlank(family.getHealthCondition().getName())) {
                status.set("0");
                return;
            } else if (family.getDiags() != null && family.getDiags().size() > 0) {
                status.set("0");
                return;
            }
        }
    }

}