package com.rf.psychological.scale.resultBusiness.scaleResult;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.dto.scale.NewResultDto;
import com.rf.psychological.scale.scaleresult.ScaleConstant;

import java.text.DecimalFormat;
import java.util.*;

/**
 * @description 亲密关系经历量表
 */
public class ECRScale extends BaseScale {
    public ECRScale(JSONArray jsonArray, JSONObject resultJson) {
        super(jsonArray, resultJson);
    }

    public JSONObject scaleCalculate() throws Exception {

        // 自定义路径使用
        /*ScaleUtil.getScaleDll("ECRSCALE.dll");
        System.out.println("JSONArray:"+jsonArray);
        System.out.println("resultJson:"+resultJson);
        XScale xScale = new XScale(jsonArray,resultJson);
        String result = xScale.ECRScale(jsonArray.toString(), resultJson.toString());
        //System.out.println("dllresult:"+result);
        //解决排序问题
        LinkedHashMap<String, Object> jsonMap = JSON.parseObject(result,LinkedHashMap.class, Feature.OrderedField);
        JSONObject jsonObject = new JSONObject(true);
        jsonObject.putAll(jsonMap);
        System.out.println("jsonObject:"+jsonObject);
        return jsonObject;*/

        //依恋回避维度总分
        int score1 = 0;
        //依恋焦虑维度总分
        int score2 = 0;

        //依恋回避维度均分
        double scoreA = 0;
        //依恋焦虑维度均分
        double scoreB = 0;


        double M1 = 0;
        double M2 = 0;
        double M3 = 0;
        double M4 = 0;

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            if ((i + 1) == 1 || (i + 1) == 3 || (i + 1) == 5 || (i + 1) == 7 || (i + 1) == 9 || (i + 1) == 11 || (i + 1) == 13 || (i + 1) == 15 || (i + 1) == 17 || (i + 1) == 19 || (i + 1) == 21 || (i + 1) == 23 || (i + 1) == 25 || (i + 1) == 27 || (i + 1) == 29 || (i + 1) == 31 || (i + 1) == 33 || (i + 1) == 35) {
                if (jsonObject1.getString("scoreDirection").equals("1")) {
                    if (jsonObject1.getString("checkItems").equals("非常不同意")) {
                        score1 += 1;
                    } else if (jsonObject1.getString("checkItems").equals("不同意")) {
                        score1 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("有点不同意")) {
                        score1 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("中立")) {
                        score1 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("有点同意")) {
                        score1 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("同意")) {
                        score1 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("非常同意")) {
                        score1 += 7;
                    }
                } else {
                    if (jsonObject1.getString("checkItems").equals("非常不同意")) {
                        score1 += 7;
                    } else if (jsonObject1.getString("checkItems").equals("不同意")) {
                        score1 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("有点不同意")) {
                        score1 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("中立")) {
                        score1 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("有点同意")) {
                        score1 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("同意")) {
                        score1 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("非常同意")) {
                        score1 += 1;
                    }
                }
            } else {
                if (jsonObject1.getString("scoreDirection").equals("1")) {
                    if (jsonObject1.getString("checkItems").equals("非常不同意")) {
                        score2 += 1;
                    } else if (jsonObject1.getString("checkItems").equals("不同意")) {
                        score2 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("有点不同意")) {
                        score2 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("中立")) {
                        score2 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("有点同意")) {
                        score2 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("同意")) {
                        score2 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("非常同意")) {
                        score2 += 7;
                    }
                } else {
                    if (jsonObject1.getString("checkItems").equals("非常不同意")) {
                        score2 += 7;
                    } else if (jsonObject1.getString("checkItems").equals("不同意")) {
                        score2 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("有点不同意")) {
                        score2 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("中立")) {
                        score2 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("有点同意")) {
                        score2 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("同意")) {
                        score2 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("非常同意")) {
                        score2 += 1;
                    }
                }
            }
        }

        DecimalFormat df = new DecimalFormat("######0.00");

        scoreA = Double.parseDouble(df.format(score1 / 18.0));
        scoreB = Double.parseDouble(df.format(score2 / 18.0));

        M1 = scoreA * 3.2893296 + scoreB * 5.4725318 - 11.5307833;
        M2 = scoreA * 7.2371075 + scoreB * 8.1776448 - 32.3553266;
        M3 = scoreA * 3.9246754 + scoreB * 9.7102446 - 28.4573220;
        M4 = scoreA * 7.3654621 + scoreB * 4.9392039 - 22.2281088;


        //得分列表
        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("M1得分", String.valueOf(M1));
        resultMap.put("M2得分", String.valueOf(M2));
        resultMap.put("M3得分", String.valueOf(M3));
        resultMap.put("M4得分", String.valueOf(M4));

        resultJson.put("M1得分", String.valueOf(M1));
        resultJson.put("M2得分", String.valueOf(M2));
        resultJson.put("M3得分", String.valueOf(M3));
        resultJson.put("M4得分", String.valueOf(M4));


