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;

/**
 * @author zsy
 * @description:多维完美主义问卷
 * @date 2021/7/20 15:55
 */
public class MPSScale extends BaseScale{

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

    public  JSONObject scaleCalculate() throws Exception {

        // 自定义路径使用
        /*ScaleUtil.getScaleDll("MPSSCALE.dll");
        System.out.println("JSONArray:"+jsonArray);
        System.out.println("resultJson:"+resultJson);
        XScale xScale = new XScale(jsonArray,resultJson);
        String result = xScale.MPSScale(jsonArray.toString(), resultJson.toString());
        //System.out.println("dllresult:"+result);
        //解决排序问题
        LinkedHashMap<String, Object> jsonMap = JSON.parseObject(result,LinkedHashMap.class, Feature.OrderedField);
        JSONObject jsonObject = new JSONObject(true);
        jsonObject.putAll(jsonMap);
        System.out.println("jsonObject:"+jsonObject);
        return jsonObject;*/

        //完美主义高标准得分
        int score = 0;
        //自我完美主义维度得分
        int score1 = 0;
        //他人完美主义维度得分
        int score2 = 0;
        //社会完美主义维度得分
        int score3 = 0;
        //完美主义适应性得分
        int score0 = 0;
        //情绪维度得分
        int score4 = 0;
        //认知行为维度得分
        int score5 = 0;
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            if (i == 1 || i == 3 || i == 13 || i == 14 || i == 25){
                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 (i == 9 || i == 10 || i == 11 || i == 18 || i == 23){
                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 (i == 0 || i == 5 || i == 20 || i == 24 || i == 28){
                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 (i == 7 || i == 15 || i == 17 || i == 19 || i == 26){
                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("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;
                }
            }
        }
        List<Map<String,String>> resultMapList = new ArrayList<>();
        score = score1 + score2 + score3;
        score0 =  + score4 + score5;
        //得分列表
        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("完美主义高标准得分", String.valueOf(score));
        resultJson.put("完美主义高标准得分", String.valueOf(score));
        resultMap.put("自我完美主义维度得分", String.valueOf(score1));
        resultJson.put("自我完美主义维度得分", String.valueOf(score1));
        resultMap.put("他人完美主义维度得分", String.valueOf(score2));
        resultJson.put("他人完美主义维度得分", String.valueOf(score2));
        resultMap.put("社会完美主义维度得分", String.valueOf(score3));
        resultJson.put("社会完美主义维度得分", String.valueOf(score3));

        resultMapList.add(resultMap);
        //得分列表
        Map<String, String> resultMap1 = new LinkedHashMap<>();
        resultMap.put("完美主义适应性得分", String.valueOf(score0));
        resultJson.put("完美主义适应性得分", String.valueOf(score0));
        resultMap.put("情绪维度得分", String.valueOf(score4));
        resultJson.put("情绪维度得分", String.valueOf(score4));
        resultMap.put("认知行为维度得分", String.valueOf(score5));
        resultJson.put("认知行为维度得分", String.valueOf(score5));
        resultMapList.add(resultMap1);
        Map<String, String> resultMap2 = new LinkedHashMap<>();
        resultMap2.put("自我疏离感维度得分", String.valueOf(score2));
        resultJson.put("自我疏离感维度得分", String.valueOf(score2));
        resultMapList.add(resultMap2);
        Map<String, String> resultMap3 = new LinkedHashMap<>();
        if (score >= 50 && score0 < 46){
            resultMap3.put("结论", "完美主义适应型");
            resultJson.put("结论", "完美主义适应型");
        }else if (score >= 50 && score0 >= 46){
            resultMap3.put("结论", "完美主义不适应型");
            resultJson.put("结论", "完美主义不适应型");
        }else if (score < 50 && score0 >= 46){
            resultMap3.put("结论", "顺其自然不适应型");
            resultJson.put("结论", "顺其自然不适应型");
        }else if (score < 50 && score0 < 46){
            resultMap3.put("结论", "顺其自然适应型");
            resultJson.put("结论", "顺其自然适应型");
        }

        JSONObject returnJson = new JSONObject(true);
        returnJson.put("resultMapList",resultMapList);
        returnJson.put("scaleName", Constant.SHEET_NAME_MPS);
        returnJson.put("resultJson",resultJson);

        return returnJson;
    }
}
