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.dao.model.AnswerEntity;
import com.rf.psychological.dao.model.DimensionEntity;
import com.rf.psychological.scale.dao.model.DimensionOrgEntity;
import com.rf.psychological.scale.dao.model.ScaleMarksEntity;
import com.rf.psychological.scale.dao.model.WarnEntity;
import com.rf.psychological.scale.service.DimensionOrgService;
import com.rf.psychological.utils.Constant;

import java.util.*;
import java.util.stream.Collectors;

import static com.rf.psychological.scale.resultBusiness.scaleResult.SCSQScale.createWarnEntry;

/**
 * @description 艾森克人格量表
 */
public class EPQScale extends BaseScale {
    public EPQScale(JSONArray jsonArray, JSONObject resultJson) {
        super(jsonArray, resultJson);
    }

    public JSONObject scaleCalculate() throws Exception {
        //分表E总分
        int scoreE = 0;
        //分表N总分
        int scoreN = 0;
        //分表P总分
        int scoreP = 0;
        //分表L总分
        int scoreL = 0;

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            if (i == 0 || i == 4 || i == 9 || i == 12 || i == 13 || i == 16 || i == 24 || i == 32 || i == 36 || i == 40 || i == 48 || i == 52 || i == 54 || i == 60 || i == 64 || i == 70 || i == 79 || i == 83 || i == 20 || i == 28 || i == 44) {
                if (jsonObject1.getString("scoreDirection").equals("1")) {
                    if (jsonObject1.getString("checkItems").equals("是")) {
                        scoreE += 1;
                    } else if (jsonObject1.getString("checkItems").equals("否")) {
                        scoreE += 0;
                    }
                } else if (jsonObject1.getString("scoreDirection").equals("0")) {
                    if (jsonObject1.getString("checkItems").equals("是")) {
                        scoreE += 0;
                    } else if (jsonObject1.getString("checkItems").equals("否")) {
                        scoreE += 1;
                    }
                }
            } else if (i == 2 || i == 6 || i == 11 || i == 14 || i == 18 || i == 22 || i == 26 || i == 30 || i == 34 || i == 38 || i == 42 || i == 46 || i == 50 || i == 56 || i == 58 || i == 62 || i == 66 || i == 68 || i == 72 || i == 73 || i == 76 || i == 77 || i == 81 || i == 85) {
                if (jsonObject1.getString("scoreDirection").equals("1")) {
                    if (jsonObject1.getString("checkItems").equals("是")) {
                        scoreN += 1;
                    } else if (jsonObject1.getString("checkItems").equals("否")) {
                        scoreN += 0;
                    }
                } else if (jsonObject1.getString("scoreDirection").equals("0")) {
                    if (jsonObject1.getString("checkItems").equals("是")) {
                        scoreN += 0;
                    } else if (jsonObject1.getString("checkItems").equals("否")) {
                        scoreN += 1;
                    }
                }
            } else if (i == 21 || i == 25 || i == 29 || i == 33 || i == 45 || i == 49 || i == 65 || i == 67 || i == 74 || i == 75 || i == 80 || i == 84 || i == 1 || i == 5 || i == 8 || i == 10 || i == 17 || i == 37 || i == 41 || i == 55 || i == 61 || i == 71 || i == 87) {
                if (jsonObject1.getString("scoreDirection").equals("1")) {
                    if (jsonObject1.getString("checkItems").equals("是")) {
                        scoreP += 1;
                    } else if (jsonObject1.getString("checkItems").equals("否")) {
                        scoreP += 0;
                    }
                } else if (jsonObject1.getString("scoreDirection").equals("0")) {
                    if (jsonObject1.getString("checkItems").equals("是")) {
                        scoreP += 0;
                    } else if (jsonObject1.getString("checkItems").equals("否")) {
                        scoreP += 1;
                    }
                }
            } else {
                if (jsonObject1.getString("scoreDirection").equals("1")) {
                    if (jsonObject1.getString("checkItems").equals("是")) {
                        scoreL += 1;
                    } else if (jsonObject1.getString("checkItems").equals("否")) {
                        scoreL += 0;
                    }
                } else if (jsonObject1.getString("scoreDirection").equals("0")) {
                    if (jsonObject1.getString("checkItems").equals("是")) {
                        scoreL += 0;
                    } else if (jsonObject1.getString("checkItems").equals("否")) {
                        scoreL += 1;
                    }
                }
            }
        }
        //新版本数据格式
        Map<String, Object> newResult = new LinkedHashMap<>();
        JSONObject iconInfo = new JSONObject();
        //雷达图需要的维度以及最大值
        List<Map<String, Object>> indicator = new LinkedList<>();
        //雷达图需要的分数
        LinkedList<String> scoreList = new LinkedList<>();
        //返回值以及存数据库格式
        List<NewResultDto> newResultDtos = new ArrayList<>();