        if (M1 > M2 && M1 > M3 && M1 > M4) {
            resultMap.put("结论", "安全型");
            resultJson.put("结论", "安全型");
        } else if (M2 > M1 && M2 > M3 && M2 > M4) {
            resultMap.put("结论", "恐惧型");
            resultJson.put("结论", "恐惧型");
        } else if (M3 > M1 && M3 > M2 && M3 > M4) {
            resultMap.put("结论", "专注型");
            resultJson.put("结论", "专注型");
        } else if (M4 > M1 && M4 > M2 && M4 > M3) {
            resultMap.put("结论", "冷漠型");
            resultJson.put("结论", "冷漠型");
        }


        List<Map<String, String>> resultMapList = new ArrayList<>();
        resultMapList.add(resultMap);

        JSONObject returnJson = new JSONObject(true);
        returnJson.put("resultMapList", resultMapList);
        //returnJson.put("scaleName", Constant.SHEET_NAME_ECR);
        returnJson.put("resultJson", resultJson);

        return returnJson;
    }

    public JSONObject scaleCalculateV2() throws Exception {

        // 自定义路径使用
        /*ScaleUtil.getScaleDll("ECRSCALE.dll");
        System.out.println("JSONArray:"+jsonArray);
        System.out.println("resultJson:"+resultJson);
        XScale xScale = new XScale(jsonArray,resultJson);
        String result = xScale.ECRScale(jsonArray.toString(), resultJson.toString());
        //System.out.println("dllresult:"+result);
        //解决排序问题
        LinkedHashMap<String, Object> jsonMap = JSON.parseObject(result,LinkedHashMap.class, Feature.OrderedField);
        JSONObject jsonObject = new JSONObject(true);
        jsonObject.putAll(jsonMap);
        System.out.println("jsonObject:"+jsonObject);
        return jsonObject;*/

        //新版本数据格式
        Map<String, Object> newResult = new LinkedHashMap<>();

        //返回值以及存数据库格式
        List<NewResultDto> newResultDtos = new ArrayList<>();

        //总分
        double score = 0;

        //依恋回避维度总分
        int score1 = 0;
        //依恋焦虑维度总分
        int score2 = 0;

        //依恋回避维度均分
        double scoreA = 0;
        //依恋焦虑维度均分
        double scoreB = 0;


        double M1 = 0;
        double M2 = 0;
        double M3 = 0;
        double M4 = 0;
        Map<String,Double> dimensionScoreMap = new HashMap<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            if ((i + 1) == 1 || (i + 1) == 3 || (i + 1) == 5 || (i + 1) == 7 || (i + 1) == 9 || (i + 1) == 11 || (i + 1) == 13 || (i + 1) == 15 || (i + 1) == 17 || (i + 1) == 19 || (i + 1) == 21 || (i + 1) == 23 || (i + 1) == 25 || (i + 1) == 27 || (i + 1) == 29 || (i + 1) == 31 || (i + 1) == 33 || (i + 1) == 35) {
                if (jsonObject1.getString("scoreDirection").equals("1")) {
                    if (jsonObject1.getString("checkItems").equals("非常不同意")) {
                        score1 += 1;
                    } else if (jsonObject1.getString("checkItems").equals("不同意")) {
                        score1 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("有点不同意")) {
                        score1 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("中立")) {
                        score1 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("有点同意")) {
                        score1 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("同意")) {
                        score1 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("非常同意")) {
                        score1 += 7;
                    }
                } else {
                    if (jsonObject1.getString("checkItems").equals("非常不同意")) {
                        score1 += 7;
                    } else if (jsonObject1.getString("checkItems").equals("不同意")) {
                        score1 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("有点不同意")) {
                        score1 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("中立")) {
                        score1 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("有点同意")) {
                        score1 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("同意")) {
                        score1 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("非常同意")) {
                        score1 += 1;
                    }
                }
            } else {
                if (jsonObject1.getString("scoreDirection").equals("1")) {
                    if (jsonObject1.getString("checkItems").equals("非常不同意")) {
                        score2 += 1;
                    } else if (jsonObject1.getString("checkItems").equals("不同意")) {
                        score2 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("有点不同意")) {
                        score2 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("中立")) {
                        score2 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("有点同意")) {
                        score2 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("同意")) {
                        score2 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("非常同意")) {
                        score2 += 7;
                    }
                } else {
                    if (jsonObject1.getString("checkItems").equals("非常不同意")) {
                        score2 += 7;
                    } else if (jsonObject1.getString("checkItems").equals("不同意")) {
                        score2 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("有点不同意")) {
                        score2 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("中立")) {
                        score2 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("有点同意")) {
                        score2 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("同意")) {
                        score2 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("非常同意")) {
                        score2 += 1;
                    }
                }
            }
        }

        DecimalFormat df = new DecimalFormat("######0.00");

        scoreA = Double.parseDouble(df.format( score1/18));
        scoreB = Double.parseDouble(df.format(  score2/18));

        dimensionScoreMap.put("8af178857b94d581017b94dac286042c", scoreA);
        dimensionScoreMap.put("8af178857b94d581017b94dac2f6042d", scoreB);
        resultJson.put("dimensionScoreMap",dimensionScoreMap);
        //总分
        score = score1 + score2;

        Double M11 = scoreA * 3.2893296 + scoreB * 5.4725318 - 11.5307833;
        Double M22 = scoreA * 7.2371075 + scoreB * 8.1776448 - 32.3553266;
        Double M33 = scoreA * 3.9246754 + scoreB * 9.7102446 - 28.4573220;
        Double M44 = scoreA * 7.3654621 + scoreB * 4.9392039 - 22.2281088;

        M1 = Double.parseDouble(df.format(M11));
        M2 = Double.parseDouble(df.format(M22));
        M3 = Double.parseDouble(df.format(M33));
        M4 = Double.parseDouble(df.format(M44));
        //得分列表
        Map<String, String> resultMap = new LinkedHashMap<>();
