package com.rf.psychological.scale.resultBusiness.scaleResult;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.dto.scale.NewResultDto;
import com.rf.psychological.dao.model.DimensionEntity;
import com.rf.psychological.scale.dao.model.ScaleMarksEntity;
import com.rf.psychological.scale.scaleresult.ScaleConstant;
import com.rf.psychological.utils.Constant;

import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.rf.psychological.scale.resultBusiness.scaleResult.NEWCOMMONScale.commonComputeDimensionScore;

/**
 * @author zsy
 * @description:气质类型测试
 * @date 2021/7/20 15:55
 */
public class TTTScale extends BaseScale{

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

    public  JSONObject scaleCalculate() throws Exception {

        // 自定义路径使用
        /*ScaleUtil.getScaleDll("TTTSCALE.dll");
        System.out.println("JSONArray:"+jsonArray);
        System.out.println("resultJson:"+resultJson);
        XScale xScale = new XScale(jsonArray,resultJson);
        String result = xScale.TTTScale(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 score1 = 0;
        //多血质得分
        int score2 = 0;
        //粘液质得分
        int score3 = 0;
        //抑郁质得分
        int score4 = 0;
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            if (i == 1 || i == 5 || i == 8 || i == 13 || i == 16 || i == 20 || i == 26 || i == 30 || i == 35 || i == 37 || i == 41 || i == 47 || i ==49 || i == 53 || i == 57){
                if (jsonObject1.getString("checkItems").equals("完全一致")) {
                    score1 += 2;
                } else if (jsonObject1.getString("checkItems").equals("比较一致")) {
                    score1 += 1;
                } else if (jsonObject1.getString("checkItems").equals("不太一致")) {
                    score1 -= 1;
                } else if (jsonObject1.getString("checkItems").equals("很不一致")) {
                    score1 -= 2;
                }
            }else if (i == 3 || i == 7 || i == 10 || i == 15 || i == 18 || i == 22 || i == 24 || i == 28 || i == 33 || i == 39 || i == 43 || i == 45 || i ==51 || i == 55 || i == 59){
                if (jsonObject1.getString("checkItems").equals("完全一致")) {
                    score2 += 2;
                } else if (jsonObject1.getString("checkItems").equals("比较一致")) {
                    score2 += 1;
                } else if (jsonObject1.getString("checkItems").equals("不太一致")) {
                    score2 -= 1;
                } else if (jsonObject1.getString("checkItems").equals("很不一致")) {
                    score2 -= 2;
                }
            }else if (i == 0 || i == 6 || i == 9 || i == 12 || i == 17 || i == 21 || i == 25 || i == 29 || i == 32 || i == 38 || i == 42 || i == 44 || i ==48 || i == 54 || i == 56){
                if (jsonObject1.getString("checkItems").equals("完全一致")) {
                    score3 += 2;
                } else if (jsonObject1.getString("checkItems").equals("比较一致")) {
                    score3 += 1;
                } else if (jsonObject1.getString("checkItems").equals("不太一致")) {
                    score3 -= 1;
                } else if (jsonObject1.getString("checkItems").equals("很不一致")) {
                    score3 -= 2;
                }
            }else {
                if (jsonObject1.getString("checkItems").equals("完全一致")) {
                    score4 += 2;
                } else if (jsonObject1.getString("checkItems").equals("比较一致")) {
                    score4 += 1;
                } else if (jsonObject1.getString("checkItems").equals("不太一致")) {
                    score4 -= 1;
                } else if (jsonObject1.getString("checkItems").equals("很不一致")) {
                    score4 -= 2;
                }
            }
        }

        //得分列表
        /*Map<String, String> resultMap = new LinkedHashMap<>();
        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));
        resultMap.put("抑郁质得分", String.valueOf(score4));
        resultJson.put("抑郁质得分", String.valueOf(score4));

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

        Map<String, String> resultMap0 = new LinkedHashMap<>();
        List<Map<String, String>> resultMapList = new ArrayList<>();
        //返回值以及存数据库格式
        List<NewResultDto> newResultDtos = new ArrayList<>();
        //获取维度信息列表
        List<DimensionEntity> dimensionEntities = (List<DimensionEntity>) resultJson.get("dimensionEntities");
        //获取评分规则列表
        List<ScaleMarksEntity> scaleMarksEntities = (List<ScaleMarksEntity>) resultJson.get("scaleMarksEntities");
        //新版本数据格式
        Map<String, Object> newResult = new LinkedHashMap<>();
        Map<String,Double> dimensionScoreMap = new HashMap<>();

        //判断最高分
        int a;
        if (score1 >= score2 && score1 >= score3 && score1 >= score4) {
            a = score1;
        } else if (score2 >= score1 && score2 >= score3 && score2 >= score4) {
            a = score2;
        } else if (score3 >= score1 && score3 >= score2 && score3 >= score4) {
            a = score3;
        } else {
            a = score4;
        }
        JSONObject jsonObject = new JSONObject(true);
        if (score1 == a){
            jsonObject.put("胆汁质",a);
        }
        if (score2 == a){
            jsonObject.put("多血质",a);
        }
        if (score3 == a){
            jsonObject.put("粘液质",a);
        }
        if (score4 == a){
            jsonObject.put("抑郁质",a);
        }
        Set<String> keys = jsonObject.keySet();
        double[] dimensionScore = new double[keys.size()];
        List<DimensionEntity> dimensionEntitieList = new ArrayList<>();
        int b = 0;
        for (String key : keys) {
            for (DimensionEntity dimensionEntity : dimensionEntities) {
                if (dimensionEntity.getName().equals(key)){
                    dimensionEntitieList.add(dimensionEntity);
                    dimensionScore[b] = jsonObject.getInteger(key);
                    b++;
                }
            }
        }
        DecimalFormat df = new DecimalFormat("######0.00");

        //int totalScore = score1 + score2 + score3 + score4;
        //resultMap0.put("总分", String.valueOf(totalScore));
        //resultJson.put("总分", String.valueOf(totalScore));


        //将量表总维度根据维度名称进行分组
        Map<String, List<ScaleMarksEntity>> scaleMarksMap = scaleMarksEntities.stream().collect(Collectors.groupingBy(ScaleMarksEntity::getName));
        JSONObject iconInfo = new JSONObject();
        //雷达图需要的维度以及最大值
        List<Map<String, Object>> indicator = new LinkedList<>();
        //雷达图所需要的常模参考值
        LinkedList<String> reference = new LinkedList<>();
        //雷达图需要的分数
        LinkedList<String> scoreList = new LinkedList<>();
        //计算维度所在评分区间
        commonComputeDimensionScore(df, resultMap0, resultJson, dimensionEntitieList, dimensionScore, scaleMarksMap, newResultDtos, indicator, reference, scoreList);
        //需要图表展示的
        iconInfo.put("indicator", indicator);
        //判断是否有常模参考值，即reference的内容如果全为0，则不进行返回
        if (!reference.stream().allMatch("0"::equals)) {
            iconInfo.put("reference", reference);
        }
        iconInfo.put("scoreList", scoreList);
        if (indicator.size() == 0 && reference.size() == 0) {
            newResult.put("iconInfo", "");
        } else {
            newResult.put("iconInfo", iconInfo);
        }

        dimensionScoreMap.put("8af178857b94d581017b94f8727a1b20", Double.valueOf(score1));
        dimensionScoreMap.put("8af178857b94d581017b94f872d01b21", Double.valueOf(score2));
        dimensionScoreMap.put("8af178857b94d581017b94f873251b22", Double.valueOf(score3));
        dimensionScoreMap.put("8af178857b94d581017b94f8737a1b23", Double.valueOf(score4));
        resultJson.put("dimensionScoreMap",dimensionScoreMap);

        newResult.put("result", newResultDtos);
        resultMapList.add(resultMap0);
        JSONObject returnJson = new JSONObject(true);
        returnJson.put("resultMapList", resultMapList);
        returnJson.put("resultJson", resultJson);
        returnJson.put(ScaleConstant.ResultEnum.RESULT_NEW_FIELD.getKeyword(), newResult);

        return returnJson;
    }
}
