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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.model.ObjectEntity;
import com.rf.psychological.dao.model.SHAPE2Entity;
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:听觉测试 && 听觉测试(开发版)
 * @date 2021/7/20 15:55
 */
public class BICCognize extends BaseScale {

    public BICCognize(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;
        String type = resultJson.getString("type");

        JSONObject resultDBJson = new JSONObject(true);

        float score = jsonObject.getFloat("valueSum");
        String testType = "";
        if (Constant.QUEST_TYPE_BIC.equals(type)){
            fileName = resultJson.getString("fileName") + "-" + Constant.SHEET_NAME_BIC + ".xlsx";
            name = Constant.SHEET_NAME_BIC;
        }else {
            fileName = resultJson.getString("fileName") + "-" + Constant.SHEET_NAME_BIC_V2 + ".xlsx";
            name = Constant.SHEET_NAME_BIC_V2;
            testType = jsonObject.getString("type");
        }

        JSONArray jsonArray = jsonObject.getJSONArray("userResponseRecords");
        List<SHAPE2Entity> shape2Entities = jsonArray.toJavaList(SHAPE2Entity.class);
        String desc = null;
        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("总分", String.valueOf(score));
        resultDBJson.put("总分", score);
        if (testType.equals("0")){
            desc = "正确率测试";
            resultMap.put("类型", "正确率测试");
            resultDBJson.put("类型", "正确率测试");
        }else if (testType.equals("1")){
            desc = "长度阈值测试";
            resultMap.put("类型", "长度阈值测试");
            resultDBJson.put("类型", "长度阈值测试");
        }else if (testType.equals("2")){
            desc = "延迟阈值测试";
            resultMap.put("类型", "延迟阈值测试");
            resultDBJson.put("类型", "延迟阈值测试");
        }
        resultDBJson.put("versionNo",Constant.COGNITION_RESULT_VERSION);
        JSONArray totalScores = new JSONArray();
        JSONObject totalScore = new JSONObject(true);
        totalScore.put("totalScore",score);
        totalScore.put("totalScoreDesc",desc);
        totalScores.add(totalScore);
        resultDBJson.put("totalScore",totalScores);

        //图表模块
        JSONObject chartModule = new JSONObject(true);
        chartModule.put("chartType","line");
        JSONObject chartBody = new JSONObject(true);
        JSONArray category = new JSONArray();
        JSONArray groupData = new JSONArray();
        JSONArray group1Value = new JSONArray();
        JSONArray group2Value = new JSONArray();
        for (SHAPE2Entity shape2Entity : shape2Entities) {
            category.add(shape2Entity.getIndex());
            group2Value.add(shape2Entity.getDiff());
            if (shape2Entity.getRightOrWrong().equals("Right")){
                group1Value.add(1);
            }else {
                group1Value.add(-1);
            }
        }
        JSONObject group1 = new JSONObject(true);
        group1.put("index",1);
        group1.put("value",group1Value);
        group1.put("name","group1");
        groupData.add(group1);
        JSONObject group2 = new JSONObject(true);
        group2.put("index",2);
        group2.put("value",group2Value);
        group2.put("name","group2");
        groupData.add(group2);
        chartBody.put("groupData",groupData);
        chartBody.put("category",category);
        chartModule.put("chartBody",chartBody);
        resultDBJson.put("chartModule", chartModule);

        JSONObject ifShow = new JSONObject(true);
        ifShow.put("totalScore",true);
        ifShow.put("dimensions",false);
        ifShow.put("note",false);
        ifShow.put("radar",true);
        ifShow.put("table",false);
        resultDBJson.put("ifShow",ifShow);
        resultMapList = new ArrayList<>();
        resultMapList.add(resultMap);
        String datas = jsonArray.toString();
        ExcelUtil.createExcelFile(SHAPE2Entity.class, shape2Entities, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_BIC), 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;
    }


}
