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:健康调查简表
 * @date 2021/7/20 15:55
 */

public class SF36Scale extends BaseScale{
    public SF36Scale(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");
        //躯体疼痛(bodily pain,BP)
        double BP = 0;
        //计算每个维度得分
        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"))){
                       /* if (jsonObject1.getString("questionNo").equals("21") && jsonObject1.getString("checkItems").equals("完全没有")){
                            BP = 6;
                            if (jsonArray.getJSONObject(21).getString("checkItems").equals("完全没有影响")){
                                BP += 6;
                            }else if (jsonArray.getJSONObject(21).getString("checkItems").equals("有一点影响")){
                                BP += 4.75;
                            }else if (jsonArray.getJSONObject(21).getString("checkItems").equals("中等影响")){
                                BP += 3.5;
                            }else if (jsonArray.getJSONObject(21).getString("checkItems").equals("影响很大")){
                                BP += 2.25;
                            }else {
                                BP += 1;
                            }
                        }else {*/
                            for (AnswerEntity answerEntity:answerEntities){
                                if (answerEntity.getQuestionNo().equals(question) && answerEntity.getName().equals(jsonObject1.getString("checkItems"))){
                                    dimensionScore[y] += Double.valueOf(answerEntity.getScore());
                                }
                            }
                        //}
                        break;
                    }
                }
            }
        }

        DecimalFormat df = new DecimalFormat("######0.00");
//        生理功能（physical functioning,PF）
        double PF = Double.parseDouble(df.format((dimensionScore[0] - 10) * 5));
//        生理职能(role- physical,RP)
        double RP =Double.parseDouble(df.format((dimensionScore[1] - 4) * 25));
        //躯体疼痛(bodily pain,BP)
        /*if (jsonArray.getJSONObject(20).getString("questionNo").equals("21") && jsonArray.getJSONObject(20).getString("checkItems").equals("完全没有")){
            BP = Double.parseDouble(df.format((BP - 2) * 10));
        }else {*/
            BP = Double.parseDouble(df.format((dimensionScore[2] - 2) * 10));
        //}
//        总体健康(general health,GH)
        double GH =Double.parseDouble(df.format((dimensionScore[3] - 5) * 5));
//        活力(validity,VT)
        double VT =Double.parseDouble(df.format((dimensionScore[4] - 4) * 5));
//        社会功能(social function,SF)
        double SF =Double.parseDouble(df.format((dimensionScore[5] - 2) * 12.5));
//        情感职能(role-emotional,RE)
        double RoleE =Double.parseDouble(df.format((dimensionScore[6] - 3)/3 * 100));
//        精神健康(mental health,MH)
        double MH =Double.parseDouble(df.format((dimensionScore[7] - 5) * 4));
//        健康变化（Reported Health Transition,HT）
        double HT = Double.parseDouble(df.format(dimensionScore[8]));

        resultMap0.put("生理功能得分", String.valueOf(PF));
        resultJson.put("生理功能得分", String.valueOf(PF));
        resultMap0.put("生理职能得分", String.valueOf(RP));
        resultJson.put("生理职能得分", String.valueOf(RP));
        resultMap0.put("躯体疼痛得分", String.valueOf(BP));
        resultJson.put("躯体疼痛得分", String.valueOf(BP));
        resultMap0.put("总体健康得分", String.valueOf(GH));
        resultJson.put("总体健康得分", String.valueOf(GH));
        resultMap0.put("活力得分", String.valueOf(VT));
        resultJson.put("活力得分", String.valueOf(VT));
        resultMap0.put("社会功能得分", String.valueOf(SF));
        resultJson.put("社会功能得分", String.valueOf(SF));
        resultMap0.put("情感职能得分", String.valueOf(RoleE));
        resultJson.put("情感职能得分", String.valueOf(RoleE));
        resultMap0.put("精神健康得分", String.valueOf(MH));
        resultJson.put("精神健康得分", String.valueOf(MH));
        resultMap0.put("健康变化得分", String.valueOf(HT));
        resultJson.put("健康变化得分", String.valueOf(HT));
        resultMapList.add(resultMap0);

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

        return returnJson;
    }
}