//        resultMap.put("M1得分", String.valueOf(M1));
//        resultMap.put("M2得分", String.valueOf(M2));
//        resultMap.put("M3得分", String.valueOf(M3));
//        resultMap.put("M4得分", String.valueOf(M4));
//
//        resultJson.put("M1得分", String.valueOf(M1));
//        resultJson.put("M2得分", String.valueOf(M2));
//        resultJson.put("M3得分", String.valueOf(M3));
//        resultJson.put("M4得分", String.valueOf(M4));

        newResultDtos.add(new NewResultDto("总分", String.valueOf(new DecimalFormat("####0.00").format(score)), "", "", "无",
                "无", "无", "无", "否"));

        if (M1 > M2 && M1 > M3 && M1 > M4) {
            resultMap.put("结论", "安全型");
            resultJson.put("结论", "安全型");
            newResultDtos.add(new NewResultDto("安全型", String.valueOf(M1), "安全型关系", "安全型的亲密关系经历。在这一段关系当中亲密关系友好健康。", "无",
                    "无", "无", "无", "否"));
        } else if (M2 > M1 && M2 > M3 && M2 > M4) {
            resultMap.put("结论", "恐惧型");
            resultJson.put("结论", "恐惧型");
            newResultDtos.add(new NewResultDto("恐惧型", String.valueOf(M2), "恐惧型关系", "属于恐惧型的关系，这种亲密关系当中有一方处于比较强势的地位，使另一方产生比较恐惧的心理。", "无",
                    "无", "无", "无", "否"));
        } else if (M3 > M1 && M3 > M2 && M3 > M4) {
            resultMap.put("结论", "专注型");
            resultJson.put("结论", "专注型");
            newResultDtos.add(new NewResultDto("专注型", String.valueOf(M3), "专注型关系", "这一段经历关系的经历当中，处理问题比较专注，将注意力和精力集中到这段关系的维持当中，比较享受或者是在意这一段的亲密关系。", "无",
                    "无", "无", "无", "否"));
        } else if (M4 > M1 && M4 > M2 && M4 > M3) {
            resultMap.put("结论", "冷漠型");
            resultJson.put("结论", "冷漠型");
            newResultDtos.add(new NewResultDto("冷漠型", String.valueOf(M4), "冷漠型关系", "这段亲密的关系属于比较冷漠的类型，不太注意对方的感受和心理体验不注重对这段关系的维持，始终处于自我内心世界，把自己放在这段关系的中心位置。", "无",
                    "无", "无", "无", "否"));
        }
        resultMap.put("依恋回避维度均分", String.valueOf(scoreA));
        resultJson.put("依恋回避维度均分", String.valueOf(scoreA));
        newResultDtos.add(new NewResultDto("依恋回避维度均分", String.valueOf(new DecimalFormat("####0.00").format(scoreA)), "无", "无", "无",
                "无", "无", "无", "否"));
        resultMap.put("依恋焦虑维度均分", String.valueOf(scoreB));
        resultJson.put("依恋焦虑维度均分", String.valueOf(scoreB));
        newResultDtos.add(new NewResultDto("依恋焦虑维度均分", String.valueOf(new DecimalFormat("####0.00").format(scoreB)), "无", "无", "无",
                "无", "无", "无", "否"));
        List<Map<String, String>> resultMapList = new ArrayList<>();
        resultMapList.add(resultMap);

        //需要图表展示的
        newResult.put("iconInfo", "");
        newResult.put("result", newResultDtos);

        JSONObject returnJson = new JSONObject(true);
        returnJson.put("resultMapList", resultMapList);
        returnJson.put("resultJson", resultJson);
        returnJson.put(ScaleConstant.ResultEnum.RESULT_NEW_FIELD.getKeyword(), newResult);

        return returnJson;
    }
}
