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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.model.GONOGO2Entity;
import com.rf.psychological.dao.model.GONOGOEntity;
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:Go-No-go冲动抑制测试（高级版）
 * @date 2021/7/20 15:55
 */
public class GONOGO2Cognize extends BaseScale {

    public GONOGO2Cognize(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("rawData");
        List<GONOGO2Entity> dataList = new ArrayList();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            GONOGO2Entity gonogo2Entity = new GONOGO2Entity();
            if (jsonObject1.getInteger("color") == 0) {
                gonogo2Entity.setColor("红色");
            } else {
                gonogo2Entity.setColor("绿色");
            }
            if (jsonObject1.getInteger("arrowDirection") == 0) {
                gonogo2Entity.setArrowDirection("向左");
            } else {
                gonogo2Entity.setArrowDirection("向右");
            }
            if (jsonObject1.getInteger("needResponse") == 0) {
                gonogo2Entity.setNeedResponse("无需反应");
            } else {
                gonogo2Entity.setNeedResponse("需反应");
            }
            if (!jsonObject1.getString("userDirection").equals("")) {
                if (jsonObject1.getInteger("userDirection") == 0) {
                    gonogo2Entity.setUserDirection("向左");
                } else {
                    gonogo2Entity.setUserDirection("向右");
                }
            }
            if (jsonObject1.getBoolean("responseTrue")) {
                gonogo2Entity.setResponseTrue("正确");
            } else {
                gonogo2Entity.setResponseTrue("错误");
            }
            gonogo2Entity.setResponseTime(jsonObject1.getString("responseTime"));
            dataList.add(gonogo2Entity);
        }
        String averageTime = jsonObject.getString("averageTime");
        String correctPercent = jsonObject.getString("correctPercent");

        int goNum = 0;
        double goRightRate = 0;
        int nogoNum = 0;
        double nogoRightRate = 0;
        for (GONOGO2Entity gonogo2Entity : dataList){
            if (gonogo2Entity.getNeedResponse().equals("需反应")){
                goNum ++;
                if (gonogo2Entity.getResponseTrue().equals("正确")){
                    goRightRate ++;
                }
            }
            if (gonogo2Entity.getNeedResponse().equals("无需反应")){
                nogoNum ++;
                if (gonogo2Entity.getResponseTrue().equals("正确")){
                    nogoRightRate ++;
                }
            }
        }
        DecimalFormat df = new DecimalFormat("######0.00");
        goRightRate = Double.parseDouble(df.format((goRightRate*100)/goNum));
        nogoRightRate = Double.parseDouble(df.format((nogoRightRate*100)/nogoNum));
        correctPercent = correctPercent.replace("%","");
        double correct = Double.valueOf(correctPercent);
        correct = Double.parseDouble(df.format(correct));
        //构建返回新的返回结构"
        resultDBJson.put("versionNo","2.0");
        JSONArray totalScores = new JSONArray();
        JSONObject totalScore = new JSONObject(true);
        totalScore.put("totalScore",correct);
        String desc = null;
        if (correct>90){
            desc = "优秀等级：您的冲动抑制能力很好，您在生活中行事沉稳专注，凡事自我克制，面对问题能够审慎思考，工作学习很有规划，很有耐心，也很自律，更加经得住诱惑。";
        }else if(correct>80 && correct<=90){
            desc = "较优秀等级：您的冲动抑制能力比较好，您在生活中行事比较沉稳专注，遇事比较能够自我克制，面对问题一般也能够审慎思考，工作学习有规划，有耐心，也比较自律，一定程度上能经得住诱惑。";
        }else if(correct>60 && correct<=80){
            desc = "一般等级: 您的冲动抑制能力处于一般水平，您在生活中行事有时沉稳专注有时也会分心，遇事有时自我克制有时也有些冲动，工作学习能做到一定程度的自律但有时也会不耐烦。";
        }else if(correct>50 && correct<=60){
            desc = "较差等级：您的冲动抑制能力较差，您在生活中行事比较冲动不耐烦，遇事不能做到自我克制，面对问题也不能审慎思考，工作学习缺乏规划和耐心，比较难做到自律。";
        }else if(correct<=50){
            desc = "极差等级：您的冲动抑制能力很差，您在生活中行事可能经常冲动不耐烦，遇事易受挫、暴躁，工作学习缺乏耐心，无法专注，社会适应能力差，难以抵制诱惑，遇到情绪问题或生活压力很容易陷入烟酒成瘾。建议您后续加强这方面的训练，生活中遇事三思而后行。";
        }
        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("指标值");
        JSONArray columnValue = new JSONArray();
        columnValue.add("总正确率;平均正确反应时;Go反应正确率;Nogo反应正确率");
        columnValue.add(correct+"%"+";"+averageTime+";"+goRightRate+"%"+";"+nogoRightRate+"%");
        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("平均反应时", averageTime);
        resultDBJson.put("平均反应时", averageTime);
        resultMap.put("Go反应正确率", String.valueOf(goRightRate));
        resultDBJson.put("Go反应正确率", String.valueOf(goRightRate));
        resultMap.put("Nogo反应正确率", String.valueOf(nogoRightRate));
        resultDBJson.put("Nogo反应正确率", String.valueOf(nogoRightRate));
        resultMapList = new ArrayList<>();
        resultMapList.add(resultMap);
        fileName = fileName + "-" + Constant.SHEET_NAME_GO_NO_GO2 + ".xlsx";
        name = Constant.SHEET_NAME_GO_NO_GO2;
        String datas = dataList.toString();
        ExcelUtil.createExcelFile(GONOGO2Entity.class, dataList, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_GO_NO_GO2), fileName, Constant.SHEET_NAME_GO_NO_GO2);

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

        return returnJson;
    }


}
