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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.cognizeModel.FDOTEntity;
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:情绪测试3：点探测（成瘾版）
 * @date 2021/7/20 15:55
 */
public class FACEDOTADDICTIONCognize extends BaseScale {

    public FACEDOTADDICTIONCognize(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);

        List<FDOTEntity> block = jsonObject.getJSONArray("data").toJavaList(FDOTEntity.class);
        JSONObject result = jsonObject.getJSONObject("result");

        //总正确数
        String rightTotal = result.getString("rightTotal");
        //积极正确数
        String relevantTotal = result.getString("relevantTotal");
        //消极正确数
        String irrelevantTotal = result.getString("irrelevantTotal");
        //总反应时
        String responseTimeTotal = result.getString("responseTimeTotal");
        //积极总反应时
        String relevantTime = result.getString("relevantTime");
        //消极总反应时
        String irrelevantTime = result.getString("irrelevantTime");
        //总平均反应时
        String responseAverageTimeTotal = result.getString("responseAverageTimeTotal");
        //积极平均反应时
        String relevantAverageTime = result.getString("relevantAverageTime");
        //消极平均反应时
        String irrelevantAverageTime = result.getString("irrelevantAverageTime");
        //总正确率
        String rightRate = result.getString("rightRate");
        //积极正确率
        String relevantRate = result.getString("relevantRate");
        //消极正确率
        String irrelevantRate = result.getString("irrelevantRate");

        resultDBJson.put("versionNo",Constant.COGNITION_RESULT_VERSION);
        JSONArray totalScores = new JSONArray();
        JSONObject totalScore = new JSONObject(true);
        String desc = null;
        totalScore.put("totalScore",rightRate.replace("%",""));
        double k = Double.valueOf(irrelevantAverageTime) - Double.valueOf(relevantAverageTime);
        if (k >= 0){
            desc = "没有焦虑情绪和情绪障碍";
        }else if(k >= -200 && k < 0){
            desc = "可能存在焦虑情绪和情绪障碍";
        }else if(k >= -500 && k < -200){
            desc = "存在一定的焦虑情绪或情绪障碍";
        }else if(k < -500){
            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("总正确数;积极正确数;消极正确数;总反应时;积极总反应时;消极总反应时;总平均反应时;积极平均反应时;消极平均反应时;积极正确率;消极正确率");
        columnValue.add(rightTotal+";"+relevantTotal+";"+irrelevantTotal+";"+responseTimeTotal+";"+relevantTime+";"+irrelevantTime+";"
                +responseAverageTimeTotal+";"+relevantAverageTime+";"+irrelevantAverageTime+";"+relevantRate+";"+irrelevantRate);
        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("总正确数", result.getString("rightTotal"));
        resultMap.put("积极正确数", result.getString("relevantTotal"));
        resultMap.put("消极正确数", result.getString("irrelevantTotal"));
        resultMap.put("总反应时", result.getString("responseTimeTotal"));
        resultMap.put("积极总反应时", result.getString("relevantTime"));
        resultMap.put("消极总反应时", result.getString("irrelevantTime"));
        resultMap.put("总平均反应时", result.getString("responseAverageTimeTotal"));
        resultMap.put("积极平均反应时", result.getString("relevantAverageTime"));
        resultMap.put("消极平均反应时", result.getString("irrelevantAverageTime"));
        resultMap.put("总正确率", result.getString("rightRate"));
        resultMap.put("积极正确率", result.getString("relevantRate"));
        resultMap.put("消极正确率", result.getString("irrelevantRate"));

        resultDBJson.put("总正确数", result.getString("rightTotal"));
        resultDBJson.put("积极正确数", result.getString("relevantTotal"));
        resultDBJson.put("消极正确数", result.getString("irrelevantTotal"));
        resultDBJson.put("总反应时", result.getString("responseTimeTotal"));
        resultDBJson.put("积极总反应时", result.getString("relevantTime"));
        resultDBJson.put("消极总反应时", result.getString("irrelevantTime"));
        resultDBJson.put("总平均反应时", result.getString("responseAverageTimeTotal"));
        resultDBJson.put("积极平均反应时", result.getString("relevantAverageTime"));
        resultDBJson.put("消极平均反应时", result.getString("irrelevantAverageTime"));
        resultDBJson.put("总正确率", result.getString("rightRate"));
        resultDBJson.put("积极正确率", result.getString("relevantRate"));
        resultDBJson.put("消极正确率", result.getString("irrelevantRate"));

        resultMapList = new ArrayList<>();
        resultMapList.add(resultMap);


        fileName = fileName + "-" + Constant.SHEET_NAME_FACEDOT_ADDICTION + ".xlsx";
        name = Constant.SHEET_NAME_FACEDOT_ADDICTION;
        String datas = jsonObject.getJSONArray("data").toString();
        ExcelUtil.createExcelFile(FDOTEntity.class, block, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_FDOT), fileName, name);

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

        return returnJson;
    }


}
