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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.model.STTEntity;
import com.rf.psychological.dao.model.XiongYingZhanChiEntity;
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.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zsy
 * @description:STTA STTB
 * @date 2021/7/20 15:55
 */
public class STTACognize extends BaseScale {

    public STTACognize(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;
        String type = resultJson.getString("type");

        JSONObject resultDBJson = new JSONObject(true);

        String exerciseTime = jsonObject.getString("exerciseTime");
        String realTime = jsonObject.getString("realTime");
        String totalTime = jsonObject.getString("totalTime");

        if ("NULL".equals(exerciseTime) || "NULL".equals(realTime) || "NULL".equals(totalTime)) {
            exerciseTime = "测试不通过";
            realTime = "测试不通过";
            totalTime = "测试不通过";
        }

        STTEntity sttEntity = new STTEntity(exerciseTime, realTime, totalTime);
        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("总反应时间", totalTime);
        resultMap.put("练习测试时间", exerciseTime);
        resultMap.put("正式测试时间", realTime);
        resultDBJson.put("总反应时间", totalTime);
        resultDBJson.put("练习测试时间", exerciseTime);
        resultDBJson.put("正式测试时间", realTime);
        resultMapList.add(resultMap);

        resultDBJson.put("versionNo",Constant.COGNITION_RESULT_VERSION);
        JSONArray totalScores = new JSONArray();
        JSONObject totalScore = new JSONObject(true);
        double correct = Double.valueOf(totalTime);
        String description = null;
        totalScore.put("totalScore",correct);

        ArrayList<STTEntity> sttEntities = new ArrayList<>();
        sttEntities.add(sttEntity);
        if (Constant.QUEST_TYPE_STTA.equals(type)) {
            if (correct > 60){
                description = "您的视觉搜索能力、信息处理速度和灵活性较差。";
            }else if(correct > 35 && correct <= 60){
                description = "您的视觉搜索能力在一般水平，信息处理速度和灵活性中等水平。";
            }else if(correct <= 35){
                description = "您的视觉搜索能力很好，信息处理速度很快，灵活性很强。";
            }
            fileName = fileName + "-" + Constant.SHEET_NAME_STTA + ".xlsx";
            name = Constant.SHEET_NAME_STTA;
            ExcelUtil.createExcelFile(STTEntity.class, sttEntities, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_STTA), fileName, Constant.SHEET_NAME_STTA);
        } else {
            if (correct > 200){
                description = "您的视觉搜索能力较差，信息处理速度和灵活性较差。";
            }else if(correct > 100 && correct <= 200){
                description = "您的视觉搜索能力在一般水平，信息处理速度和灵活性中等水平。";
            }else if(correct <= 100){
                description = "您的视觉搜索能力很好，信息处理速度很快，灵活性很强。";
            }
            fileName = fileName + "-" + Constant.SHEET_NAME_STTB + ".xlsx";
            name = Constant.SHEET_NAME_STTB;
            ExcelUtil.createExcelFile(STTEntity.class, sttEntities, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_STTB), fileName, Constant.SHEET_NAME_STTB);
        }
        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(totalTime+";"+exerciseTime+";"+realTime);
        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);

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

        return returnJson;
    }


}
