package org.diagbot.graphWeb.work;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 量表得分计算结果
 * @Author: HUJING
 * @Date: 2019/4/9 14:33
 */
public class ScaleCalculate {
    public Map<String, Object> scaleScoreCalc(JSONObject jsonObject) {
        Map<String, Object> scaleCalcResult = new HashMap<>();
        Map<String, Object> groupResults = null;
        List<Map<String, Object>> groupLists = new ArrayList<>();
        double sum = 0;
        Map<String, Object> data = (Map<String, Object>) jsonObject.get("data");
        String scaleName = data.get("scaleName").toString();
        int scaleType = Integer.valueOf(data.get("scaleType").toString());
        if (scaleType == 1) {
            List<Map<String, Object>> groupList = (List<Map<String, Object>>) data.get("group");
            for (Map<String, Object> group : groupList) {
                double groupSum = 0;
                groupResults = new HashMap<>();
                String groupName = group.get("groupName").toString();
                List<Map<String, Object>> rowsList = (List<Map<String, Object>>) group.get("rows");
                for (Map<String, Object> rows : rowsList) {
                    List<Map<String, Object>> rowList = (List<Map<String, Object>>) rows.get("row");
                    //多条选第一条
                    Map<String, Object> firstRow = null;
                    if (rowList.size() > 1) {
                        int i = 0;
                        JSONArray selectList = new JSONArray();
                        for (Map<String, Object> row : rowList) {
                            List<Map<String, Object>> detailsList = (List<Map<String, Object>>) row.get("details");
                            for (Map<String, Object> details : detailsList) {
                                if (details.get("select") != null && Integer.valueOf(details.get("select").toString()) == 1) {
                                    i += 1;
                                    if (!selectList.contains(row)) {
                                        selectList.add(row);
                                    }
                                }
                            }
                        }
                        if (i > 1) {
                            firstRow = (Map<String, Object>) selectList.get(0);
                            int coefficient = Integer.valueOf(firstRow.get("coefficient").toString());
                            int constant = Integer.valueOf(firstRow.get("constant").toString());
                            List<Map<String, Object>> detailsList = (List<Map<String, Object>>) firstRow.get("details");
                            for (Map<String, Object> details : detailsList) {
                                if (details.get("select") != null && Integer.valueOf(details.get("select").toString()) == 1) {
                                    Double score = Double.valueOf(details.get("score").toString());
                                    //分值*系数+常数
                                    score = score * coefficient + constant;
                                    groupSum += score;
                                    sum += score;
                                }
                            }
                        } else {
                            for (Map<String, Object> row : rowList) {
                                int coefficient = Integer.valueOf(row.get("coefficient").toString());
                                int constant = Integer.valueOf(row.get("constant").toString());
                                List<Map<String, Object>> detailsList = (List<Map<String, Object>>) row.get("details");
                                for (Map<String, Object> details : detailsList) {
                                    if (details.get("select") != null && Integer.valueOf(details.get("select").toString()) == 1) {
                                        Double score = Double.valueOf(details.get("score").toString());
                                        //分值*系数+常数
                                        score = score * coefficient + constant;
                                        groupSum += score;
                                        sum += score;
                                    }
                                }
                            }
                        }
                    } else {
                        firstRow = rowList.get(0);
                        int coefficient = Integer.valueOf(firstRow.get("coefficient").toString());
                        int constant = Integer.valueOf(firstRow.get("constant").toString());
                        List<Map<String, Object>> detailsList = (List<Map<String, Object>>) firstRow.get("details");
                        for (Map<String, Object> details : detailsList) {
                            if (details.get("select") != null && Integer.valueOf(details.get("select").toString()) == 1) {
                                Double score = Double.valueOf(details.get("score").toString());
                                //分值*系数+常数
                                score = score * coefficient + constant;
                                groupSum += score;
                                sum += score;
                            }
                        }

                    }
                }
                //            String text = scaleResultText(groupSum);
                JSONObject groupCalculate = new JSONObject();
                JSONObject groupResult = new JSONObject();
                groupResult.put("text", "");
                groupResult.put("value", groupSum);

                groupCalculate.put("result", groupResult);
                groupCalculate.put("isShow", 1);

                groupResults.put("groupName", groupName);
                groupResults.put("groupCalculate", groupCalculate);

                groupLists.add(groupResults);
            }
            String text = "";
            Map<String, Object> calculate = (Map<String, Object>) data.get("calculate");
            List<Map<String, Object>> rangeList = (List<Map<String, Object>>) calculate.get("range");
            for (Map<String, Object> range : rangeList) {
                Double max = Double.valueOf(range.get("max").toString());
                Double min = Double.valueOf(range.get("min").toString());
                if (sum >= min && sum <= max) {
                    text = range.get("text").toString();
                    break;
                }
            }

            scaleCalcResult.put("scaleName", scaleName);
            scaleCalcResult.put("group", groupLists);

            JSONObject calcalculate = new JSONObject();
            JSONObject result = new JSONObject();
            result.put("text", text);
            //TODO 根据数字类型给出结果
            result.put("value", sum);
            calcalculate.put("result", result);
            scaleCalcResult.put("calcalculate", calcalculate);
        } else {
            scaleCalcResult.put("errMsg", "量表类型错误。");
        }
        return scaleCalcResult;
    }

