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

/**
 * @author lhl
 * @description:Weiss 功能缺陷量表(父母版)
 * @date 2021/10/31
 */
public class WFIRSScale extends BaseScale {
    public WFIRSScale(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");
        if (dimensionEntities.size() > 0){
            //计算每个维度得分
            double[] dimensionScore = new double[dimensionEntities.size()];
            //存储各维度得分项为3分的选项个数
            int []three=new int[dimensionEntities.size()];
            //存储各维度得分项为2分的选项个数
            int []two=new int[dimensionEntities.size()];
            //存储各维度选项"不适用"的个数
            int []nonsuit=new int[dimensionEntities.size()];
            //存储各维度选项"从不"的个数
            ///int []never=new int[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.parseDouble(answerEntity.getScore());
                                     if(Integer.parseInt(answerEntity.getScore())==2){
                                         two[y]+=1;
                                     }else if (Integer.parseInt(answerEntity.getScore())==3){
                                         three[y]+=1;
                                     }else if (jsonObject1.getString("checkItems").equals("不适用")){
                                         nonsuit[y]+=1;
                                     }
                                }
                            }
                            break;
                        }
                    }
                }
               /* //计算总分
                for (AnswerEntity answerEntity:answerEntities){
                    if (answerEntity.getQuestionNo().equals(String.valueOf(i+1))){
                        if (answerEntity.getName().equals(jsonObject1.getString("checkItems"))){
                            score += Integer.valueOf(answerEntity.getScore());
                        }
                    }
                }*/
            }

           /* resultMap0.put("GDMS总分", String.valueOf(score));
            resultJson.put("GDMS总分", String.valueOf(score));*/

            //计算每个维度的平均分(选项为："不适用"，不参与评分)
            DecimalFormat df = new DecimalFormat("######0.00");
            double[] Mdscore = new double[dimensionScore.length];
            for (int i = 0; i < dimensionScore.length; i++) {
                if(nonsuit[i]!=0&&i!=3&&i!=4){
                    if(10.0-nonsuit[i]!=0) {
                        Mdscore[i] = Double.parseDouble(df.format(dimensionScore[i]/(10.0 - nonsuit[i])));
                    }else{
                        Mdscore[i] =0;
                    }
                }else if(nonsuit[i]!=0&&i==3){
                    if(3.0-nonsuit[i]!=0) {
                        Mdscore[3] = Double.parseDouble(df.format(dimensionScore[3]/(3.0 - nonsuit[i])));
                    }else{
                        Mdscore[3] =0;
                    }
                }else if(nonsuit[i]!=0&&i==4){
                    if(7.0-nonsuit[i]!=0) {
                        Mdscore[4] = Double.parseDouble(df.format(dimensionScore[4]/(7.0 - nonsuit[i])));
                    }else{
                        Mdscore[4] =0;
                    }
                }else if(nonsuit[i]==0) {
                    if(i==3){
                        Mdscore[3]= Double.parseDouble(df.format(dimensionScore[3]/3.0));
                    }else if(i==4){
                        Mdscore[4]= Double.parseDouble(df.format(dimensionScore[4]/7.0));
                    }else {
                        Mdscore[i]= Double.parseDouble(df.format(dimensionScore[i]/10.0));
                    }
                }

            }
           /* Mdscore[0]= Double.parseDouble(df.format(dimensionScore[0]/10.0));
            Mdscore[1]= Double.parseDouble(df.format(dimensionScore[1]/10.0));
            Mdscore[2]= Double.parseDouble(df.format(dimensionScore[2]/10.0));
            Mdscore[3]= Double.parseDouble(df.format(dimensionScore[3]/3.0));
            Mdscore[4]= Double.parseDouble(df.format(dimensionScore[4]/7.0));
            Mdscore[5]= Double.parseDouble(df.format(dimensionScore[5]/10.0));*/
            Map<String,Double> dimensionScoreMap = new HashMap<>();

             if(two[0]>=2||three[0]>=1||Mdscore[0]>=1.5){
                 resultMap0.put("家庭领域", "临床上存在功能损害");
                 resultJson.put("家庭领域", "临床上存在功能损害");
             }else {
                 resultMap0.put("家庭领域", String.valueOf(Mdscore[0]));
                 resultJson.put("家庭领域", String.valueOf(Mdscore[0]));
             }
            if(two[1]>=2||three[1]>=1||Mdscore[1]>=1.5){
                resultMap0.put("学习和学校领域", "临床上存在功能损害");
                resultJson.put("学习和学校领域", "临床上存在功能损害");
            }else {
                resultMap0.put("学习和学校领域", String.valueOf(Mdscore[1]));
                resultJson.put("学习和学校领域", String.valueOf(Mdscore[1]));
            }
            if(two[2]>=2||three[2]>=1||Mdscore[2]>=1.5){
                resultMap0.put("生活技能领域", "临床上存在功能损害");
                resultJson.put("生活技能领域", "临床上存在功能损害");
            }else {
                resultMap0.put("生活技能领域", String.valueOf(Mdscore[2]));
                resultJson.put("生活技能领域", String.valueOf(Mdscore[2]));
            }
            if(two[3]>=2||three[3]>=1||Mdscore[3]>=1.5){
                resultMap0.put("自我管理领域", "临床上存在功能损害");
                resultJson.put("自我管理领域", "临床上存在功能损害");
            }else {
                resultMap0.put("自我管理领域", String.valueOf(Mdscore[3]));
                resultJson.put("自我管理领域", String.valueOf(Mdscore[3]));
            }
            if(two[4]>=2||three[4]>=1||Mdscore[4]>=1.5){
                resultMap0.put("社会活动领域", "临床上存在功能损害");
                resultJson.put("社会活动领域", "临床上存在功能损害");
            }else {
                resultMap0.put("社会活动领域", String.valueOf(Mdscore[3]));
                resultJson.put("社会活动领域", String.valueOf(Mdscore[3]));
            }
            if(two[5]>=2||three[5]>=1||Mdscore[5]>=1.5){
                resultMap0.put("冒险活动领域", "临床上存在功能损害");
                resultJson.put("冒险活动领域", "临床上存在功能损害");
            }else {
                resultMap0.put("冒险活动领域", String.valueOf(Mdscore[3]));
                resultJson.put("冒险活动领域", String.valueOf(Mdscore[3]));
            }
            dimensionScoreMap.put("2c91f37d7bec0878017c40dcbb93382e",Mdscore[0]);
            dimensionScoreMap.put("2c91f37d7bec0878017c40dcbbc0382f",Mdscore[1]);
            dimensionScoreMap.put("2c91f37d7bec0878017c40dcbbec3830",Mdscore[2]);
            dimensionScoreMap.put("2c91f37d7bec0878017c40dcbc183831",Mdscore[3]);
            dimensionScoreMap.put("2c91f37d7bec0878017c40dcbc443832",Mdscore[3]);
            dimensionScoreMap.put("2c91f37d7bec0878017c40dcbc703833",Mdscore[3]);
            resultJson.put("dimensionScoreMap",dimensionScoreMap);
            resultMapList.add(resultMap0);
        }

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

        return returnJson;

    }




}
