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.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 BEH0375 extends QCCatalogue {

    @Override
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getBeHospitalizedDoc() == null) {
            status.set("0");
            return;
        }
        //硬规则 匹配未婚
        String marry = inputInfo.getBeHospitalizedDoc().getStructureMap().get(Content.marry);
        MaritalLabel maritalLabel = inputInfo.getBeHospitalizedDoc().getMaritalLabel();
        if ("未婚".equals(marry) ||
                (maritalLabel != null &&
                (StringUtil.isBlank(maritalLabel.getText())
                        || maritalLabel.getText().contains("未婚")
                        || maritalLabel.getText().contains("离婚")
                        || maritalLabel.getText().contains("离异")
                        || maritalLabel.getText().contains("已故")
                        || maritalLabel.getText().contains("丧偶")
                        || maritalLabel.getText().contains("详见原病历")
                )
                )
        ) {
            status.set("0");
            return;
        }
        //台州市结构化的
        Map<String, String> structureMap_beh = inputInfo.getBeHospitalizedDoc().getStructureMap();
        if (StringUtils.isNotEmpty(structureMap_beh.get("配偶健康状况"))) {
            status.set("0");
            return;
        }
        List<Family> familyList = Lists.newArrayList();
        if (inputInfo.getBeHospitalizedDoc().getFamilyLabel() != null){
            List<Family> familiesFl = inputInfo.getBeHospitalizedDoc().getFamilyLabel().getFamilies();
            if (ListUtil.isNotEmpty(familiesFl)) {
                familyList.addAll(familiesFl);
            }
        }

        if (inputInfo.getBeHospitalizedDoc().getMaritalLabel() != null){
            List<Family> familiesMl = inputInfo.getBeHospitalizedDoc().getMaritalLabel().getFamily();
            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;
            }
        }
    }

}