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.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 癫痫患者自我管理量表
 */
public class ESMScale extends BaseScale {

    public ESMScale(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 score = 0;

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            if (i == 3 || i == 7 || i == 8 || i == 15 || i == 20 || i == 23 || i == 24 || i == 26 || i == 27 || i == 29) {
                if (jsonObject1.getString("scoreDirection").equals("1")) {
                    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 (jsonObject1.getString("checkItems").equals("大多数")) {
                        score1 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score1 += 5;
                    }
                } else if (jsonObject1.getString("scoreDirection").equals("0")) {
                    if (jsonObject1.getString("checkItems").equals("从不")) {
                        score1 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("很少")) {
                        score1 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("有时")) {
                        score1 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("大多数")) {
                        score1 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score1 += 1;
                    }
                }

            } else if (i == 0 || i == 2 || i == 4 || i == 6 || i == 37) {
                if (jsonObject1.getString("scoreDirection").equals("1")) {
                    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 (jsonObject1.getString("checkItems").equals("大多数")) {
                        score2 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score2 += 5;
                    }
                } else if (jsonObject1.getString("scoreDirection").equals("0")) {
                    if (jsonObject1.getString("checkItems").equals("从不")) {
                        score2 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("很少")) {
                        score2 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("有时")) {
                        score2 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("大多数")) {
                        score2 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score2 += 1;
                    }
                }
            } else if (i == 5 || i == 16 || i == 22 || i == 28 || i == 30 || i == 33 || i == 35) {
                if (jsonObject1.getString("scoreDirection").equals("1")) {
                    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 (jsonObject1.getString("checkItems").equals("大多数")) {
                        score3 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score3 += 5;
                    }
                } else if (jsonObject1.getString("scoreDirection").equals("0")) {
                    if (jsonObject1.getString("checkItems").equals("从不")) {
                        score3 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("很少")) {
                        score3 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("有时")) {
                        score3 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("大多数")) {
                        score3 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score3 += 1;
                    }
                }
            } else if (i == 9 || i == 10 || i == 11 || i == 14 || i == 18 || i == 31) {
                if (jsonObject1.getString("scoreDirection").equals("1")) {
                    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 (jsonObject1.getString("checkItems").equals("大多数")) {
                        score4 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score4 += 5;
                    }
                } else if (jsonObject1.getString("scoreDirection").equals("0")) {
                    if (jsonObject1.getString("checkItems").equals("从不")) {
                        score4 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("很少")) {
                        score4 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("有时")) {
                        score4 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("大多数")) {
                        score4 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score4 += 1;
                    }
                }
            } else {
                if (jsonObject1.getString("scoreDirection").equals("1")) {
                    if (jsonObject1.getString("checkItems").equals("从不")) {
                        score5 += 1;
                    } else if (jsonObject1.getString("checkItems").equals("很少")) {
                        score5 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("有时")) {
                        score5 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("大多数")) {
                        score5 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score5 += 5;
                    }
                } else if (jsonObject1.getString("scoreDirection").equals("0")) {
                    if (jsonObject1.getString("checkItems").equals("从不")) {
                        score5 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("很少")) {
                        score5 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("有时")) {
                        score5 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("大多数")) {
                        score5 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score5 += 1;
                    }
                }
            }
        }

        score = score1 + score2 + score3 + score4 + score5;

        //得分列表
        Map<String, String> resultMap = new LinkedHashMap<>();
        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(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(score));

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

        return returnJson;
    }
}
