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 STAIScale extends BaseScale {

    public STAIScale(JSONArray jsonArray, JSONObject resultJson) {
        super(jsonArray, resultJson);
    }

    public JSONObject scaleCalculate() throws Exception {
        //状态焦虑分
        int scoreS = 0;
        //特质焦虑分
        int scoreT = 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 <= 19) {
                    for (int y = 0; y < split.length; y++) {
                        if (jsonObject1.getString("checkItems").equals(split[y])) {
                            switch (y) {
                                case 0: {
                                    scoreS += 1;
                                    break;
                                }
                                case 1: {
                                    scoreS += 2;
                                    break;
                                }
                                case 2: {
                                    scoreS += 3;
                                    break;
                                }
                                case 3: {
                                    scoreS += 4;
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    for (int y = 0; y < split.length; y++) {
                        if (jsonObject1.getString("checkItems").equals(split[y])) {
                            switch (y) {
                                case 0: {
                                    scoreT += 1;
                                    break;
                                }
                                case 1: {
                                    scoreT += 2;
                                    break;
                                }
                                case 2: {
                                    scoreT += 3;
                                    break;
                                }
                                case 3: {
                                    scoreT += 4;
                                    break;
                                }
                            }
                        }
                    }
                }
            } else if (jsonObject1.getString("scoreDirection").equals("0")) {
                String[] split = scaleEntities.get(i).getCheckItems().split("；");
                if (i <= 19) {
                    for (int y = 0; y < split.length; y++) {
                        if (jsonObject1.getString("checkItems").equals(split[y])) {
                            switch (y) {
                                case 0: {
                                    scoreS += 4;
                                    break;
                                }
                                case 1: {
                                    scoreS += 3;
                                    break;
                                }
                                case 2: {
                                    scoreS += 2;
                                    break;
                                }
                                case 3: {
                                    scoreS += 1;
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    for (int y = 0; y < split.length; y++) {
                        if (jsonObject1.getString("checkItems").equals(split[y])) {
                            switch (y) {
                                case 0: {
                                    scoreT += 4;
                                    break;
                                }
                                case 1: {
                                    scoreT += 3;
                                    break;
                                }
                                case 2: {
                                    scoreT += 2;
                                    break;
                                }
                                case 3: {
                                    scoreT += 1;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }

        //得分列表
        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("状态焦虑分", String.valueOf(scoreS));
        resultJson.put("特质焦虑分", String.valueOf(scoreT));


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

        return returnJson;
    }
}
