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 生活满意度指数B
 */
public class LSIBScale extends BaseScale {

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

    public JSONObject scaleCalculate() throws Exception {
        //总分
        int score = 0;

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

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);

            String[] split = scaleEntities.get(i).getCheckItems().split("；");
            if (i == 0 || i == 5) {
                for (int y = 0; y < split.length; y++) {
                    if (jsonObject1.getString("checkItems").equals(split[y])) {
                        switch (y) {
                            case 0: {
                                score += 2;
                                break;
                            }
                            case 1: {
                                score += 0;
                                break;
                            }
                        }
                    }
                }
            } else {
                for (int y = 0; y < split.length; y++) {
                    if (jsonObject1.getString("checkItems").equals(split[y])) {
                        switch (y) {
                            case 0: {
                                score += 2;
                                break;
                            }
                            case 1: {
                                score += 1;
                                break;
                            }
                            case 2: {
                                score += 0;
                                break;
                            }
                        }
                    }
                }
            }
        }

        //得分列表
        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("总分", String.valueOf(score));
        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_LSIB);
        returnJson.put("resultJson", resultJson);

        return returnJson;
    }
}
