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.StroopEntity;
import com.rf.psychological.file.excel.ExcelClass;
import com.rf.psychological.scale.resultBusiness.scaleResult.BaseScale;
import com.rf.psychological.user.dao.model.UserEntity;
import com.rf.psychological.utils.Constant;
import com.rf.psychological.utils.ExcelUtil;
import com.rf.psychological.utils.Utils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zsy
 * @description:数字符号转换测验
 * @date 2021/7/20 15:55
 */
public class DSSTCognize extends BaseScale {

    public DSSTCognize(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;
        UserEntity userEntity = (UserEntity) resultJson.get("userEntity");
        String testTime = resultJson.getString("testTime");

        JSONObject resultDBJson = new JSONObject(true);

        String score = jsonObject.getString("score");
        String b64Img = jsonObject.getString("Img");

        resultDBJson.put("versionNo",Constant.COGNITION_RESULT_VERSION);
        JSONArray totalScores = new JSONArray();
        JSONObject totalScore = new JSONObject(true);
        String desc = null;
        totalScore.put("totalScore",score);
        if (Double.valueOf(score) > 60){
            desc = "您的认知反应速度和信息处理能力很好，且持续注意力很好。";
        }else if(Double.valueOf(score) > 30 && Double.valueOf(score) <= 60){
            desc = "您的认知反应速度和信息处理能力处在中等水平。";
        }else if(Double.valueOf(score) > 15 && Double.valueOf(score) <= 30){
            desc = "您的信息处理能力和反应速度较差，且注意力较差。";
        }else if(Double.valueOf(score) <= 15){
            desc = "您的信息处理能力和反应速度很差，建议临床上进一步筛查有认知功能障碍的可能性。";
        }
        totalScore.put("totalScoreDesc",desc);
        totalScores.add(totalScore);
        resultDBJson.put("totalScore",totalScores);

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

        fileName = fileName + "-" + Constant.SHEET_NAME_DSST + ".xlsx";
        name = Constant.SHEET_NAME_DSST;
        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("总分", score);
        resultMapList.add(resultMap);
        ExcelUtil.createExcelFile(ObjectEntity.class, null, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_DSST), fileName, Constant.SHEET_NAME_DSST);
        String imgFilePath;
        File file = new File("./心理检测/检测结果/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + Constant.SHEET_NAME_DSST);
        if (!file.exists()) {
            //file.createNewFile();
            file.mkdirs();
        }
        imgFilePath = "./心理检测/检测结果/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + Constant.SHEET_NAME_DSST + "/" + userEntity.getPetName() + Constant.SPLIT_CHAR + userEntity.getBirthday() + Constant.SPLIT_CHAR + userEntity.getGender() + Constant.SPLIT_CHAR + testTime + "-" + Constant.SHEET_NAME_DSST + ".jpg";
        Utils.GenerateImage(b64Img.substring(b64Img.indexOf(",")+1), imgFilePath);
        b64Img = imgFilePath;

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

        return returnJson;
    }


}