    private String scaleResultText(int sum) {
        String text = null;
        if (sum >= 0 && sum <= 6) {
            text = "无";
        } else if (sum >= 7 && sum <= 12) {
            text = "轻度";
        } else if (sum >= 13 && sum <= 29) {
            text = "中度";
        } else if (sum >= 30 && sum <= 46) {
            text = "重度";
        }
        return text;
    }

    public Map<String, Object> scaleCalcMethod(JSONObject jsonObject) throws Exception {
        Map<String, Object> scaleCalcResult = new HashMap<>();
        Map<String, Object> data = (Map<String, Object>) jsonObject.get("data");
        int type = (int) data.get("type");
        if (type == 2) {
            Map<String, Object> content = (Map<String, Object>) data.get("content");
            List<Map<String, Object>> mapList = (List<Map<String, Object>>) content.get("details");
            if ("肾小球滤过率".equals(content.get("name"))) {
                int age = 0;
                double scr = 0.00;
                float k = 0.0f;
                double a = 0.00;
                double denger = 0.00;
                for (int i = 0; i < mapList.size(); i++) {
                    Map<String, Object> detailSub = mapList.get(i);
                    if ("年龄".equals(detailSub.get("name"))) {
                        if ("".equals(detailSub.get("value"))) {
                            //如果拿到的年龄为空，
                            break;
                        } else {
                            age = Integer.valueOf(detailSub.get("value").toString());
                        }
                    } else if ("血肌酐".equals(detailSub.get("name"))) {
                        if ("".equals(detailSub.get("value"))) {
                            //如果给的value是空，给的2.2621是假数据
                            break;
                        } else {
                            if ("umol/L".equals(detailSub.get("uint"))) {
                                scr = Double.valueOf(detailSub.get("value").toString()) / 88.41;
                            } else if ("mg/dL".equals(detailSub.get("uint"))) {
                                scr = Double.valueOf(detailSub.get("value").toString());
                            }
                        }
                    } else if ("性别".equals(detailSub.get("name"))) {
                        List<Map<String, Object>> genderDetails = (List<Map<String, Object>>) detailSub.get("details");
                        for (int j = 0; j < genderDetails.size(); j++) {
                            Map<String, Object> genderDetail = genderDetails.get(j);
                            //返回的数据结构性别暂时是写死（默认女性）
                            if ((int) genderDetail.get("state") == 1) {
                                if ("男".equals(genderDetail.get("detailName"))) {
                                    k = 0.9f;
                                    denger = Double.valueOf(genderDetail.get("value").toString());
                                    if (scr <= 0.90) {
                                        a = -0.411;
                                    } else {
                                        a = -1.209;
                                    }
                                } else if ("女".equals(genderDetail.get("detailName"))) {
                                    k = 0.7f;
                                    denger = Double.valueOf(genderDetail.get("value").toString());
                                    if (scr <= 0.70) {
                                        a = -0.329;
                                    } else {
                                        a = -1.209;
                                    }
                                }
                            }
                        }
                    }
                }
                double eGFR3 = 141 * Math.pow((scr / k), a) * Math.pow(0.993, age) * denger;
                String unit = "ml/min•1.73m2";
                String text = null;
                if (eGFR3 <= 0) {
                    text = "指标值缺少";
                } else if (eGFR3 > 0 && eGFR3 < 15) {
                    text = "肾功能衰竭";
                } else if (eGFR3 >= 15 && eGFR3 <= 29) {
                    text = "重度下降";
                } else if (eGFR3 > 29 && eGFR3 < 60) {
                    text = "中度下降";
                } else if (eGFR3 >= 60 && eGFR3 <= 89) {
                    text = "轻度下降";
                } else if (eGFR3 > 89) {
                    text = "正常或肾损伤代偿期";
                }
                JSONObject valueResult = new JSONObject();
                JSONObject nameResult = new JSONObject();
                valueResult.put("name", "GFR值");
                valueResult.put("text", new DecimalFormat("0.00").format(eGFR3) + " " + unit);
                nameResult.put("name", "评估结论");
                nameResult.put("text", text);
                //                scaleCalcResult.put("text", text);
                //                scaleCalcResult.put("unit", unit);
                //                scaleCalcResult.put("value", new DecimalFormat("#.00").format(eGFR3));
                JSONArray result = new JSONArray();
                result.add(valueResult);
                result.add(nameResult);
                scaleCalcResult.put("result", result);
                System.out.println("text:" + text + "\tunit:" + unit + "\tvalue:" + eGFR3);
            }
        }
        return scaleCalcResult;
    }
}
