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.springframework.stereotype.Component;

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

/**
 * @Description: 子女数量情况未描述
 * @author: rengb
 * @time: 2020/3/10 14:02
 */
@Component
public class BEH0058 extends QCCatalogue {

    @Override
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getBeHospitalizedDoc() == null) {
            status.set("0");
            return;
        }
        BeHospitalizedDoc beHospitalizedDoc = inputInfo.getBeHospitalizedDoc();
        //硬规则 匹配未育
        String marry = beHospitalizedDoc.getStructureMap().get(Content.marry);
        MaritalLabel maritalLabel = beHospitalizedDoc.getMaritalLabel();
        if (maritalLabel == null) {
            status.set("0");
            return;
        }
        String maritalText = maritalLabel.getText();
        if (StringUtil.isBlank(maritalText)) {
            status.set("0");
            return;
        } else {
            String regex = ".*\\d?子\\d?女.*";
            boolean flag = maritalText.matches(regex);
            regex = ".*\\d+-\\d+-\\d+-0.*";
            boolean matches = maritalText.matches(regex);
            if (flag || matches) {
                status.set("0");
                return;
            }
        }
        //硬规则 匹配未育
        if ("未婚".equals(marry)) {
            status.set("0");
            return;
        }
        if (StringUtil.isNotBlank(maritalText)) {
            List<String> words = Lists.newArrayList("未婚", "未育", "未婚育", "未生育", "未生", "0子0女"
                    , "0-0-0-0", "详见原病历");
            for (String word : words) {
                if (maritalText.contains(word)) {
                    status.set("0");
                    return;
                }
            }
        }
        Map<String, String> structureMap = beHospitalizedDoc.getStructureMap();
        String familyMembersHealth = structureMap.get("家庭成员健康情况");
        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("[儿子女]");
        if (StringUtil.isNotBlank(familyMembersHealth)) {
            if (p.matcher(familyMembersHealth).find()) {
                status.set("0");
                return;
            }
        }
        long count = familyList
                .stream()
                .filter(
                        i -> i != null
                                && StringUtil.isNotBlank(i.getName())
                                && StringUtil.isContainNumber(i.getName())
                                && p.matcher(i.getName()).find()
                )
                .count();
        if (count > 0) {
            status.set("0");
        }
    }

}