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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

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

/**
 * @author xbw
 * @description:魁北克睡眠问卷
 * @date 2021/10/29 15:00
 */
public class QSQScale extends BaseScale{
    public QSQScale(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<>();
        DecimalFormat df = new DecimalFormat("######0.00");
        //获取答案列表
        List<AnswerEntity> answerEntities = (List<AnswerEntity>) resultJson.get("answerEntities");
        //获取评分规则列表
        //获取维度信息列表
        List<DimensionEntity> dimensionEntities = (List<DimensionEntity>) resultJson.get("dimensionEntities");
        double sum = 0;
        Map<String ,Double> dimensionOrg = new HashMap<>();
        Map<String,Double> dimensionScoreMap = new HashMap<>();
        if (dimensionEntities.size() > 0){
            //计算每个维度得分
            double[] dimensionScore = new double[dimensionEntities.size()];
                for (int y = 0; y < dimensionEntities.size(); y++) {
                    DimensionEntity dimensionEntity = dimensionEntities.get(y);
                    sum = 0;
                    String questionNos = dimensionEntity.getQuestionNo();
                    String[] questionNo = questionNos.split("；");
                    for (String question:questionNo){
                        JSONObject jsonObject1 = jsonArray.getJSONObject(Integer.parseInt(question)-1);
                        if (question.equals(jsonObject1.getString("questionNo"))){
                            for (AnswerEntity answerEntity:answerEntities){
                                if (answerEntity.getQuestionNo().equals(question) && answerEntity.getName().equals(jsonObject1.getString("checkItems"))){
                                    dimensionScore[y] += Double.parseDouble(answerEntity.getScore());
                                    sum = sum + 1;
                                    break;
                                }
                            }
                        }
                    }
                    dimensionScore[y] = dimensionScore[y]/sum;//计算当前维度平均分
                    //检查最小值条件
                    if(dimensionEntity.getName().equals("日间症状") && dimensionScore[y] < 2.0){
                        dimensionScore[y] = 2.0;
                    }
                    else if (dimensionEntity.getName().equals("夜间症状") && dimensionScore[y] < 1.5){
                        dimensionScore[y] = 1.5;
                    }
                    else if (dimensionEntity.getName().equals("过度嗜睡") && dimensionScore[y] < 1.8){
                        dimensionScore[y] = 1.8;
                    }
                    else if (dimensionEntity.getName().equals("情绪") && dimensionScore[y] < 1.1){
                        dimensionScore[y] = 1.1;
                    }
                    else if (dimensionEntity.getName().equals("社会交往") && dimensionScore[y] < 2.5){
                        dimensionScore[y] = 2.5;
                    }
                    String dimensionSName = dimensionEntity.getName().concat("维度平均分");
                    dimensionScore[y] = Double.parseDouble(df.format(dimensionScore[y]));
                    dimensionOrg.put(dimensionEntity.getId(),dimensionScore[y]);
                    resultMap0.put(dimensionSName, String.valueOf(dimensionScore[y]));
                    resultJson.put(dimensionSName, String.valueOf(dimensionScore[y]));

                    dimensionScoreMap.put(dimensionEntity.getId(),dimensionScore[y]);
                }
                resultJson.put("dimensionScoreMap",dimensionScoreMap);
                //计算总分
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                for (AnswerEntity answerEntity:answerEntities){
                    if (answerEntity.getQuestionNo().equals(String.valueOf(i+1))){
                        if (answerEntity.getName().equals(jsonObject1.getString("checkItems"))){
                            score += Integer.valueOf(answerEntity.getScore());
                        }
                    }
                }
            }

            score = Double.parseDouble(df.format(score/jsonArray.size()));
            resultMap0.put("QSQ平均分", String.valueOf(score));
            resultJson.put("QSQ平均分", String.valueOf(score));
            resultMapList.add(resultMap0);
        }
        JSONObject returnJson = new JSONObject(true);
        returnJson.put("resultMapList",resultMapList);
        //returnJson.put("scaleName",resultJson.getString("scaleName"));
        returnJson.put("resultJson",resultJson);
        return returnJson;
    }
}
