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.Content;
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.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 BEH0376 extends QCCatalogue {

    @Override
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getBeHospitalizedDoc() == null) {
            status.set("0");
            return;
        }
        BeHospitalizedDoc beHospitalizedDoc = inputInfo.getBeHospitalizedDoc();
        MaritalLabel maritalLabel = beHospitalizedDoc.getMaritalLabel();
        //台州市结构化的
        Map<String, String> structureMap_beh = beHospitalizedDoc.getStructureMap();
        if (StringUtils.isNotEmpty(structureMap_beh.get("子女健康状况"))) {
            status.set("0");
            return;
        }
        if (maritalLabel == null || StringUtils.isEmpty(maritalLabel.getText())) {
            status.set("0");
            return;
        }
        //硬规则 匹配未育
        String marry = structureMap_beh.get(Content.marry);
        if ("未婚".equals(marry)) {
            status.set("0");
            return;
        }
        String maritalText = maritalLabel.getText();
        if (StringUtil.isNotBlank(maritalText)) {
            List<String> words = Lists.newArrayList("未婚", "未育", "未婚育", "未生育", "未生", "0子0女", "详见原病历");
            for (String word : words) {
                if (maritalText.contains(word)) {
                    status.set("0");
                    return;
                }
            }
            String regex = ".*\\d+-\\d+-\\d+-0.*";
            if (maritalText.matches(regex)){
                status.set("0");
                return;
            }
        }
        //未婚 无子女
        if (maritalLabel.getMaritalStatus() != null && maritalLabel.getMaritalStatus().getName().contains("未婚")) {
            status.set("0");
            return;
        }
        List<Family> familyList = Lists.newArrayList();
        List<Family> familiesFl = inputInfo.getBeHospitalizedDoc().getFamilyLabel().getFamilies();
        List<Family> familiesMl = inputInfo.getBeHospitalizedDoc().getMaritalLabel().getFamily();
        if (ListUtil.isNotEmpty(familiesFl)) {
            familyList.addAll(familiesFl);
        }
        if (ListUtil.isNotEmpty(familiesMl)) {
            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;
            }
        }
    }

}