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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.utils.Constant;

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

/**
 * @description 儿童行为问卷（父母用）
 */
public class CCSASScale extends BaseScale {
    public CCSASScale(JSONArray jsonArray, JSONObject resultJson) {
        super(jsonArray, resultJson);
    }

    public JSONObject scaleCalculate() throws Exception {
        //品行行为总分
        int score1 = 0;
        //学习问题总分
        int score2 = 0;
        //心神障碍总分
        int score3 = 0;
        //冲动-多动总分
        int score4 = 0;
        //焦虑总分
        int score5 = 0;

        //品行行为均分
        double score1A = 0;
        //学习问题均分
        double score2A = 0;
        //心神障碍均分
        double score3A = 0;
        //冲动-多动均分
        double score4A = 0;
        //焦虑均分
        double score5A = 0;


        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            if ((i + 1) == 2 || (i + 1) == 8 || (i + 1) == 14 || (i + 1) == 19 || (i + 1) == 20 || (i + 1) == 21 || (i + 1) == 22 || (i + 1) == 23 || (i + 1) == 27 || (i + 1) == 33 || (i + 1) == 34 || (i + 1) == 39) {
                if (jsonObject1.getString("checkItems").equals("无")) {
                    score1 += 0;
                } else if (jsonObject1.getString("checkItems").equals("稍有")) {
                    score1 += 1;
                } else if (jsonObject1.getString("checkItems").equals("相当多")) {
                    score1 += 2;
                } else if (jsonObject1.getString("checkItems").equals("很多")) {
                    score1 += 3;
                }
            } else if ((i + 1) == 10 || (i + 1) == 25 || (i + 1) == 31 || (i + 1) == 37) {
                if (jsonObject1.getString("checkItems").equals("无")) {
                    score2 += 0;
                } else if (jsonObject1.getString("checkItems").equals("稍有")) {
                    score2 += 1;
                } else if (jsonObject1.getString("checkItems").equals("相当多")) {
                    score2 += 2;
                } else if (jsonObject1.getString("checkItems").equals("很多")) {
                    score2 += 3;
                }
            } else if ((i + 1) == 32 || (i + 1) == 41 || (i + 1) == 43 || (i + 1) == 44 || (i + 1) == 48) {
                if (jsonObject1.getString("checkItems").equals("无")) {
                    score3 += 0;
                } else if (jsonObject1.getString("checkItems").equals("稍有")) {
                    score3 += 1;
                } else if (jsonObject1.getString("checkItems").equals("相当多")) {
                    score3 += 2;
                } else if (jsonObject1.getString("checkItems").equals("很多")) {
                    score3 += 3;
                }
            } else if ((i + 1) == 4 || (i + 1) == 5 || (i + 1) == 1 || (i + 1) == 13) {
                if (jsonObject1.getString("checkItems").equals("无")) {
                    score4 += 0;
                } else if (jsonObject1.getString("checkItems").equals("稍有")) {
                    score4 += 1;
                } else if (jsonObject1.getString("checkItems").equals("相当多")) {
                    score4 += 2;
                } else if (jsonObject1.getString("checkItems").equals("很多")) {
                    score4 += 3;
                }
            } else if ((i + 1) == 12 || (i + 1) == 16 || (i + 1) == 24 || (i + 1) == 47) {
                if (jsonObject1.getString("checkItems").equals("无")) {
                    score5 += 0;
                } else if (jsonObject1.getString("checkItems").equals("稍有")) {
                    score5 += 1;
                } else if (jsonObject1.getString("checkItems").equals("相当多")) {
                    score5 += 2;
                } else if (jsonObject1.getString("checkItems").equals("很多")) {
                    score5 += 3;
                }
            }
        }

        DecimalFormat df = new DecimalFormat("######0.00");

        score1A = Double.parseDouble(df.format(score1 / 12.0));
        score2A = Double.parseDouble(df.format(score2 / 4.0));
        score3A = Double.parseDouble(df.format(score3 / 5.0));
        score4A = Double.parseDouble(df.format(score4 / 4.0));
        score5A = Double.parseDouble(df.format(score5 / 5.0));


        //得分列表
        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("品行行为均分", String.valueOf(score1A));
        resultMap.put("学习问题均分", String.valueOf(score2A));
        resultMap.put("心神障碍均分", String.valueOf(score3A));
        resultMap.put("冲动-多动均分", String.valueOf(score4A));
        resultMap.put("焦虑均分", String.valueOf(score5A));

        resultJson.put("品行行为均分", String.valueOf(score1A));
        resultJson.put("学习问题均分", String.valueOf(score2A));
        resultJson.put("心神障碍均分", String.valueOf(score3A));
        resultJson.put("冲动-多动均分", String.valueOf(score4A));
        resultJson.put("焦虑均分", String.valueOf(score5A));


        List<Map<String, String>> resultMapList = new ArrayList<>();
        resultMapList.add(resultMap);

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

        return returnJson;
    }
}
