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.MaritalStatus;
import com.lantone.qc.pub.model.label.MaritalLabel;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

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

/**
 * @Description: 婚姻状况前后不一致
 * @author: rengb
 * @time: 2020/3/10 14:02
 */
@Component
public class BEH0372 extends QCCatalogue {

    @Override
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getBeHospitalizedDoc() == null) {
            status.set("0");
            return;
        }

        String basicInfoStatus = inputInfo.getBeHospitalizedDoc().getStructureMap().get("婚姻");
        String bhbasicInfoStatus = inputInfo.getBeHospitalizedDoc().getStructureMap().get("婚姻状况");
        if (StringUtil.isBlank(basicInfoStatus) || StringUtil.isBlank(bhbasicInfoStatus)) {
            status.set("0");
            return;
        }

        if (basicInfoStatus.equals(bhbasicInfoStatus)) {
            status.set("0");
            return;
        }
        String maritalStatus = "";
        MaritalStatus mts = inputInfo.getBeHospitalizedDoc().getMaritalLabel().getMaritalStatus();
        if (mts != null && StringUtil.isNotBlank(mts.getName())) {
            maritalStatus = mts.getName();
        }

        if (chcekMaritalStatus(basicInfoStatus, maritalStatus)) {
            status.set("0");
            return;
        }

        //无婚育史
        MaritalLabel maritalLabel = inputInfo.getBeHospitalizedDoc().getMaritalLabel();
        if (maritalLabel == null || maritalLabel.getText() == null) {
            status.set("0");
        }
    }

    private boolean chcekMaritalStatus(String basicInfoStatus, String maritalStatus) {
        if (basicInfoStatus.equals(maritalStatus)) {
            return true;
        }
        boolean flag = false;
        List<String> regxs = Lists.newArrayList(
                "([\\s\\S]*离[\\s\\S]*婚[\\s\\S]*)|([\\s\\S]*离[\\s\\S]*异[\\s\\S]*)",
                "([\\s\\S]*已[\\s\\S]*婚[\\s\\S]*)|([\\s\\S]*结[\\s\\S]*婚[\\s\\S]*)|([\\s\\S]*丧[\\s\\S]*偶[\\s\\S]*)",
                "[\\s\\S]*未[\\s\\S]*婚[\\s\\S]*",
                "[\\s\\S]*丧[\\s\\S]*偶[\\s\\S]*",
                "[\\s\\S]*其[\\s\\S]*他[\\s\\S]*"
        );
        Pattern pt = null;
        for (String regx : regxs) {
            pt = Pattern.compile(regx);
            if (pt.matcher(basicInfoStatus).find() && pt.matcher(maritalStatus).find()) {
                flag = true;
                break;
            }
        }
        return flag;
    }

}