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.entity.Family;
import com.lantone.qc.pub.model.label.FamilyLabel;
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 BEH0398 extends QCCatalogue {

    @Override
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getBeHospitalizedDoc() == null) {
            status.set("0");
            return;
        }
        //硬规则 匹配独生子 独生女
        FamilyLabel familyLabel = inputInfo.getBeHospitalizedDoc().getFamilyLabel();
        if (familyLabel != null) {
            String familyLabelText = familyLabel.getText();
            if (StringUtil.isNotBlank(familyLabelText)) {
                if (familyLabelText.contains("独生子") || familyLabelText.contains("独生女")
                        || familyLabelText.contains("独子") || familyLabelText.contains("独女")) {
                    status.set("0");
                    return;
                }
            }
        }
        Map<String, String> structureMap = inputInfo.getBeHospitalizedDoc().getStructureMap();
        if (StringUtils.isNotEmpty(structureMap.get("家庭成员健康情况"))) {
            status.set("0");
            return;
        }
        if (StringUtils.isNotEmpty("兄弟姐妹健康状况")) {
            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());

        if (ListUtil.isEmpty(filterFamilies)) {
            status.set("0");
            return;
        }
        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;
            }
        }
    }

}