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

/**
 * @author zsy
 * @description:3-7岁儿童气质问卷
 * @date 2021/7/20 15:55
 */
public class NYLSScale extends BaseScale{

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

    public  JSONObject scaleCalculate() throws Exception {

        // 自定义路径使用
        /*ScaleUtil.getScaleDll("NYLSScale.dll");
        System.out.println("JSONArray:"+jsonArray);
        System.out.println("resultJson:"+resultJson);
        XScale xScale = new XScale(jsonArray,resultJson);
        String result = xScale.NYLSScale(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;*/

        //活动量得分
        double score1 = 0;
        //规律性得分
        double score2 = 0;
        //趋避性得分
        double score3 = 0;
        //适应度得分
        double score4 = 0;
        //反应强度得分
        double score5 = 0;
        //情绪本质得分
        double score6 = 0;
        //坚持度得分
        double score7 = 0;
        //注意分散度得分
        double score8 = 0;
        //反应阈得分
        double score9 = 0;
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            if (jsonObject1.getString("scoreDirection").equals("1")) {
                if (i == 0 || i == 17 || i == 23 || i == 33 || i == 44 || i == 52 || i == 59 || i == 63){
                    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("checkItems").equals("经常是")) {
                        score1 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score1 += 7;
                    }
                }else if (i == 5 || i == 12 || i == 19 || i == 30 || i == 37 || i == 46 || i == 55 || i == 69){
                    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("checkItems").equals("经常是")) {
                        score2 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score2 += 7;
                    }
                }else if (i == 3 || i == 9 || i == 20 || i == 29 || i == 41 || i == 45 || i == 61 || i == 65){
                    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("checkItems").equals("经常是")) {
                        score3 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score3 += 7;
                    }
                }else if (i == 6 || i == 14 || i == 24 || i == 32 || i == 39 || i == 50 || i == 58 || i == 67){
                    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("checkItems").equals("经常是")) {
                        score4 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score4 += 7;
                    }
                }else if (i == 7 || i == 15 || i == 26 || i == 34|| i == 42 || i == 53 || i == 60 || i == 68){
                    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("经常是")) {
                        score5 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score5 += 7;
                    }
                }else if (i == 1 || i == 13 || i == 18 || i == 28 || i == 40 || i == 49 || i == 55 || i == 64){
                    if (jsonObject1.getString("checkItems").equals("从没发生过")) {
                        score6 += 1;
                    } else if (jsonObject1.getString("checkItems").equals("非常少")) {
                        score6 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("偶尔有一次")) {
                        score6 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("有时")) {
                        score6 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("时常")) {
                        score6 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("经常是")) {
                        score6 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score6 += 7;
                    }
                }else if (i == 4 || i == 11 || i == 21 || i == 27 || i == 36 || i == 47 || i == 57 || i == 71){
                    if (jsonObject1.getString("checkItems").equals("从没发生过")) {
                        score7 += 1;
                    } else if (jsonObject1.getString("checkItems").equals("非常少")) {
                        score7 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("偶尔有一次")) {
                        score7 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("有时")) {
                        score7 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("时常")) {
                        score7 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("经常是")) {
                        score7 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score7 += 7;
                    }
                }else if (i == 8 || i == 16 || i == 25 || i == 35 || i == 43 || i == 51 || i == 62 || i == 70){
                    if (jsonObject1.getString("checkItems").equals("从没发生过")) {
                        score8 += 1;
                    } else if (jsonObject1.getString("checkItems").equals("非常少")) {
                        score8 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("偶尔有一次")) {
                        score8 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("有时")) {
                        score8 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("时常")) {
                        score8 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("经常是")) {
                        score8 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score8 += 7;
                    }
                }else {
                    if (jsonObject1.getString("checkItems").equals("从没发生过")) {
                        score9 += 1;
                    } else if (jsonObject1.getString("checkItems").equals("非常少")) {
                        score9 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("偶尔有一次")) {
                        score9 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("有时")) {
                        score9 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("时常")) {
                        score9 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("经常是")) {
                        score9 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score9 += 7;
                    }
                }
            } else if (jsonObject1.getString("scoreDirection").equals("0")){
                if (i == 0 || i == 17 || i == 23 || i == 33 || i == 44 || i == 52 || i == 59 || i == 63){
                    if (jsonObject1.getString("checkItems").equals("从没发生过")) {
                        score1 += 7;
                    } else if (jsonObject1.getString("checkItems").equals("非常少")) {
                        score1 += 6;
                    } else 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 == 5 || i == 12 || i == 19 || i == 30 || i == 37 || i == 46 || i == 55 || i == 69){
                    if (jsonObject1.getString("checkItems").equals("从没发生过")) {
                        score2 += 7;
                    } else if (jsonObject1.getString("checkItems").equals("非常少")) {
                        score2 += 6;
                    } else 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 == 3 || i == 9 || i == 20 || i == 29 || i == 41 || i == 45 || i == 61 || i == 65){
                    if (jsonObject1.getString("checkItems").equals("从没发生过")) {
                        score3 += 7;
                    } else if (jsonObject1.getString("checkItems").equals("非常少")) {
                        score3 += 6;
                    } else 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 == 6 || i == 14 || i == 24 || i == 32 || i == 39 || i == 50 || i == 58 || i == 67){
                    if (jsonObject1.getString("checkItems").equals("从没发生过")) {
                        score4 += 7;
                    } else if (jsonObject1.getString("checkItems").equals("非常少")) {
                        score4 += 6;
                    } else 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 (i == 7 || i == 15 || i == 26 || i == 34|| i == 42 || i == 53 || i == 60 || i == 68){
                    if (jsonObject1.getString("checkItems").equals("从没发生过")) {
                        score5 += 7;
                    } else if (jsonObject1.getString("checkItems").equals("非常少")) {
                        score5 += 6;
                    } else 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;
                    }
                }else if (i == 1 || i == 13 || i == 18 || i == 28 || i == 40 || i == 49 || i == 55 || i == 64){
                    if (jsonObject1.getString("checkItems").equals("从没发生过")) {
                        score6 += 7;
                    } else if (jsonObject1.getString("checkItems").equals("非常少")) {
                        score6 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("偶尔有一次")) {
                        score6 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("有时")) {
                        score6 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("时常")) {
                        score6 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("经常是")) {
                        score6 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score6 += 1;
                    }
                }else if (i == 4 || i == 11 || i == 21 || i == 27 || i == 36 || i == 47 || i == 57 || i == 71){
                    if (jsonObject1.getString("checkItems").equals("从没发生过")) {
                        score7 += 7;
                    } else if (jsonObject1.getString("checkItems").equals("非常少")) {
                        score7 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("偶尔有一次")) {
                        score7 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("有时")) {
                        score7 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("时常")) {
                        score7 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("经常是")) {
                        score7 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score7 += 1;
                    }
                }else if (i == 8 || i == 16 || i == 25 || i == 35 || i == 43 || i == 51 || i == 62 || i == 70){
                    if (jsonObject1.getString("checkItems").equals("从没发生过")) {
                        score8 += 7;
                    } else if (jsonObject1.getString("checkItems").equals("非常少")) {
                        score8 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("偶尔有一次")) {
                        score8 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("有时")) {
                        score8 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("时常")) {
                        score8 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("经常是")) {
                        score8 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score8 += 1;
                    }
                }else {
                    if (jsonObject1.getString("checkItems").equals("从没发生过")) {
                        score9 += 7;
                    } else if (jsonObject1.getString("checkItems").equals("非常少")) {
                        score9 += 6;
                    } else if (jsonObject1.getString("checkItems").equals("偶尔有一次")) {
                        score9 += 5;
                    } else if (jsonObject1.getString("checkItems").equals("有时")) {
                        score9 += 4;
                    } else if (jsonObject1.getString("checkItems").equals("时常")) {
                        score9 += 3;
                    } else if (jsonObject1.getString("checkItems").equals("经常是")) {
                        score9 += 2;
                    } else if (jsonObject1.getString("checkItems").equals("总是")) {
                        score9 += 1;
                    }
                }
            }
        }
        DecimalFormat df   = new DecimalFormat("######0.00");
        score1 = Double.parseDouble(df.format(score1/8));
        score2 = Double.parseDouble(df.format(score2/8));
        score3 = Double.parseDouble(df.format(score3/8));
        score4 = Double.parseDouble(df.format(score4/8));
        score5 = Double.parseDouble(df.format(score5/8));
        score6 = Double.parseDouble(df.format(score6/8));
        score7 = Double.parseDouble(df.format(score7/8));
        score8 = Double.parseDouble(df.format(score8/8));
        score9 = Double.parseDouble(df.format(score9/8));

        //得分列表

        Map<String, String> resultMap1 = new LinkedHashMap<>();
        resultMap1.put("活动量得分", String.valueOf(score1));
        resultJson.put("活动量得分", String.valueOf(score1));
        //得分列表
        Map<String, String> resultMap2 = new LinkedHashMap<>();
        resultMap2.put("规律性得分", String.valueOf(score2));
        resultJson.put("规律性得分", String.valueOf(score2));
        //得分列表
        Map<String, String> resultMap3 = new LinkedHashMap<>();
        resultMap3.put("趋避性得分", String.valueOf(score3));
        resultJson.put("趋避性得分", String.valueOf(score3));
        Map<String, String> resultMap4 = new LinkedHashMap<>();
        resultMap4.put("适应度得分", String.valueOf(score4));
        resultJson.put("适应度得分", String.valueOf(score4));
        Map<String, String> resultMap5 = new LinkedHashMap<>();
        resultMap5.put("反应强度得分", String.valueOf(score5));
        resultJson.put("反应强度得分", String.valueOf(score5));
        Map<String, String> resultMap6 = new LinkedHashMap<>();
        resultMap6.put("情绪本质得分", String.valueOf(score6));
        resultJson.put("情绪本质得分", String.valueOf(score6));
        Map<String, String> resultMap7 = new LinkedHashMap<>();
        resultMap7.put("坚持度得分", String.valueOf(score7));
        resultJson.put("坚持度得分", String.valueOf(score7));
        Map<String, String> resultMap8 = new LinkedHashMap<>();
        resultMap8.put("注意分散度得分", String.valueOf(score8));
        resultJson.put("注意分散度得分", String.valueOf(score8));
        Map<String, String> resultMap9 = new LinkedHashMap<>();
        resultMap9.put("反应阈得分", String.valueOf(score9));
        resultJson.put("反应阈得分", String.valueOf(score9));

        List<Map<String,String>> resultMapList = new ArrayList<>();
        resultMapList.add(resultMap1);
        resultMapList.add(resultMap2);
        resultMapList.add(resultMap3);
        resultMapList.add(resultMap4);
        resultMapList.add(resultMap5);
        resultMapList.add(resultMap6);
        resultMapList.add(resultMap7);
        resultMapList.add(resultMap8);
        resultMapList.add(resultMap9);

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

        resultJson.put("测试结果指导", "此结果分为四类，请按照自己情况查看对应结果");
        resultJson.put("结论", "难养型：1.节律性、趋避性、适应性、情绪本质至少三项低于平均值；2.反应强度高于平均值；3.五项中至少两项偏离出一个标准差。" +
                "易养型：1.如果反应强度高于平均值，则其它四项中最多有一项低于平均值；如果反应强度不高于平均值，则其它四项中最多有两项低于平均值；2.没有任何一项偏离出一个标准差。" +
                "启动缓慢型：1.五项中至少三项得分低于平均值且趋避性或适应性有一项低于一个标准差；2.活动量得分不可高于二分之一个标准差；3.情绪本质得分不可低于一个标准差。" +
                "其余为中间型");
        return returnJson;
    }
}
