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

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

    public DSTCognize(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 result = jsonObject.getJSONObject("result");
        String aescScore = result.getString("AESCScore");
        String descScore = result.getString("DESCScore");
        JSONArray aesc = result.getJSONArray("AESC"); //正序
        JSONArray desc = result.getJSONArray("DESC"); //倒叙

        ArrayList<DSTEntity> dstEntities = new ArrayList<>();
        List<DSTEntity> aescEntities = aesc.toJavaList(DSTEntity.class);
        List<DSTEntity> descEntities = desc.toJavaList(DSTEntity.class);
        dstEntities.addAll(aescEntities);
        dstEntities.addAll(descEntities);

        resultDBJson.put("versionNo",Constant.COGNITION_RESULT_VERSION);
        JSONArray totalScores = new JSONArray();
        JSONObject totalScore = new JSONObject(true);
        double correct = Double.valueOf(aescScore) + Double.valueOf(descScore);
        String description = null;
        totalScore.put("totalScore",correct);
        if (correct > 20){
            description = "您的即时记忆较好，记忆容量较大，且注意力很集中。";
        }else if(correct > 10 && correct <= 20){
            description = "您的即时记忆一般，记忆容量也处在一般水平，注意力集中。";
        }else if(correct > 5 && correct <= 10){
            description = "您的即时记忆能力较差，记忆容量较小，建议加强记忆训练。";
        }else if(correct <= 5){
            description = "您的即时记忆能力受损较严重，建议临床上进一步筛查有记忆障碍的可能性。";
        }
        totalScore.put("totalScoreDesc",description);
        totalScores.add(totalScore);
        resultDBJson.put("totalScore",totalScores);
        //表格数据
        JSONArray tableModule = new JSONArray();
        JSONObject table1 = new JSONObject(true);
        JSONArray columnName = new JSONArray();
        columnName.add("指标名称");
        columnName.add("指标值");
        JSONArray columnValue = new JSONArray();
        columnValue.add("顺序成绩;倒序成绩");
        columnValue.add(aescScore+";"+descScore);
        table1.put("tableName","table1");
        table1.put("columnName",columnName);
        table1.put("columnValue",columnValue);
        tableModule.add(table1);
        resultDBJson.put("tableModule", tableModule);

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

        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("顺序成绩", aescScore);
        resultDBJson.put("顺序成绩", aescScore);
        resultMap.put("倒序成绩", descScore);
        resultDBJson.put("倒序成绩", descScore);
        resultMapList = new ArrayList<>();
        resultMapList.add(resultMap);

        String datas = JSONArray.toJSONString(dstEntities);
        if (Constant.QUEST_TYPE_DST.equals(type)){
            name = Constant.SHEET_NAME_DST;
        }else {
            name = Constant.SHEET_NAME_DST_VOICE;
        }
        fileName = fileName + "-" + name + ".xlsx";
        ExcelUtil.createExcelFile(DSTEntity.class, dstEntities, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_DST), 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;
    }


}
