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.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.math.BigDecimal;
import java.math.RoundingMode;
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 ALERTNESSCognize extends BaseScale {

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

        resultMapList = new ArrayList<>();
        //总时长
        int allTime = 0;
        //500ms- 1000ms 次数
        int time500 = 0;
        //1000ms 次数
        int time1000 = 0;
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        List<ObjectEntity> dataList = jsonArray.toJavaList(ObjectEntity.class);
        for (ObjectEntity roa : dataList) {
            int temp = Integer.parseInt(roa.getValue());
            allTime += temp;
            if (temp > 500 && temp < 1000) {
                time500 += 1;
            } else if (temp >= 1000) {
                time1000 += 1;
            }
        }

        //平均时长(ms)
        double averageTime = new BigDecimal((double) allTime / (double) dataList.size()).setScale(2, RoundingMode.HALF_UP).doubleValue();
        //超过0.5秒的比例
        double ratio500 = new BigDecimal((double) time500 / (double) dataList.size()).setScale(2, RoundingMode.HALF_UP).doubleValue();
        //超过1秒的比例
        double ratio1000 = new BigDecimal((double) time1000 / (double) dataList.size()).setScale(2, RoundingMode.HALF_UP).doubleValue();

        String alertness;
        String desc;

        resultDBJson.put("versionNo",Constant.COGNITION_RESULT_VERSION);
        if (averageTime >= 550){
            alertness = "警觉度5级";
            desc = "您的注意力稳定性和专注力很差，不能够在较长时间内将注意力专注于一件事情上。您的视觉反应能力和洞察力的敏锐程度很差，不能很好地觉察情景中危险刺激。建议您后续加强注意稳定性训练，提高反应能力和专注力。";
        }else if(averageTime >= 500 && averageTime < 550){
            alertness = "警觉度4级";
            desc = "您的注意力稳定性和专注力较差，不能够在较长时间内将注意力专注于一件事情上。您的视觉反应能力和洞察力的敏锐程度较差，不能很好地觉察情景中危险刺激。建议您后续加强注意稳定性训练，提高反应能力和专注力。";
        }else if(averageTime >= 450 && averageTime < 500){
            alertness = "警觉度3级";
            desc = "您具有一般水平的注意力稳定性和专注力，能够在较长时间但不能在很长时间内将注意力专注于一件事情上。您的视觉反应能力和洞察力的敏锐程度一般，能够较好但不能很好地觉察情景中危险刺激。";
        }else if(averageTime >= 400 && averageTime < 450){
            alertness = "警觉度2级";
            desc = "您的注意力稳定性和专注力较好，能够在较长的时间内将注意力专注于一件事情上。您的视觉反应能力和洞察力较敏锐，觉察情景中危险刺激的能力较高。";
        }else {
            alertness = "警觉度1级";
            desc = "您的注意力稳定性和专注力非常好，能够在非常长的时间内将注意力专注于一件事情上。您的视觉反应能力和洞察力非常敏锐，觉察情景中危险刺激的能力非常高，在物种生存法则中更易生存下来。";
        }
        //表格数据
        JSONArray tableModule = new JSONArray();
        JSONObject table1 = new JSONObject(true);
        JSONArray columnName = new JSONArray();
        columnName.add("指标名称");
        columnName.add("指标值");
        JSONArray columnValue = new JSONArray();
        columnValue.add("超过0.5秒的比例;超过1秒的比例;平均时长(ms);警觉度等级;解释");
        columnValue.add(ratio500+";"+ratio1000+";"+averageTime+";"+alertness+";"+desc);
        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",false);
        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("超过0.5秒的比例", String.valueOf(new BigDecimal((double) time500 / (double) dataList.size()).setScale(2, RoundingMode.HALF_UP).doubleValue()));
        resultMap.put("超过1秒的比例", String.valueOf(new BigDecimal((double) time1000 / (double) dataList.size()).setScale(2, RoundingMode.HALF_UP).doubleValue()));
        resultMap.put("平均时长(ms)", String.valueOf(new BigDecimal((double) allTime / (double) dataList.size()).setScale(2, RoundingMode.HALF_UP).doubleValue()));
        resultMap.put("警觉度等级", alertness);
        resultMap.put("解释", desc);

        resultDBJson.put("超过0.5秒的比例", String.valueOf(new BigDecimal((double) time500 / (double) dataList.size()).setScale(2, RoundingMode.HALF_UP).doubleValue()));
        resultDBJson.put("超过1秒的比例", String.valueOf(new BigDecimal((double) time1000 / (double) dataList.size()).setScale(2, RoundingMode.HALF_UP).doubleValue()));
        resultDBJson.put("平均时长(ms)", String.valueOf(new BigDecimal((double) allTime / (double) dataList.size()).setScale(2, RoundingMode.HALF_UP).doubleValue()));
        resultDBJson.put("警觉度等级", alertness);
        resultDBJson.put("解释", desc);
        resultMapList.add(resultMap);
        if (Constant.QUEST_TYPE_ALERTNESS.equals(type)){
            fileName = fileName + "-" + Constant.SHEET_NAME_ALERTNESS + ".xlsx";
            name = Constant.SHEET_NAME_ALERTNESS;
        }else if (Constant.QUEST_TYPE_ALERTNESS_CHILD.equals(type)){
            fileName = fileName + "-" + Constant.SHEET_NAME_ALERTNESS_CHILD + ".xlsx";
            name = Constant.SHEET_NAME_ALERTNESS_CHILD;
        }else {
            fileName = fileName + "-" + Constant.SHEET_NAME_ALERTNESS_ADDICTION + ".xlsx";
            name = Constant.SHEET_NAME_ALERTNESS_ADDICTION;
        }

        String datas = jsonArray.toString();
        ExcelUtil.createExcelFile(ObjectEntity.class, dataList, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_ALERTNESS), 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;
    }


}
