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

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

    public MEMORYYATAICognize(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<WebMemoryEntity> dataList = new ArrayList<>();
        JSONObject jsonObject0 = jsonObject.getJSONObject("data");
        JSONArray back0 = jsonObject0.getJSONArray("back0");
        for (int i = 0; i < back0.size(); i++) {
            JSONArray back0a = back0.getJSONArray(i);
            for (int y = 0; y < back0a.size(); y++) {
                JSONObject back0b = back0a.getJSONObject(y);
                WebMemoryEntity webMemoryEntity = new WebMemoryEntity();
                webMemoryEntity = back0b.toJavaObject(WebMemoryEntity.class);
                webMemoryEntity.setDiffLevel("0back");
                dataList.add(webMemoryEntity);
            }
        }
        JSONArray back1 = jsonObject0.getJSONArray("back1");
        for (int i = 0; i < back1.size(); i++) {
            JSONArray back1a = back1.getJSONArray(i);
            for (int y = 0; y < back1a.size(); y++) {
                JSONObject back1b = back1a.getJSONObject(y);
                WebMemoryEntity webMemoryEntity = new WebMemoryEntity();
                webMemoryEntity = back1b.toJavaObject(WebMemoryEntity.class);
                webMemoryEntity.setDiffLevel("1back");
                dataList.add(webMemoryEntity);
            }
        }
        JSONArray back2 = jsonObject0.getJSONArray("back2");
        for (int i = 0; i < back2.size(); i++) {
            JSONArray back2a = back2.getJSONArray(i);
            for (int y = 0; y < back2a.size(); y++) {
                JSONObject back2b = back2a.getJSONObject(y);
                WebMemoryEntity webMemoryEntity = new WebMemoryEntity();
                webMemoryEntity = back2b.toJavaObject(WebMemoryEntity.class);
                webMemoryEntity.setDiffLevel("2back");
                dataList.add(webMemoryEntity);
            }
        }
        JSONArray totalIndexResult = jsonObject.getJSONArray("totalIndexResult");
        JSONObject resultBack0 = totalIndexResult.getJSONObject(0);
        Map<String, String> resultMap0 = new LinkedHashMap<>();
        resultMap0.put("0-back难度等级", String.valueOf(resultBack0.get("diffLevel")));
        resultDBJson.put("0-back难度等级", String.valueOf(resultBack0.get("diffLevel")));
        resultMap0.put("0-back总测试数", String.valueOf(resultBack0.get("totalTestCount")));
        resultDBJson.put("0-back总测试数", String.valueOf(resultBack0.get("totalTestCount")));
        resultMap0.put("0-back正确数", String.valueOf(resultBack0.get("rightCount")));
        resultDBJson.put("0-back正确数", String.valueOf(resultBack0.get("rightCount")));
        resultMap0.put("0-back正确率", String.valueOf(resultBack0.get("rightRate")));
        resultDBJson.put("0-back正确率", String.valueOf(resultBack0.get("rightRate")));
        resultMap0.put("0-back平均反应时（ms）", String.valueOf(resultBack0.get("averageResponseTime")));
        resultDBJson.put("0-back平均反应时（ms）", String.valueOf(resultBack0.get("averageResponseTime")));
        resultMap0.put("0-back测试用时", String.valueOf(resultBack0.get("testTotalTime")));
        resultDBJson.put("0-back测试用时", String.valueOf(resultBack0.get("testTotalTime")));
        resultMapList.add(resultMap0);
        JSONObject resultBack1 = totalIndexResult.getJSONObject(1);
        Map<String, String> resultMap1 = new LinkedHashMap<>();
        resultMap1.put("1-back难度等级", String.valueOf(resultBack1.get("diffLevel")));
        resultDBJson.put("1-back难度等级", String.valueOf(resultBack1.get("diffLevel")));
        resultMap1.put("1-back总测试数", String.valueOf(resultBack1.get("totalTestCount")));
        resultDBJson.put("1-back总测试数", String.valueOf(resultBack1.get("totalTestCount")));
        resultMap1.put("1-back正确数", String.valueOf(resultBack1.get("rightCount")));
        resultDBJson.put("1-back正确数", String.valueOf(resultBack1.get("rightCount")));
        resultMap1.put("1-back正确率", String.valueOf(resultBack1.get("rightRate")));
        resultDBJson.put("1-back正确率", String.valueOf(resultBack1.get("rightRate")));
        resultMap1.put("1-back平均反应时（ms）", String.valueOf(resultBack1.get("averageResponseTime")));
        resultDBJson.put("1-back平均反应时（ms）", String.valueOf(resultBack1.get("averageResponseTime")));
        resultMap1.put("1-back测试用时", String.valueOf(resultBack1.get("testTotalTime")));
        resultDBJson.put("1-back测试用时", String.valueOf(resultBack1.get("testTotalTime")));
        resultMapList.add(resultMap1);
        JSONObject resultBack2 = totalIndexResult.getJSONObject(2);
        Map<String, String> resultMap2 = new LinkedHashMap<>();
        resultMap2.put("2-back难度等级", String.valueOf(resultBack2.get("diffLevel")));
        resultDBJson.put("2-back难度等级", String.valueOf(resultBack2.get("diffLevel")));
        resultMap2.put("2-back总测试数", String.valueOf(resultBack2.get("totalTestCount")));
        resultDBJson.put("2-back总测试数", String.valueOf(resultBack2.get("totalTestCount")));
        resultMap2.put("2-back正确数", String.valueOf(resultBack2.get("rightCount")));
        resultDBJson.put("2-back正确数", String.valueOf(resultBack2.get("rightCount")));
        resultMap2.put("2-back正确率", String.valueOf(resultBack2.get("rightRate")));
        resultDBJson.put("2-back正确率", String.valueOf(resultBack2.get("rightRate")));
        resultMap2.put("2-back平均反应时（ms）", String.valueOf(resultBack2.get("averageResponseTime")));
        resultDBJson.put("2-back平均反应时（ms）", String.valueOf(resultBack2.get("averageResponseTime")));
        resultMap2.put("2-back测试用时", String.valueOf(resultBack2.get("testTotalTime")));
        resultDBJson.put("2-back测试用时", String.valueOf(resultBack2.get("testTotalTime")));
        resultMapList.add(resultMap2);

        fileName = fileName + "-" + Constant.SHEET_NAME_MEMORY_YATAI + ".xlsx";
        name = Constant.SHEET_NAME_MEMORY_YATAI;
        String datas = jsonObject0.toString();
        ExcelUtil.createExcelFile(WebMemoryEntity.class, dataList, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_MEMORY), fileName, Constant.SHEET_NAME_MEMORY_YATAI);

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

        return returnJson;
    }


}
