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.ScaleEntity;
import com.rf.psychological.scale.dao.model.ScaleMarksEntity;
import com.rf.psychological.scale.scaleresult.ScaleConstant;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zsy
 * @description:医学应对问卷
 * @date 2021/7/20 15:55
 */
public class MCMQScale extends BaseScale {

    public MCMQScale(JSONArray jsonArray, JSONObject resultJson) {
        super(jsonArray, resultJson);
    }

    public JSONObject scaleCalculate() throws Exception {

        //总分
        double score = 0;
        Map<String, String> resultMap0 = new LinkedHashMap<>();
        List<Map<String, String>> resultMapList = new ArrayList<>();

        //获取答案列表
        List<AnswerEntity> answerEntities = (List<AnswerEntity>) resultJson.get("answerEntities");
        //获取题目和答案列表
        List<ScaleEntity> scaleEntities = (List<ScaleEntity>) resultJson.get("scaleEntities");
        //获取评分规则列表
        List<ScaleMarksEntity> scaleMarksEntities = (List<ScaleMarksEntity>) resultJson.get("scaleMarksEntities");
        //获取维度信息列表
        List<DimensionEntity> dimensionEntities = (List<DimensionEntity>) resultJson.get("dimensionEntities");
        Map<String ,Double> dimensionScoreMap = new HashMap<>();
        //计算每个维度得分
        double[] dimensionScore = new double[dimensionEntities.size()];
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            for (int y = 0; y < dimensionEntities.size(); y++) {
                //for (DimensionEntity dimensionEntity:dimensionEntities){
                DimensionEntity dimensionEntity = dimensionEntities.get(y);
                String questionNos = dimensionEntity.getQuestionNo();
                String[] questionNo = questionNos.split("；");
                for (String question:questionNo){
                    String checkItem;
                    if (question.equals(jsonObject1.getString("questionNo"))){
                        if (question.equals("2")){
                            checkItem = jsonObject1.getString("checkItems");
                            String[] checkItems = checkItem.split(";");
                            for (String item:checkItems){
                                if (item.equals("不想")){
                                    dimensionScore[y] += 1;
                                }else if (item.equals("有时想")){
                                    dimensionScore[y] += 2;
                                }else if (item.equals("经常想")){
                                    dimensionScore[y] += 3;
                                }else if (item.equals("总是想")){
                                    dimensionScore[y] += 4;
                                }
                            }
                        }
                        if (question.equals("4")){
                            checkItem = jsonObject1.getString("checkItems");
                            String[] checkItems = checkItem.split(";");
                            for (String item:checkItems){
                                if (item.equals("从不这样")){
                                    dimensionScore[y] += 1;
                                }else if (item.equals("有时这样")){
                                    dimensionScore[y] += 2;
                                }else if (item.equals("经常这样")){
                                    dimensionScore[y] += 3;
                                }else if (item.equals("总是这样")){
                                    dimensionScore[y] += 4;
                                }
                            }
                        }
                        if (question.equals("6")){
                            checkItem = jsonObject1.getString("checkItems");
                            String[] checkItems = checkItem.split(";");
                            for (String item:checkItems){
                                if (item.equals("从不这样")){
                                    dimensionScore[y] += 1;
                                }else if (item.equals("有时这样")){
                                    dimensionScore[y] += 2;
                                }else if (item.equals("经常这样")){
                                    dimensionScore[y] += 3;
                                }else if (item.equals("总是这样")){
                                    dimensionScore[y] += 4;
                                }
                            }
                        }
                        for (AnswerEntity answerEntity:answerEntities){
                            if (answerEntity.getQuestionNo().equals(question) && answerEntity.getName().equals(jsonObject1.getString("checkItems"))){
                                dimensionScore[y] += Double.parseDouble(answerEntity.getScore());
                                dimensionScoreMap.put(dimensionEntity.getId(),dimensionScoreMap.get(dimensionEntity.getId())==null?Double.parseDouble(answerEntity.getScore()):(dimensionScoreMap.get(dimensionEntity.getId()) +Double.parseDouble(answerEntity.getScore())));
                            }
                        }
                        break;
                    }
                }

            }
        }
        resultJson.put("dimensionScoreMap",dimensionScoreMap);
        //计算总分
        score = dimensionScore[0] + dimensionScore[1] + dimensionScore[2];
        resultMap0.put("总分", String.valueOf(score));
        resultJson.put("总分", String.valueOf(score));
        for (int y = 0; y < dimensionEntities.size(); y++) {
            DimensionEntity dimensionEntity = dimensionEntities.get(y);
            DecimalFormat df = new DecimalFormat("######0.00");
            resultMap0.put(dimensionEntity.getName() + "得分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]))));
            resultJson.put(dimensionEntity.getName() + "得分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]))));
        }
        resultMapList.add(resultMap0);

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

        return returnJson;
    }

}