        //得分列表
        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("E总分", String.valueOf(scoreE));
        resultJson.put("E总分", String.valueOf(scoreE));
        resultMap.put("N总分", String.valueOf(scoreN));
        resultJson.put("N总分", String.valueOf(scoreN));
        resultMap.put("P总分", String.valueOf(scoreP));
        resultJson.put("P总分", String.valueOf(scoreP));
        resultMap.put("L总分", String.valueOf(scoreL));
        resultJson.put("L总分", String.valueOf(scoreL));

        Map<String,Double> dimensionScoreMap = new HashMap<>();
        dimensionScoreMap.put("8af178857b94d581017b94e06dce0852", (double) scoreE);
        dimensionScoreMap.put("8af178857b94d581017b94e06e3e0853", (double) scoreN);
        dimensionScoreMap.put("8af178857b94d581017b94e06eac0854", (double) scoreP);
        dimensionScoreMap.put("8af178857b94d581017b94e06f1c0855", (double) scoreL);

        //雷达图数据
        Map<String, Object> indicatorMap = new HashMap<>();
        if (scoreE >= 16) {
            resultMap.put("E结论", "人格外向，可能是好交际，渴望刺激和冒险，情感易于冲动。");
            resultJson.put("E结论", "人格外向，可能是好交际，渴望刺激和冒险，情感易于冲动。");
            newResultDtos.add(new NewResultDto("E分量", String.valueOf(scoreE), "无", "人格外向，可能是好交际，渴望刺激和冒险，情感易于冲动。", "无",
                    "无", "内外向", "无", "否"));
            indicatorMap.put("text", "E分量");
            indicatorMap.put("max", "21");
            indicator.add(indicatorMap);
        } else if (scoreE >= 8) {
            resultMap.put("E结论", "中间型");
            resultJson.put("E结论", "中间型");
            newResultDtos.add(new NewResultDto("E分量", String.valueOf(scoreE), "无", "中间型", "无",
                    "无", "内外向", "无", "否"));
            indicatorMap.put("text", "E分量");
            indicatorMap.put("max", "15");
            indicator.add(indicatorMap);
        } else if (scoreE >= 0) {
            resultMap.put("E结论", "人格内向，如好静，富于内省，不喜欢刺激，喜欢有秩序的生活方式，情绪比较稳定。");
            resultJson.put("E结论", "人格内向，如好静，富于内省，不喜欢刺激，喜欢有秩序的生活方式，情绪比较稳定。");
            newResultDtos.add(new NewResultDto("E分量", String.valueOf(scoreE), "无", "人格内向，如好静，富于内省，不喜欢刺激，喜欢有秩序的生活方式，情绪比较稳定。", "无",
                    "无", "内外向", "无", "否"));
            indicatorMap.put("text", "E分量");
            indicatorMap.put("max", "7");
            indicator.add(indicatorMap);
        }

        Map<String, Object> indicatorMap1 = new HashMap<>();
        if (scoreN >= 15) {
            resultMap.put("N结论", "焦虑、忧心仲仲、常郁郁不乐，有强烈情绪反应，甚至出现不理智行为。");
            resultJson.put("N结论", "焦虑、忧心仲仲、常郁郁不乐，有强烈情绪反应，甚至出现不理智行为。");
            newResultDtos.add(new NewResultDto("N分量", String.valueOf(scoreN), "无", "焦虑、忧心仲仲、常郁郁不乐，有强烈情绪反应，甚至出现不理智行为。", "无",
                    "无", "情绪稳定性", "无", "否"));
            indicatorMap1.put("text", "N分量");
            indicatorMap1.put("max", "24");
            indicator.add(indicatorMap1);
        } else if (scoreN >= 9) {
            resultMap.put("N结论", "中间型");
            resultJson.put("N结论", "中间型");
            newResultDtos.add(new NewResultDto("N分量", String.valueOf(scoreN), "无", "中间型", "无",
                    "无", "情绪稳定性", "无", "否"));
            indicatorMap1.put("text", "N分量");
            indicatorMap1.put("max", "14");
            indicator.add(indicatorMap1);
        } else if (scoreN >= 0) {
            resultMap.put("N结论", "情绪稳定");
            resultJson.put("N结论", "情绪稳定");
            newResultDtos.add(new NewResultDto("N分量", String.valueOf(scoreN), "无", "情绪稳定", "无",
                    "无", "情绪稳定性", "无", "否"));
            indicatorMap1.put("text", "N分量");
            indicatorMap1.put("max", "8");
            indicator.add(indicatorMap1);
        }


