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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.scale.dao.model.ScaleEntity;
import com.rf.psychological.utils.Constant;

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

/**
 * @description 儿童睡眠习惯问卷
 */
public class CSHQScale extends BaseScale {

    public CSHQScale(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;
        //异态睡眠得分
        int score6 = 0;
        //睡眠呼吸障碍得分
        int score7 = 0;
        //白天嗜睡得分
        int score8 = 0;
        //总分
        int score = 0;

        List<ScaleEntity> scaleEntities = (List<ScaleEntity>) resultJson.get("scaleEntities");

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            if (jsonObject1.getString("scoreDirection").equals("1")) {
                String[] split = scaleEntities.get(i).getCheckItems().split("；");
                if (i == 0 || i == 2 || i == 3 || i == 4 || i == 5 || i == 7) {
                    for (int y = 0; y < split.length; y++) {
                        if (jsonObject1.getString("checkItems").equals(split[y])) {
                            switch (y) {
                                case 0: {
                                    score1 += 3;
                                    break;
                                }
                                case 1: {
                                    score1 += 2;
                                    break;
                                }
                                case 2: {
                                    score1 += 1;
                                    break;
                                }
                            }
                        }
                    }
                } else if (i == 1) {
                    for (int y = 0; y < split.length; y++) {
                        if (jsonObject1.getString("checkItems").equals(split[y])) {
                            switch (y) {
                                case 0: {
                                    score2 += 3;
                                    break;
                                }
                                case 1: {
                                    score2 += 2;
                                    break;
                                }
                                case 2: {
                                    score2 += 1;
                                    break;
                                }
                            }
                        }
                    }
                    //todo 题号20的分类有问题
                } else if (i == 6 || i == 20) {
                    for (int y = 0; y < split.length; y++) {
                        if (jsonObject1.getString("checkItems").equals(split[y])) {
                            switch (y) {
                                case 0: {
                                    score4 += 3;
                                    break;
                                }
                                case 1: {
                                    score4 += 2;
                                    break;
                                }
                                case 2: {
                                    score4 += 1;
                                    break;
                                }
                            }
                        }
                    }
                } else if (i == 8 || i == 9 || i == 10) {
                    for (int y = 0; y < split.length; y++) {
                        if (jsonObject1.getString("checkItems").equals(split[y])) {
                            switch (y) {
                                case 0: {
                                    score3 += 3;
                                    break;
                                }
                                case 1: {
                                    score3 += 2;
                                    break;
                                }
                                case 2: {
                                    score3 += 1;
                                    break;
                                }
                            }
                        }
                    }
                } else if (i == 15 || i == 23 || i == 14) {
                    for (int y = 0; y < split.length; y++) {
                        if (jsonObject1.getString("checkItems").equals(split[y])) {
                            switch (y) {
                                case 0: {
                                    score5 += 3;
                                    break;
                                }
                                case 1: {
                                    score5 += 2;
                                    break;
                                }
                                case 2: {
                                    score5 += 1;
                                    break;
                                }
                            }
                        }
                    }
                } else if (i == 17 || i == 18 || i == 19) {
                    for (int y = 0; y < split.length; y++) {
                        if (jsonObject1.getString("checkItems").equals(split[y])) {
                            switch (y) {
                                case 0: {
                                    score7 += 3;
                                    break;
                                }
                                case 1: {
                                    score7 += 2;
                                    break;
                                }
                                case 2: {
                                    score7 += 1;
                                    break;
                                }
                            }
                        }
                    }
                } else if (i == 28 || i == 29 || i == 30 || i == 31 || i == 32) {
                    for (int y = 0; y < split.length; y++) {
                        if (jsonObject1.getString("checkItems").equals(split[y])) {
                            switch (y) {
                                case 0: {
                                    score8 += 3;
                                    break;
                                }
                                case 1: {
                                    score8 += 2;
                                    break;
                                }
                                case 2: {
                                    score8 += 1;
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    for (int y = 0; y < split.length; y++) {
                        if (jsonObject1.getString("checkItems").equals(split[y])) {
                            switch (y) {
                                case 0: {
                                    score6 += 3;
                                    break;
                                }
                                case 1: {
                                    score6 += 2;
                                    break;
                                }
                                case 2: {
                                    score6 += 1;
                                    break;
                                }
                            }
                        }
                    }
                }
            } else if (jsonObject1.getString("scoreDirection").equals("0")) {
                String[] split = scaleEntities.get(i).getCheckItems().split("；");
                if (i == 0 || i == 2 || i == 3 || i == 4 || i == 5 || i == 7) {
                    for (int y = 0; y < split.length; y++) {
                        if (jsonObject1.getString("checkItems").equals(split[y])) {
                            switch (y) {
                                case 0: {
                                    score1 += 1;
                                    break;
                                }
                                case 1: {
                                    score1 += 2;
                                    break;
                                }
                                case 2: {
                                    score1 += 3;
                                    break;
                                }
                            }
                        }
                    }
                } else if (i == 1) {
                    for (int y = 0; y < split.length; y++) {
                        if (jsonObject1.getString("checkItems").equals(split[y])) {
                            switch (y) {
                                case 0: {
                                    score2 += 1;
                                    break;
                                }
                                case 1: {
                                    score2 += 2;
                                    break;
                                }
                                case 2: {
                                    score2 += 3;
                                    break;
                                }
                            }
                        }
                    }
                } else if (i == 6 || i == 19) {
                    for (int y = 0; y < split.length; y++) {
                        if (jsonObject1.getString("checkItems").equals(split[y])) {
                            switch (y) {
                                case 0: {
                                    score4 += 1;
                                    break;
                                }
                                case 1: {
                                    score4 += 2;
                                    break;
                                }
                                case 2: {
                                    score4 += 3;
                                    break;
                                }
                            }
                        }
                    }
                } else if (i == 8 || i == 9 || i == 10) {
                    for (int y = 0; y < split.length; y++) {
                        if (jsonObject1.getString("checkItems").equals(split[y])) {
                            switch (y) {
                                case 0: {
                                    score3 += 1;
                                    break;
                                }
                                case 1: {
                                    score3 += 2;
                                    break;
                                }
                                case 2: {
                                    score3 += 3;
                                    break;
                                }
                            }
                        }
                    }
                } else if (i == 15 || i == 23 || i == 14) {
                    for (int y = 0; y < split.length; y++) {
                        if (jsonObject1.getString("checkItems").equals(split[y])) {
                            switch (y) {
                                case 0: {
                                    score5 += 1;
                                    break;
                                }
                                case 1: {
                                    score5 += 2;
                                    break;
                                }
                                case 2: {
                                    score5 += 3;
                                    break;
                                }
                            }
                        }
                    }
                } else if (i == 17 || i == 18 || i == 19) {
                    for (int y = 0; y < split.length; y++) {
                        if (jsonObject1.getString("checkItems").equals(split[y])) {
                            switch (y) {
                                case 0: {
                                    score7 += 1;
                                    break;
                                }
                                case 1: {
                                    score7 += 2;
                                    break;
                                }
                                case 2: {
                                    score7 += 3;
                                    break;
                                }
                            }
                        }
                    }
                } else if (i == 28 || i == 29 || i == 30 || i == 31 || i == 32) {
                    for (int y = 0; y < split.length; y++) {
                        if (jsonObject1.getString("checkItems").equals(split[y])) {
                            switch (y) {
                                case 0: {
                                    score8 += 1;
                                    break;
                                }
                                case 1: {
                                    score8 += 2;
                                    break;
                                }
                                case 2: {
                                    score8 += 3;
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    for (int y = 0; y < split.length; y++) {
                        if (jsonObject1.getString("checkItems").equals(split[y])) {
                            switch (y) {
                                case 0: {
                                    score6 += 1;
                                    break;
                                }
                                case 1: {
                                    score6 += 2;
                                    break;
                                }
                                case 2: {
                                    score6 += 3;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }

        score = score1 + score2 + score3 + score4 + score5 + score6 + score7 + score8;


        //得分列表
        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("总分", String.valueOf(score));
        resultMap.put("就寝习惯（睡眠阻抗）总分", String.valueOf(score1));
        resultMap.put("睡眠延迟总分", String.valueOf(score2));
        resultMap.put("睡眠持续时间总分", String.valueOf(score3));
        resultMap.put("睡眠焦虑总分", String.valueOf(score4));
        resultMap.put("夜醒总分", String.valueOf(score5));
        resultMap.put("异态睡眠总分", String.valueOf(score6));
        resultMap.put("睡眠呼吸障碍总分", String.valueOf(score7));
        resultMap.put("白天嗜睡总分", String.valueOf(score8));

        resultJson.put("总分", String.valueOf(score));
        resultJson.put("就寝习惯（睡眠阻抗）总分", String.valueOf(score1));
        resultJson.put("睡眠延迟总分", String.valueOf(score2));
        resultJson.put("睡眠持续时间总分", String.valueOf(score3));
        resultJson.put("睡眠焦虑总分", String.valueOf(score4));
        resultJson.put("夜醒总分", String.valueOf(score5));
        resultJson.put("异态睡眠总分", String.valueOf(score6));
        resultJson.put("睡眠呼吸障碍总分", String.valueOf(score7));
        resultJson.put("白天嗜睡总分", String.valueOf(score8));


        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_CSHQ);
        returnJson.put("resultJson", resultJson);

        return returnJson;
    }
}
