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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.cognizeModel.ECATEntity;
import com.rf.psychological.dao.cognizeModel.EMEMEntity;
import com.rf.psychological.dao.model.ObjectEntity;
import com.rf.psychological.file.excel.ExcelBean;
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.*;
import java.util.stream.Collectors;

/**
 * @author zsy
 * @description:情绪测试2：词汇测试(成瘾版)
 * @date 2021/7/20 15:55
 */
public class ECATADDICTIONCognize extends BaseScale {

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

        JSONObject ecat = jsonObject.getJSONObject("ECAT");
        JSONObject erec = jsonObject.getJSONObject("EREC");
        JSONObject emem = jsonObject.getJSONObject("EMEM");
        //ECAT计算
        List<ECATEntity> ecatEntities = ecat.getJSONArray("data").toJavaList(ECATEntity.class);
        JSONObject ecatResult = ecat.getJSONObject("result");
        String posNum = ecatResult.getString("posNum");
        String negNum = ecatResult.getString("negNum");
        String posAverageTime = ecatResult.getString("posAverageTime");
        String negAverageTime = ecatResult.getString("negAverageTime");

        JSONObject resultMapJson = new JSONObject(true);
        HashMap<String, String> ecatReturnResult = new HashMap<>();
        Map<String, String> ecatResultMap = new LinkedHashMap<>();
        ecatResultMap.put("ECAT积极词语个数", posNum);
        resultDBJson.put("ECAT积极词语个数", posNum);
        ecatReturnResult.put("ECAT积极词语个数", posNum);

        ecatResultMap.put("ECAT消极词语个数", negNum);
        resultDBJson.put("ECAT消极词语个数", negNum);
        ecatReturnResult.put("ECAT消极词语个数", negNum);

        ecatResultMap.put("ECAT积极词语平均反应时", posAverageTime);
        resultDBJson.put("ECAT积极词语平均反应时", posAverageTime);
        ecatReturnResult.put("ECAT积极词语平均反应时", posAverageTime);

        ecatResultMap.put("ECAT消极词语平均反应时", negAverageTime);
        resultDBJson.put("ECAT消极词语平均反应时", negAverageTime);
        ecatReturnResult.put("ECAT消极词语平均反应时", negAverageTime);

        resultMapJson.put("ECAT", ecatResultMap);
        //EREC
        JSONObject data = erec.getJSONObject("data");
        String totalWords = data.getString("totalWords");
        String positiveWordCount = data.getString("positiveWordCount");
        String negtiveWordCount = data.getString("negtiveWordCount");
        JSONObject result = erec.getJSONObject("result");
        String posCorrectRate = result.getString("posCorrectRate");
        String negCorrectRate = result.getString("negCorrectRate");
        String totalCorrectRate = result.getString("totalCorrectRate");

        Map<String, String> erecResultMap = new LinkedHashMap<>();
        HashMap<String, String> erecReturnResult = new HashMap<>();
        erecReturnResult.put("EREC总体正确率", totalCorrectRate);
        resultDBJson.put("EREC总体正确率", totalCorrectRate);
        erecResultMap.put("EREC积极词语的正确率", posCorrectRate);
        erecReturnResult.put("EREC积极词语的正确率", posCorrectRate);
        resultDBJson.put("EREC积极词语的正确率", posCorrectRate);
        erecResultMap.put("EREC消极词语的正确率", negCorrectRate);
        erecReturnResult.put("EREC消极词语的正确率", negCorrectRate);
        resultDBJson.put("EREC消极词语的正确率", negCorrectRate);

        resultMapJson.put("EREC", erecResultMap);

        ObjectEntity totalObjectEntity = new ObjectEntity("总词数", totalWords);
        ObjectEntity posObjectEntity = new ObjectEntity("积极词语个数", positiveWordCount);
        ObjectEntity negObjectEntity = new ObjectEntity("消极词语个数", negtiveWordCount);
        ArrayList<ObjectEntity> erecObjects = new ArrayList<>();
        erecObjects.add(totalObjectEntity);
        erecObjects.add(posObjectEntity);
        erecObjects.add(negObjectEntity);
        //EMEM
        List<EMEMEntity> ememEntities = emem.getJSONArray("data").toJavaList(EMEMEntity.class);
        JSONObject ememResult = emem.getJSONObject("result");
        String ememPosCorrectRate = ememResult.getString("posCorrectRate");
        String ememNegCorrectRate = ememResult.getString("negCorrectRate");
        String ememTotalCorrectRate = ememResult.getString("totalCorrectRate");

        Map<String, String> ememResultMap = new LinkedHashMap<>();
        HashMap<String, String> ememReturnResult = new HashMap<>();
        ememResultMap.put("EMEM总体正确率", ememTotalCorrectRate);
        ememReturnResult.put("EMEM总体正确率", ememTotalCorrectRate);
        resultDBJson.put("EMEM总体正确率", ememTotalCorrectRate);
        ememResultMap.put("EMEM积极词语的正确率", ememPosCorrectRate);
        ememReturnResult.put("EMEM积极词语的正确率", ememPosCorrectRate);
        resultDBJson.put("EMEM积极词语的正确率", ememPosCorrectRate);
        ememResultMap.put("EMEM消极词语的正确率", ememNegCorrectRate);
        ememReturnResult.put("EMEM消极词语的正确率", ememNegCorrectRate);
        resultDBJson.put("EMEM消极词语的正确率", ememNegCorrectRate);

        resultMapJson.put("EMEM", ememResultMap);

        ArrayList<Class> classes = new ArrayList<>();
        //实体类
        classes.add(ECATEntity.class);
        classes.add(ObjectEntity.class);
        classes.add(EMEMEntity.class);
        //答题记录
        Map<String, List> objs = new HashMap<>();
        objs.put("ECAT", ecatEntities.stream().sorted(Comparator.comparing(ECATEntity::getIndex, Comparator.comparingInt(Integer::parseInt))).collect(Collectors.toList()));
        objs.put("EREC", erecObjects);
        objs.put("EMEM", ememEntities.stream().sorted(Comparator.comparing(EMEMEntity::getIndex, Comparator.comparingInt(Integer::parseInt))).collect(Collectors.toList()));
        //表头
        List<Map<Integer, List<ExcelBean>>> tableHeader = new ArrayList<>();
        tableHeader.add(new ExcelClass().contentExcel(Constant.QUEST_TYPE_ECAT));
        tableHeader.add(new ExcelClass().contentExcel(Constant.QUEST_TYPE_EREC));
        tableHeader.add(new ExcelClass().contentExcel(Constant.QUEST_TYPE_EMEM));
        if (Constant.QUEST_TYPE_EALL.equals(type)){
            fileName = fileName + "-" + Constant.SHEET_NAME_EALL + ".xlsx";
            name = Constant.SHEET_NAME_EALL;
        }else {
            fileName = fileName + "-" + Constant.SHEET_NAME_ECAT_ADDICTION + ".xlsx";
            name = Constant.SHEET_NAME_ECAT_ADDICTION;
        }

        ExcelUtil.createEALLExcelFile(classes, objs, resultMapJson, tableHeader, fileName, name);

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

        return returnJson;
    }


}