        Map<String, Object> indicatorMap2 = new HashMap<>();
        if (scoreP >= 8) {
            resultMap.put("P结论", "孤独、不关心他人，难以适应外部环境，不近人情，与别人不友好，喜欢寻衅搅扰，喜欢干奇特的事情，并且不顾危险。");
            resultJson.put("P结论", "孤独、不关心他人，难以适应外部环境，不近人情，与别人不友好，喜欢寻衅搅扰，喜欢干奇特的事情，并且不顾危险。");
            newResultDtos.add(new NewResultDto("P分量", String.valueOf(scoreP), "无", "孤独、不关心他人，难以适应外部环境，不近人情，与别人不友好，喜欢寻衅搅扰，喜欢干奇特的事情，并且不顾危险。", "无",
                    "无", "神经质", "无", "否"));
            indicatorMap2.put("text", "P分量");
            indicatorMap2.put("max", "23");
            indicator.add(indicatorMap2);
        } else {
            resultMap.put("P结论", "正常");
            resultJson.put("P结论", "正常");
            newResultDtos.add(new NewResultDto("P分量", String.valueOf(scoreP), "无", "正常", "无",
                    "无", "神经质", "无", "否"));
            indicatorMap2.put("text", "P分量");
            indicatorMap2.put("max", "7");
            indicator.add(indicatorMap2);
        }

        Map<String, Object> indicatorMap3 = new HashMap<>();
        if (scoreL >= 18) {
            resultMap.put("L结论", "测试无效");
            resultJson.put("L结论", "测试无效");
            newResultDtos.add(new NewResultDto("L分量", String.valueOf(scoreL), "无", "测试无效", "无",
                    "无", "掩饰、虚假", "无", "否"));
            indicatorMap3.put("text", "L分量");
            indicatorMap3.put("max", "20");
            indicator.add(indicatorMap3);
        } else {
            resultMap.put("L结论", "正常");
            resultJson.put("L结论", "正常");
            newResultDtos.add(new NewResultDto("L分量", String.valueOf(scoreL), "无", "正常", "无",
                    "无", "掩饰、虚假", "无", "否"));
            indicatorMap3.put("text", "L分量");
            indicatorMap3.put("max", "17");
            indicator.add(indicatorMap3);
        }

        newResultDtos.add(new NewResultDto("总分", String.valueOf(scoreN + scoreE + scoreL + scoreP), "无", "无", "无",
                "无", "无", "无", "是"));

        scoreList.add(String.valueOf(scoreE));
        scoreList.add(String.valueOf(scoreN));
        scoreList.add(String.valueOf(scoreP));
        scoreList.add(String.valueOf(scoreL));

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


        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_EPQ);
        returnJson.put("resultJson", resultJson);
        returnJson.put("newResultJson", newResult);



        //总分
//        double score = 0;
        //获取答案列表
//        List<AnswerEntity> answerEntities = (List<AnswerEntity>) resultJson.get("answerEntities");
        //获取评分规则列表
//        List<ScaleMarksEntity> scaleMarksEntities = (List<ScaleMarksEntity>) resultJson.get("scaleMarksEntities");
        //获取维度信息列表
//        List<DimensionEntity> dimensionEntities = (List<DimensionEntity>) resultJson.get("dimensionEntities");
        //计算每个维度得分dimensionEntities
