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;

/**
 * @author zsy
 * @description:贝克抑郁自评量表
 * @date 2021/7/20 15:55
 */
public class BDIScale extends BaseScale{

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

    public  JSONObject scaleCalculate() throws Exception {
        //得分
        int score = 0;
        List<ScaleEntity> scaleEntities = (List<ScaleEntity>) resultJson.get("scaleEntities");
        //答题结果列表
        List<ScaleEntity> dataList = new ArrayList<>();
        List<ScaleEntity> resultDataList = jsonArray.toJavaList(ScaleEntity.class);
        int x = 0;
        for (ScaleEntity scaleEntity:resultDataList){
            scaleEntity.setAnswer(scaleEntities.get(x).getCheckItems());
            dataList.add(scaleEntity);
            x ++;
        }
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            String[] split = scaleEntities.get(i).getCheckItems().split("；");
            if (jsonObject1.getString("scoreDirection").equals("1")){
                for (int y = 0; y < split.length; y++){
                    if (jsonObject1.getString("checkItems").equals(split[y])){
                        switch (y){
                            case 1:{
                                score += 1;
                                break;
                            }
                            case 2:{
                                score += 2;
                                break;
                            }
                            case 3:{
                                score += 3;
                                break;
                            }
                        }
                    }
                }
            }else {
                for (int y = 0; y < split.length; y++){
                    if (jsonObject1.getString("checkItems").equals(split[y])){
                        switch (y){
                            case 0:{
                                score += 3;
                                break;
                            }
                            case 1:{
                                score += 2;
                                break;
                            }
                            case 2:{
                                score += 1;
                                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_BDI);
        returnJson.put("resultJson",resultJson);

        return returnJson;
    }
}
