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

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

    public MTTCHILDCognize(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 gtctEntitieList = jsonObject.getJSONArray("userRecords");
        List<GTCTEntity> gtctEntities = gtctEntitieList.toJavaList(GTCTEntity.class);
        //用户测试结果
        JSONArray userResult = jsonObject.getJSONArray("indexList");
        fileName = resultJson.getString("fileName") + "-" + Constant.SHEET_NAME_MTT_CHILD + ".xlsx";
        name = Constant.SHEET_NAME_MTT_CHILD;
        double score = 0;
        JSONArray columnValue = new JSONArray();
        String key = "";
        String value = "";
        for (int i = 0; i < userResult.size(); i++) {
            JSONObject jsonObject1 = userResult.getJSONObject(i);
            score += Double.valueOf(jsonObject1.getString("raghtRate").replace("%",""));
            Map<String, String> resultMap = new LinkedHashMap<>();
            resultMap.put("难度等级", String.valueOf(jsonObject1.getInteger("diff")));
            resultDBJson.put((i + 1) + "难度等级", String.valueOf(jsonObject1.getInteger("diff")));
            resultMap.put("正确率", String.valueOf(jsonObject1.getString("raghtRate")));
            resultDBJson.put((i + 1) + "难度等级正确率", String.valueOf(jsonObject1.getString("raghtRate")));
            if ((i + 1) == userResult.size()) {
                key += (i + 1) + "难度等级正确率";
                value += String.valueOf(jsonObject1.getString("raghtRate"));
            }else {
                key += (i + 1) + "难度等级正确率;";
                value += String.valueOf(jsonObject1.getString("raghtRate")) + ";";
            }
            resultMapList.add(resultMap);
        }
        DecimalFormat df = new DecimalFormat("######0.00");
        score = Double.parseDouble(df.format(score/userResult.size()));

        resultDBJson.put("versionNo",Constant.COGNITION_RESULT_VERSION);
        JSONArray totalScores = new JSONArray();
        JSONObject totalScore = new JSONObject(true);
        String desc = null;
        totalScore.put("totalScore",score);
        if (score>90){
            desc = "优秀等级：您具有很好的注意分配、视觉定向能力、眼动跟随和追踪物体的协调能力、专注力以及阅读能力。比如，您阅读时会很快很准，不会出现漏字或看错字，您在学习或做一件事情时很专注和沉浸其中。";
        }else if(score>80 && score<=90){
            desc = "较优秀等级：您具有较好的注意分配、视觉定向能力、眼动跟随和追踪物体的协调能力、专注力以及阅读能力。比如，您能较快较准地阅读，不太会出现漏字或看错字，您在学习或做一件事情时能够较专注和沉浸其中。";
        }else if(score>70 && score<=80){
            desc = "一般等级：您具有一般水平的注意分配、视觉定向能力、眼动跟随和追踪物体的协调能力、专注力以及阅读能力。比如，您阅读文字时不会太快太准，但也不会出现太多的漏字或看错字的情况，您学习或做一件事情的专注度和投入程度处于一般水平，能够在一定程度上沉浸在当下所做的事情上。";
        }else if(score>60 && score<=70){
            desc = "较差等级：您的注意分配、视觉定向能力、眼动跟随和追踪物体的协调能力、专注力以及阅读能力较差。比如，您在阅读时会较慢，且漏字和看错字情况较多，您不能很好地沉浸在学习或当下所做的事情中。";
        }else if(score<=60){
            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("指标值");
        columnValue.add(key);
        columnValue.add(value);
        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);

        String datas = gtctEntitieList.toString();
        ExcelUtil.createExcelFile(GTCTEntity.class, gtctEntities, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_GTCT), 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;
    }


}