//        double[] dimensionScore = new double[dimensionEntities.size()];
        //jsonArray:用户自己选择的题目选项
        /*for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            //计算总分
            for (AnswerEntity answerEntity : answerEntities) {
                if (answerEntity.getQuestionNo().equals(jsonObject1.getString("questionNo"))) {
                    if (answerEntity.getName().replaceAll("\\s", "").equals(jsonObject1.getString("checkItems").replaceAll("\\s", ""))) {
                        score += Double.parseDouble(answerEntity.getScore());
                        break;
                    }
                }
            }
            String institutionNo = resultJson.getString("institutionNo");
            String userId = resultJson.getString("userId");
            DimensionOrgService dimensionOrgService = (DimensionOrgService) resultJson.get("dimensionOrgService");
            List<WarnEntity> warnEntityList = new ArrayList<>();
            //计算维度分数
            for (int y = 0; y < dimensionEntities.size(); y++) {
                //获取维度信息
                DimensionEntity dimensionEntity = dimensionEntities.get(y);
                //获取该维度下有哪些选项
                String questionNos = dimensionEntity.getQuestionNo();
                //删除空白字符后得到进行划分得到选择题号
                String[] questionNo = questionNos.replaceAll("\\s", "").split("；");
                //计算每道题的得分
                for (String question : questionNo) {
                    if (question.equals(jsonObject1.getString("questionNo"))) {
                        //获取所有答案中本道题目的答案list数组
                        List<AnswerEntity> nowQuestionAnswerList = answerEntities.stream().filter(answerEntity -> answerEntity.getQuestionNo().equals(question)).collect(Collectors.toList());
                        //本题目的正确答案与自己的选项进行对比，正确的话相应分数进行相加
                        for (AnswerEntity answerEntity : nowQuestionAnswerList) {
                            if (answerEntity.getName().replaceAll("\\s", "").equals(jsonObject1.getString("checkItems").replaceAll("\\s", ""))) {
                                dimensionScore[y] += Double.parseDouble(answerEntity.getScore());
                                break;
                            }
                        }
                    }
                }
            }

            if(dimensionEntities != null && dimensionEntities.size() > 0){
                for (int y = 0; y < dimensionEntities.size(); y++) {
                    //计算维度逻辑是否需要预警
                    DimensionOrgEntity byInstitutionNoAndDimensionId = dimensionOrgService.findByInstitutionNoAndDimensionId(institutionNo, dimensionEntities.get(y).getId());
                    if(byInstitutionNoAndDimensionId != null){
                        WarnEntity warnEntity = null;
                        switch (byInstitutionNoAndDimensionId.getLogic()){
                            case "lt":
                                if(dimensionScore[y]<Double.parseDouble(byInstitutionNoAndDimensionId.getThreshold())){
                                    //添加预警信息
                                    warnEntityList.add( createWarnEntry(dimensionEntities.get(y).getFlag(),institutionNo,userId,dimensionEntities.get(y).getId(),dimensionScore[y]));
                                }
                                break;
                            case "le":
                                if(dimensionScore[y]<=Double.parseDouble(byInstitutionNoAndDimensionId.getThreshold())){
                                    //添加预警信息
                                    warnEntityList.add( createWarnEntry(dimensionEntities.get(y).getFlag(),institutionNo,userId,dimensionEntities.get(y).getId(),dimensionScore[y]));
                                }
                                break;
                            case "eq":
                                if(dimensionScore[y]==Double.parseDouble(byInstitutionNoAndDimensionId.getThreshold())){
                                    //添加预警信息
                                    warnEntityList.add( createWarnEntry(dimensionEntities.get(y).getFlag(),institutionNo,userId,dimensionEntities.get(y).getId(),dimensionScore[y]));
                                }
                                break;
                            case "ne":
                                if(dimensionScore[y] != Double.parseDouble(byInstitutionNoAndDimensionId.getThreshold())){
                                    //添加预警信息
                                    warnEntityList.add( createWarnEntry(dimensionEntities.get(y).getFlag(),institutionNo,userId,dimensionEntities.get(y).getId(),dimensionScore[y]));
                                }
                                break;
                            case "ge":
                                if(dimensionScore[y]>=Double.parseDouble(byInstitutionNoAndDimensionId.getThreshold())){
                                    //添加预警信息
                                    warnEntityList.add( createWarnEntry(dimensionEntities.get(y).getFlag(),institutionNo,userId,dimensionEntities.get(y).getId(),dimensionScore[y]));
                                }
                                break;
                            case "gt":
                                if(dimensionScore[y]>Double.parseDouble(byInstitutionNoAndDimensionId.getThreshold())){
                                    //添加预警信息
                                    warnEntityList.add( createWarnEntry(dimensionEntities.get(y).getFlag(),institutionNo,userId,dimensionEntities.get(y).getId(),dimensionScore[y]));
                                }
                                break;
                        }

                    }
                }
                resultJson.put("warnEntityList",warnEntityList);
            }
        }*/

        return returnJson;
    }
}
