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

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @ClassName : BEH03047
 * @Description : 初步诊断诊断依据不充分(暂只做高血压)
 * @Author : 胡敬
 * @Date: 2020-07-27 14:54
 */
@Component
public class BEH03047 extends QCCatalogue {

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getBeHospitalizedDoc() == null) {
            return;
        }
        DiagLabel initialDiagLabel = inputInfo.getBeHospitalizedDoc().getInitialDiagLabel();
        if (initialDiagLabel == null || StringUtil.isBlank(initialDiagLabel.getText())) {
            return;
        }

        List<Diag> initDiags = initialDiagLabel.getDiags();
        List<String> initDiagFilter = initDiags
                .stream()
                .map(Diag::getHospitalDiagName)
                .filter(x -> StringUtil.isNotBlank(x) && x.contains("高血压"))
                .collect(Collectors.toList());

        if (initDiagFilter.size() > 0) {
            String regex = "\\d+/\\d+(mmHg)?";
            List<Integer> highBlood = new ArrayList<>();
            List<Integer> lowBlood = new ArrayList<>();

            Map<String, String> beStructMap = inputInfo.getBeHospitalizedDoc().getStructureMap();
            //现病史取血压值
            String presentText = beStructMap.get("现病史");
            String bloodPressure = regularValue(presentText, regex);
            saveBloodValue(highBlood, lowBlood, bloodPressure);

            //生命体征取血压
            String bloodValue = beStructMap.get("血压");
            saveBloodValue(highBlood, lowBlood, bloodValue);

            //专科检查
            String special = beStructMap.get("专科体格检查");
            bloodPressure = regularValue(special, regex);
            saveBloodValue(highBlood, lowBlood, bloodPressure);

            highBlood.sort(Comparator.reverseOrder());
            lowBlood.sort(Comparator.reverseOrder());

            Integer high = highBlood.get(0);//收缩压
            Integer low = lowBlood.get(0);//舒张压

            String diag = initDiagFilter.get(0);//高血压病2级
            if (diag.contains("1")) {
                if (high < 140 && low < 90) {
                    status.set("-1");
                    info.set(diag + "：" + "血压未达到1级标准");
                }
            } else if (diag.contains("2")) {
                if (high < 160 && low < 100) {
                    status.set("-1");
                    info.set(diag + "：" + "血压未达到2级标准");
                }
            } else if (diag.contains("3")) {
                if (high < 180 && low < 110) {
                    status.set("-1");
                    info.set(diag + "：" + "血压未达到3级标准");
                }
            }

        }

    }

    /**
     * 从血压值中取收缩压和舒张压（170/97mmHg）
     *
     * @param highBlood
     * @param lowBlood
     * @param bloodPressure
     */
    private void saveBloodValue(List<Integer> highBlood, List<Integer> lowBlood, String bloodPressure) {
        if (StringUtil.isNotBlank(bloodPressure)) {
            if (bloodPressure.contains("mmHg")) {
                bloodPressure = bloodPressure.substring(0, bloodPressure.lastIndexOf("mmHg"));
            }
            String[] bloodValue = bloodPressure.split("/");
            if (bloodValue.length == 2) {
                highBlood.add(Integer.parseInt(bloodValue[0]));
                lowBlood.add(Integer.parseInt(bloodValue[1]));
            }
        }
    }

    private String regularValue(String input, String regex) {
        String value = "";
        if (StringUtil.isNotBlank(input)) {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(input);
            if (m.find()) {
                value = m.group(0);
            }
        }
        return value;
    }

}