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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.model.ANTEntity;
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:ANT儿童版
 * @date 2021/7/20 15:55
 */
public class ANTFISHCognize extends BaseScale {

    public ANTFISHCognize(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;
        String name;

        JSONObject resultDBJson = new JSONObject(true);

        JSONArray jsonArray = jsonObject.getJSONArray("data");
        //准确率
        String rightRate = jsonObject.getString("rightRate");
        //总反应时长
        String totalResponseTime = jsonObject.getString("totalResponseTime");
        JSONObject result = jsonObject.getJSONObject("result");
        fileName = resultJson.getString("fileName") + "-" + Constant.SHEET_NAME_ANT_FISH + ".xlsx";
        name = Constant.SHEET_NAME_ANT_FISH;

        //专业报告处理
        resultDBJson.put("versionNo",Constant.COGNITION_RESULT_VERSION);

        JSONObject ifShow = new JSONObject(true);
        ifShow.put("totalScore",true);
        ifShow.put("dimensions",true);
        ifShow.put("note",false);
        ifShow.put("radar",false);
        ifShow.put("table",true);
        resultDBJson.put("ifShow",ifShow);

        //总得分
        JSONArray totalScores = new JSONArray();
        JSONObject totalScore = new JSONObject(true);
        String desc = null;
        String hitRate = String.valueOf(result.get("hitRate"));
        double accur = Double.valueOf(hitRate);
        if (accur>0.8){
            desc = "您的注意警觉、定向和执行控制能力很好。";
        }else if(accur>0.75 && accur<=0.8){
            desc = "您的注意警觉、定向和执行控制能力较好。";
        }else if(accur>0.7 && accur<=0.75){
            desc = "您的注意警觉、定向和执行控制能力在一般正常水平。";
        }else if(accur>0.65 && accur<=0.7){
            desc = "您的注意警觉、定向和执行控制能力较差。";
        }else if(accur<=0.65){
            desc = "您的注意警觉、定向和执行控制能力很差";
        }
        totalScore.put("totalScore",accur);
        totalScore.put("totalScoreDesc",desc);
        totalScores.add(totalScore);
        resultDBJson.put("totalScore",totalScores);

        //各维度得分及结论模块
        JSONArray dimensions = new JSONArray();
        JSONObject scoreA = new JSONObject(true);
        scoreA.put("name","冲突分数");

        String DescribeA = null;
        scoreA.put("score",result.get("conflictScore"));
        double conflict = Double.valueOf(String.valueOf(result.get("conflictScore")));
        if (conflict>50000){
            DescribeA = "您的注意执行控制能力很差，几乎不能够很好地处理对外界信息进行反应时的冲突。";
        }else if(conflict>=40000 && conflict<=50000  ){
            DescribeA = "您的注意执行控制能力较差，不能够很好地处理对外界信息进行反应时的冲突。";
        }else if(conflict>=30000 && conflict<40000){
            DescribeA = "您的注意执行控制能力在一般水平，能够较好但不能很好地处理对外界信息进行反应时的冲突。";
        }else if(conflict>=20000 && conflict<30000){
            DescribeA = "您的注意执行控制能力较好，能够较好地处理对外界信息进行反应时的冲突。";
        }else if(conflict<20000){
            DescribeA = "您的注意执行控制能力很好，能够很好地处理对外界信息进行反应时的冲突。";
        }
        scoreA.put("Describe",DescribeA);
        scoreA.put("maximum","无");
        scoreA.put("groupName","第一组");
        dimensions.add(scoreA);

        JSONObject scoreB = new JSONObject(true);
        scoreB.put("name","定向分数");

        scoreB.put("score",result.get("directionScore"));
        double position = Double.valueOf(String.valueOf(result.get("directionScore")));
        String DescribeB = null;
        if (position< -5000){
            DescribeB = "您的注意定向能力在很差，几乎不能很好地发觉情景中新变化并做出反应。";
        }else if(position>=-5000 && position<=-4000){
            DescribeB = "您的注意定向能力较差，不能够很好地发觉情景中新变化并做出反应。";
        }else if(position>-4000 && position<-3000){
            DescribeB = "您的注意定向能力在一般水平，不能够很好但能较好地发觉情景中新变化并做出反应。";
        }else if(position>=-3000 && position<=-2000){
            DescribeB = "您的注意定向能力较好，能够较好地发觉情景中新变化并做出反应。";
        }else if(position > -2000){
            DescribeB = "您的注意定向能力很好，能够很好地发觉情景中的新变化并做出反应。";
        }
        scoreB.put("Describe",DescribeB);
        scoreB.put("maximum","无");
        scoreB.put("groupName","第一组");
        dimensions.add(scoreB);

        JSONObject scoreC = new JSONObject(true);
        scoreC.put("name","警觉分数");

        scoreC.put("score",result.get("warnScore"));
        double warn = Double.valueOf(String.valueOf(result.get("warnScore")));
        String DescribeC = null;
        if (warn <1000){
            DescribeC = "您的注意警觉能力很好，能够很好地对某一事物保持警觉投入状态。";
        }else if(warn>=1000 && warn<=2000){
            DescribeC = "您的注意警觉能力较好，能够较好地对某一事物保持警觉投入状态。";
        }else if(warn> 2000 && warn< 3000){
            DescribeC = "您的注意警觉能力在一般水平，并不能够很好但能较好地对某一事物保持警觉投入状态。";
        }else if(warn>= 3000 && warn<=4000){
            DescribeC = "您的注意警觉能力较差，不能够很好地对某一事物保持警觉投入状态。";
        }else if(warn >4000){
            DescribeC = "您的注意警觉能力很差，几乎不能对某一事物保持警觉投入状态。";
        }
        scoreC.put("Describe",DescribeC);
        scoreC.put("maximum","无");
        scoreC.put("groupName","第一组");
        dimensions.add(scoreC);
        resultDBJson.put("dimensions", dimensions);

        //表格数据
        JSONArray tableModule = new JSONArray();
        JSONObject table1 = new JSONObject(true);
        JSONArray columnName = new JSONArray();
        columnName.add("指标名称");
        columnName.add("指标值");
        JSONArray columnValue = new JSONArray();
        columnValue.add("总命中次数;一致性命中次数;不一致性命中次数;NoCue命中次数;NeutralCue命中次数;SpatialCue命中次数;" +
                "总命中率;总反应时;一致性命中率;一致性反应时;不一致性命中率;不一致性反应时;无线索命中率;无线索反应时;" +
                "中立线索命中率;中立线索反应时;空间线索命中率;空间线索反应时;警觉分数;定向分数;冲突分数");
        columnValue.add(result.get("hitsAll")+";"+result.get("hitscongruent")+";"+result.get("hitsincongruent")+";"+
                result.get("hitsNoCue")+";"+result.get("hitsNeutralCue")+";"+result.get("hitsSpatialCue")+";"+
                result.get("hitRate")+";"+result.get("responseTimeAll")+";"+result.get("congruentHitRate")+";"+result.get("responseTimeCongruent")+";"+result.get("incongruentHitRate")
                +";"+result.get("responseTimeIncongruent")+";"+result.get("noCueHitRate")+";"+result.get("responseTimeNoCue")+";"+result.get("neutralCueHitRate")+";"+result.get("responseTimeNeutralCue")
                +";"+result.get("spatialCueHitRate")+";"+result.get("responseTimeSpatialCue")+";"+result.get("warnScore")+";"+result.get("directionScore")+";"+result.get("conflictScore"));
        table1.put("tableName","table1");
        table1.put("columnName",columnName);
        table1.put("columnValue",columnValue);
        tableModule.add(table1);
        resultDBJson.put("tableModule", tableModule);

        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("准确率", rightRate);
        resultMap.put("总反应时长", totalResponseTime);
        resultMapList.add(resultMap);

        Map<String, String> resultMap1 = new LinkedHashMap<>();
        resultMap1.put("总命中次数", String.valueOf(result.get("hitsAll")));
        resultMap1.put("一致性命中次数", String.valueOf(result.get("hitscongruent")));
        resultMap1.put("不一致性命中次数", String.valueOf(result.get("hitsincongruent")));
        resultMap1.put("NoCue命中次数", String.valueOf(result.get("hitsNoCue")));
        resultMap1.put("NeutralCue命中次数", String.valueOf(result.get("hitsNeutralCue")));
        resultMap1.put("SpatialCue命中次数", String.valueOf(result.get("hitsSpatialCue")));
        resultMap1.put("命中率", String.valueOf(result.get("hitRate")));
        resultMap1.put("一致性命中率", String.valueOf(result.get("congruentHitRate")));
        resultMap1.put("不一致性命中率", String.valueOf(result.get("incongruentHitRate")));
        resultMap1.put("NoCue命中率", String.valueOf(result.get("noCueHitRate")));
        resultMap1.put("NeutralCue命中率", String.valueOf(result.get("neutralCueHitRate")));
        resultMap1.put("SpatialCue命中率", String.valueOf(result.get("spatialCueHitRate")));
        resultMap1.put("反应时", String.valueOf(result.get("responseTimeAll")));
        resultMap1.put("一致性反应时", String.valueOf(result.get("responseTimeCongruent")));
        resultMap1.put("不一致性反应时", String.valueOf(result.get("responseTimeIncongruent")));
        resultMap1.put("NoCue反应时", String.valueOf(result.get("responseTimeNoCue")));
        resultMap1.put("NeutralCue反应时", String.valueOf(result.get("responseTimeNeutralCue")));
        resultMap1.put("SpatialCue反应时", String.valueOf(result.get("responseTimeSpatialCue")));
        resultMap1.put("警觉分数", String.valueOf(result.get("warnScore")));
        resultMap1.put("定向分数", String.valueOf(result.get("directionScore")));
        resultMap1.put("冲突分数", String.valueOf(result.get("conflictScore")));

        resultDBJson.put("总命中次数", String.valueOf(result.get("hitsAll")));
        resultDBJson.put("一致性命中次数", String.valueOf(result.get("hitscongruent")));
        resultDBJson.put("不一致性命中次数", String.valueOf(result.get("hitsincongruent")));
        resultDBJson.put("NoCue命中次数", String.valueOf(result.get("hitsNoCue")));
        resultDBJson.put("NeutralCue命中次数", String.valueOf(result.get("hitsNeutralCue")));
        resultDBJson.put("SpatialCue命中次数", String.valueOf(result.get("hitsSpatialCue")));
        resultDBJson.put("命中率", String.valueOf(result.get("hitRate")));
        resultDBJson.put("一致性命中率", String.valueOf(result.get("congruentHitRate")));
        resultDBJson.put("不一致性命中率", String.valueOf(result.get("incongruentHitRate")));
        resultDBJson.put("NoCue命中率", String.valueOf(result.get("noCueHitRate")));
        resultDBJson.put("NeutralCue命中率", String.valueOf(result.get("neutralCueHitRate")));
        resultDBJson.put("SpatialCue命中率", String.valueOf(result.get("spatialCueHitRate")));
        resultDBJson.put("反应时", String.valueOf(result.get("responseTimeAll")));
        resultDBJson.put("一致性反应时", String.valueOf(result.get("responseTimeCongruent")));
        resultDBJson.put("不一致性反应时", String.valueOf(result.get("responseTimeIncongruent")));
        resultDBJson.put("NoCue反应时", String.valueOf(result.get("responseTimeNoCue")));
        resultDBJson.put("NeutralCue反应时", String.valueOf(result.get("responseTimeNeutralCue")));
        resultDBJson.put("SpatialCue反应时", String.valueOf(result.get("responseTimeSpatialCue")));
        resultDBJson.put("警觉分数", String.valueOf(result.get("warnScore")));
        resultDBJson.put("定向分数", String.valueOf(result.get("directionScore")));
        resultDBJson.put("冲突分数", String.valueOf(result.get("conflictScore")));
        resultMapList.add(resultMap1);
        String datas = jsonArray.toString();
        ExcelUtil.createExcelFile(ANTEntity.class, jsonArray.toJavaList(ANTEntity.class), resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_ANT), 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;
    }


}
