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.ScaleMarksEntity;

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

/**
 * @author zsy
 * @description:BRIEF量表
 * @date 2021/7/20 15:55
 */

public class BRIEFScale extends BaseScale{
    public BRIEFScale(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<ScaleMarksEntity> scaleMarksEntities = (List<ScaleMarksEntity>) resultJson.get("scaleMarksEntities");
        //获取维度信息列表
        List<DimensionEntity> dimensionEntities = (List<DimensionEntity>) resultJson.get("dimensionEntities");
        //计算每个维度得分
        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){
                    if (question.equals(jsonObject1.getString("questionNo"))){
                        for (AnswerEntity answerEntity:answerEntities){
                            if (answerEntity.getQuestionNo().equals(question) && answerEntity.getName().equals(jsonObject1.getString("checkItems"))){
                                dimensionScore[y] += Double.valueOf(answerEntity.getScore());
                            }
                        }
                        break;
                    }
                }
            }
        }

        score += dimensionScore[8] + dimensionScore[9];
        DecimalFormat df = new DecimalFormat("######0.00");
        resultMap0.put("总均分", String.valueOf(Double.parseDouble(df.format(score/72))));
        resultJson.put("总均分", String.valueOf(Double.parseDouble(df.format(score/72))));
        for (int y = 0; y < dimensionEntities.size(); y++) {
            DimensionEntity dimensionEntity = dimensionEntities.get(y);
            if (dimensionEntity.getName().equals("抑制")){
                resultMap0.put(dimensionEntity.getName() + "均分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]/10))));
                resultJson.put(dimensionEntity.getName() + "均分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]/10))));
            }else if (dimensionEntity.getName().equals("转换")){
                resultMap0.put(dimensionEntity.getName() + "均分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]/8))));
                resultJson.put(dimensionEntity.getName() + "均分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]/8))));
            }else if (dimensionEntity.getName().equals("情感控制")){
                resultMap0.put(dimensionEntity.getName() + "均分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]/10))));
                resultJson.put(dimensionEntity.getName() + "均分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]/10))));
            }else if (dimensionEntity.getName().equals("启动")){
                resultMap0.put(dimensionEntity.getName() + "均分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]/8))));
                resultJson.put(dimensionEntity.getName() + "均分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]/8))));
            }else if (dimensionEntity.getName().equals("工作记忆")){
                resultMap0.put(dimensionEntity.getName() + "均分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]/10))));
                resultJson.put(dimensionEntity.getName() + "均分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]/10))));
            }else if (dimensionEntity.getName().equals("计划")){
                resultMap0.put(dimensionEntity.getName() + "均分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]/12))));
                resultJson.put(dimensionEntity.getName() + "均分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]/12))));
            }else if (dimensionEntity.getName().equals("组织")){
                resultMap0.put(dimensionEntity.getName() + "均分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]/6))));
                resultJson.put(dimensionEntity.getName() + "均分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]/6))));
            }else if (dimensionEntity.getName().equals("监控")){
                resultMap0.put(dimensionEntity.getName() + "均分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]/8))));
                resultJson.put(dimensionEntity.getName() + "均分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]/8))));
            }else if (dimensionEntity.getName().equals("行为管理指数")){
                resultMap0.put(dimensionEntity.getName() + "均分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]/28))));
                resultJson.put(dimensionEntity.getName() + "均分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]/28))));
            }else {
                resultMap0.put(dimensionEntity.getName() + "均分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]/44))));
                resultJson.put(dimensionEntity.getName() + "均分", String.valueOf(Double.parseDouble(df.format(dimensionScore[y]/44))));
            }
        }
        resultMapList.add(resultMap0);

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

        return returnJson;
    }
}
