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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.model.STROOPTASKEntity;
import com.rf.psychological.dao.model.StroopEntity;
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.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zsy
 * @description:Stroop任务
 * @date 2021/7/20 15:55
 */
public class STROOPTASKCognize extends BaseScale {

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

        resultMapList = new ArrayList<>();
        JSONArray rawData = jsonObject.getJSONArray("rawData");
        List<STROOPTASKEntity> strooptaskEntities = rawData.toJavaList(STROOPTASKEntity.class);
        JSONArray listScore = jsonObject.getJSONArray("listScore");
        for (int i = 0; i < listScore.size(); i++) {
            Map<String, String> resultMap = new LinkedHashMap<>();
            JSONObject score = listScore.getJSONObject(i);
            if (score.getString("type").equals("1")){
                resultMap.put("同色同义平均用时", score.getString("resonseTime"));
                resultDBJson.put("同色同义平均用时", score.getString("resonseTime"));
                resultMap.put("同色同义平均正确率", score.getString("correct"));
                resultDBJson.put("同色同义平均正确率", score.getString("correct"));
            }else if (score.getString("type").equals("2")){
                resultMap.put("不同义且互补色平均用时", score.getString("resonseTime"));
                resultDBJson.put("不同义且互补色平均用时", score.getString("resonseTime"));
                resultMap.put("不同义且互补色平均正确率", score.getString("correct"));
                resultDBJson.put("不同义且互补色平均正确率", score.getString("correct"));
            }else {
                resultMap.put("不同义且不为互补色平均用时", score.getString("resonseTime"));
                resultDBJson.put("不同义且不为互补色平均用时", score.getString("resonseTime"));
                resultMap.put("不同义且不为互补色平均正确率", score.getString("correct"));
                resultDBJson.put("不同义且不为互补色平均正确率", score.getString("correct"));
            }
            resultMapList.add(resultMap);
        }
        fileName = fileName + "-" + Constant.SHEET_NAME_STROOP_TASK + ".xlsx";
        name = Constant.SHEET_NAME_STROOP_TASK;
        String datas = rawData.toString();
        ExcelUtil.createExcelFile(STROOPTASKEntity.class, strooptaskEntities, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_STROOP_TASK), 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;
    }


}
