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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.model.SCEntity;
import com.rf.psychological.dao.model.SHAPE2Entity;
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 SCCognize extends BaseScale {

    public SCCognize(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("results");
        List<SCEntity> scEntityList = jsonArray.toJavaList(SCEntity.class);
        int num = 0;
        int errorNum = 0;
        int diff1 = 0;
        int diff2 = 0;
        int diff3= 0;
        int diff4 = 0;
        int diff5 = 0;
        int diff6 = 0;
        int diff7 = 0;
        int diff8 = 0;
        int diff9 = 0;
        for (SCEntity scEntity : scEntityList) {
            if (!scEntity.getError().equals("NULL")) {
                num += 1;
                if (scEntity.getSpeed() == 1){
                    if (scEntity.getLength() == 100){
                        if (Integer.valueOf(scEntity.getError()) < 0) {
                            diff1 -= Integer.valueOf(scEntity.getError());
                        } else {
                            diff1 += Integer.valueOf(scEntity.getError());
                        }
                    }else if (scEntity.getLength() == 150){
                        if (Integer.valueOf(scEntity.getError()) < 0) {
                            diff2 -= Integer.valueOf(scEntity.getError());
                        } else {
                            diff2 += Integer.valueOf(scEntity.getError());
                        }
                    }else {
                        if (Integer.valueOf(scEntity.getError()) < 0) {
                            diff3 -= Integer.valueOf(scEntity.getError());
                        } else {
                            diff3 += Integer.valueOf(scEntity.getError());
                        }
                    }
                }else if (scEntity.getSpeed() == 2){
                    if (scEntity.getLength() == 100){
                        if (Integer.valueOf(scEntity.getError()) < 0) {
                            diff4 -= Integer.valueOf(scEntity.getError());
                        } else {
                            diff4 += Integer.valueOf(scEntity.getError());
                        }
                    }else if (scEntity.getLength() == 150){
                        if (Integer.valueOf(scEntity.getError()) < 0) {
                            diff5 -= Integer.valueOf(scEntity.getError());
                        } else {
                            diff5 += Integer.valueOf(scEntity.getError());
                        }
                    }else {
                        if (Integer.valueOf(scEntity.getError()) < 0) {
                            diff6 -= Integer.valueOf(scEntity.getError());
                        } else {
                            diff6 += Integer.valueOf(scEntity.getError());
                        }
                    }
                }else {
                    if (scEntity.getLength() == 100){
                        if (Integer.valueOf(scEntity.getError()) < 0) {
                            diff7 -= Integer.valueOf(scEntity.getError());
                        } else {
                            diff7 += Integer.valueOf(scEntity.getError());
                        }
                    }else if (scEntity.getLength() == 150){
                        if (Integer.valueOf(scEntity.getError()) < 0) {
                            diff8 -= Integer.valueOf(scEntity.getError());
                        } else {
                            diff8 += Integer.valueOf(scEntity.getError());
                        }
                    }else {
                        if (Integer.valueOf(scEntity.getError()) < 0) {
                            diff9 -= Integer.valueOf(scEntity.getError());
                        } else {
                            diff9 += Integer.valueOf(scEntity.getError());
                        }
                    }
                }
                if (Integer.valueOf(scEntity.getError()) < 0) {
                    errorNum -= Integer.valueOf(scEntity.getError());
                } else {
                    errorNum += Integer.valueOf(scEntity.getError());
                }
            }
        }
        DecimalFormat df = new DecimalFormat("######0.00");
        double correct = 0;
        if (num != 0){
            correct = Double.parseDouble(df.format(errorNum / num));
        }

        resultDBJson.put("versionNo",Constant.COGNITION_RESULT_VERSION);
        JSONArray totalScores = new JSONArray();
        JSONObject totalScore = new JSONObject(true);
        String desc = null;
        totalScore.put("totalScore",correct);

        if (correct>500){
            desc = "极差等级:您天生时间知觉敏锐程度很差，几乎不能感知自己的生理时钟，在生活工作中也几乎不能把握时间节奏，比如时间一点点过去您并没有觉察，时间感很差，生活缺乏计划，该睡觉的时候可能在打游戏或者做其他的事，该吃饭的时候也不吃饭。您很需要加强时间感知力训练。\n";
        }else if(correct>400 && correct<=500){
            desc = "较差等级:您天生时间知觉敏锐程度较差，不能够很好地感知自己的生理时钟，在生活工作中也不能很好地把握时间节奏，比如有时候时间不知不觉过去很久自己也不知道怎么回事，时间感较差，生活缺乏计划，可能需要加强时间感训练。\n";
        }else if(correct>200 && correct<=400){
            desc = "一般等级:您天生时间知觉敏锐程度一般，对自己生理时钟的感知能力也处在一般水平，在生活工作中不能够很好但能够较好地把握时间节奏。比如，每天来回上班地点的行程时间，您内心会大概有个计算。\n";
        }else if(correct>100 && correct<=200){
            desc = "较优秀等级:您在一定程度上忠于自己的生理时钟，属于天生时间知觉比较敏锐的人，在生活工作中对时间节奏把握得比较准确。比如，您平常需要吃早餐的时间到了，您可能会感到饿并且要吃点东西。每天来回上班地点的行程时间，您内心也能大概较好地计算。\n";
        }else if(correct<=100){
            desc = "优秀等级:您很忠于自己的生理时钟，属于天生时间知觉相当敏锐的人，在生活工作中对时间节奏把握得很好很准确。比如，在您平常需要吃早餐的时间，您就会感到饿。每天来回上班地点的行程时间，您内心也会计算得很好。\n";
        }
        totalScore.put("totalScoreDesc",desc);
        totalScores.add(totalScore);
        resultDBJson.put("totalScore",totalScores);
        //表格数据
        JSONArray tableModule = new JSONArray();
        JSONObject table1 = new JSONObject(true);
        JSONArray columnName = new JSONArray();
        columnName.add("指标名称");
        columnName.add("指标值(ms)");
        JSONArray columnValue = new JSONArray();
        columnValue.add("平均反应误差;难度一误差;难度二误差;难度三误差;难度四误差;难度五误差;难度六误差;难度七误差;难度八误差;难度九误差");
        columnValue.add(correct +";"+diff1 +";"+diff2 +";"+diff3 +";"+diff4 +";"+diff5 +";"+diff6 +";"+diff7 +";"+diff8 +";"+diff9);
        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("平均反应误差", String.valueOf(correct));
        resultDBJson.put("平均反应误差", String.valueOf(correct));
        resultMap.put("难度一误差", String.valueOf(diff1));
        resultDBJson.put("难度一误差", String.valueOf(diff1));
        resultMap.put("难度二误差", String.valueOf(diff2));
        resultDBJson.put("难度二误差", String.valueOf(diff2));
        resultMap.put("难度三误差", String.valueOf(diff3));
        resultDBJson.put("难度三误差", String.valueOf(diff3));
        resultMap.put("难度四误差", String.valueOf(diff4));
        resultDBJson.put("难度四误差", String.valueOf(diff4));
        resultMap.put("难度五误差", String.valueOf(diff5));
        resultDBJson.put("难度五误差", String.valueOf(diff5));
        resultMap.put("难度六误差", String.valueOf(diff6));
        resultDBJson.put("难度六误差", String.valueOf(diff6));
        resultMap.put("难度七误差", String.valueOf(diff7));
        resultDBJson.put("难度七误差", String.valueOf(diff7));
        resultMap.put("难度八误差", String.valueOf(diff8));
        resultDBJson.put("难度八误差", String.valueOf(diff8));
        resultMap.put("难度九误差", String.valueOf(diff9));
        resultDBJson.put("难度九误差", String.valueOf(diff9));
        resultMapList = new ArrayList<>();
        resultMapList.add(resultMap);
        fileName = fileName + "-" + Constant.SHEET_NAME_SC + ".xlsx";
        name = Constant.SHEET_NAME_SC;
        String datas = jsonArray.toString();
        ExcelUtil.createExcelFile(SCEntity.class, scEntityList, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_SC), fileName, Constant.SHEET_NAME_SC);
        //枪林弹雨

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

        return returnJson;
    }


}
