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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.model.ObjectEntity;
import com.rf.psychological.dao.model.SHAPE2Entity;
import com.rf.psychological.dao.model.STTEntity;
import com.rf.psychological.file.excel.ExcelClass;
import com.rf.psychological.scale.resultBusiness.scaleResult.BaseScale;
import com.rf.psychological.utils.Constant;
import com.rf.psychological.utils.ExcelUtil;

import java.text.DecimalFormat;
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 ADDITIONCognize extends BaseScale {

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

    public  JSONObject scaleCalculate() throws Exception {
        List<Map<String, String>> resultMapList = new ArrayList<>();
        JSONObject jsonObject = resultJson.getJSONObject("jsonObject");
        String fileName = resultJson.getString("fileName");
        String name;

        JSONObject resultDBJson = new JSONObject(true);

        JSONArray jsonArray = jsonObject.getJSONArray("data");
        //测试者完成的题目总数
        int totalNum = 0;
        //总正确数
        int rightNum = 0;
        //得分之和
        int totalScoreTemp = 0;
        //最终得分
        String score = "";
        //double score = 0;
        if (jsonObject.containsKey("total")) {
            totalNum = jsonObject.getInteger("total");
        }
        if (jsonObject.containsKey("score")) {
            score = jsonObject.getString("score");
            //score = jsonObject.getInteger("score");
        }
        List<ObjectEntity> dataList = jsonArray.toJavaList(ObjectEntity.class);
        if (dataList != null && dataList.size() > 0) {

            for (ObjectEntity object : dataList) {
                rightNum = Integer.parseInt(object.getValue()) + rightNum;
                //totalScoreTemp = totalScoreTemp + Utils.x_n(2,Integer.parseInt(object.getKey())-2) * Integer.parseInt(object.getValue());
            }
            //score = Utils.loga_n(2,totalScoreTemp,2);
        }

        resultDBJson.put("versionNo",Constant.COGNITION_RESULT_VERSION);
        JSONArray totalScores = new JSONArray();
        JSONObject totalScore = new JSONObject(true);
        double correct = Double.valueOf(score);
        String description = null;
        totalScore.put("totalScore",correct);
        if (correct > 8){
            description = "您的运算、心算能力很好，思维灵活，大脑处理信息能力强，工作记忆容量大。";
        }else if(correct > 6 && correct <= 8){
            description = "您的运算能力、心算能力正常，处于中等水平，大脑信息加工能力和工作记忆容量在中等水平。";
        }else if(correct <= 6){
            description = "您的运算、心算能力较差，大脑工作记忆的信息加工容量有限，建议加强心算训练。";
        }
        totalScore.put("totalScoreDesc",description);
        totalScores.add(totalScore);
        resultDBJson.put("totalScore",totalScores);
        //表格数据
        JSONArray tableModule = new JSONArray();
        JSONObject table1 = new JSONObject(true);
        JSONArray columnName = new JSONArray();
        columnName.add("指标名称");
        columnName.add("指标值");
        JSONArray columnValue = new JSONArray();
        columnValue.add("总分;题目总数;总正确数");
        columnValue.add(score+";"+totalNum+";"+rightNum);
        table1.put("tableName","table1");
        table1.put("columnName",columnName);
        table1.put("columnValue",columnValue);
        tableModule.add(table1);
        resultDBJson.put("tableModule", tableModule);

        JSONObject ifShow = new JSONObject(true);
        ifShow.put("totalScore",true);
        ifShow.put("dimensions",false);
        ifShow.put("note",false);
        ifShow.put("radar",false);
        ifShow.put("table",true);
        resultDBJson.put("ifShow",ifShow);


        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("总分", score);
        resultMap.put("题目总数", String.valueOf(totalNum));
        resultMap.put("总正确数", String.valueOf(rightNum));

        resultDBJson.put("总分", score);
        resultDBJson.put("题目总数", String.valueOf(totalNum));
        resultDBJson.put("总正确数", String.valueOf(rightNum));

        fileName = fileName + "-" + Constant.SHEET_NAME_ADDITION + ".xlsx";
        name = Constant.SHEET_NAME_ADDITION;
        //System.out.println(totalScoreTemp);
        resultMapList = new ArrayList<>();
        resultMapList.add(resultMap);
        String datas = jsonArray.toString();
        ExcelUtil.createExcelFile(ObjectEntity.class, dataList, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_ADDITION), fileName, Constant.SHEET_NAME_ADDITION);

        JSONObject returnJson = new JSONObject(true);
        returnJson.put("fileName",fileName);
        returnJson.put("name", name);
        returnJson.put("resultJson",resultDBJson);
        returnJson.put("datas",datas);

        return returnJson;
    }


}
