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

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.Address;
import com.lantone.qc.pub.model.label.PersonalLabel;
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;

/**
 * @Description: 居住地未描述
 * @author: rengb
 * @time: 2020/3/10 14:02
 */
@Component
public class BEH0043 extends QCCatalogue {

    @Override
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getBeHospitalizedDoc() == null) {
            status.set("0");
            return;
        }
        //台州结构化
        Map<String, String> structureMap = inputInfo.getBeHospitalizedDoc().getStructureMap();
        if (StringUtils.isNotEmpty(structureMap.get("联系地址")) || StringUtils.isNotEmpty(structureMap.get("现住址"))) {
            status.set("0");
            return;
        }

        PersonalLabel personalLabel = inputInfo.getBeHospitalizedDoc().getPersonalLabel();
        if (personalLabel == null) {
            status.set("0");
            return;
        }
        String text = personalLabel.getText();
        if (StringUtil.isBlank(text) || text.contains("居住") || text.contains("生长") || text.contains("生活")
                || text.contains("详见原病历")) {
            status.set("0");
            return;
        }
        List<Address> addresses = personalLabel.getAddresses();
        if (ListUtil.isNotEmpty(addresses)) {
            long count = addresses.stream().filter(i -> {
                boolean flag = false;
                if (i != null && StringUtil.isNotBlank(i.getName())) {
                    if (i.getName().indexOf("居住") > -1) {
                        flag = true;
                    } else {
                        Pattern pattern = Pattern.compile("[\\s\\S]*(居住|生长)[\\s\\S]{0,5}" + i.getName() + "[\\s\\S]*");
                        flag = pattern.matcher(text).matches();
                    }
                }
                return flag;
            }).count();
            if (count > 0) {
                status.set("0");
            }
        }
    }

}