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.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.OSUtil;
import com.rf.psychological.utils.Utils;

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

/**
 * @author zsy
 * @description:蒙特利尔认知评估量表（MoCA） & 蒙特利尔认知评估量表V2
 * @date 2021/7/20 15:55
 */
public class MOCACognize extends BaseScale {

    public MOCACognize(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;
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        String name;
        String type = resultJson.getString("type");

        int score = jsonObject.getInteger("valueSum");
        JSONObject resultDBJson = new JSONObject(true);
        Map<String, String> resultMap = new LinkedHashMap<>();
        if (Constant.QUEST_TYPE_MOCA.equals(type)){
            fileName = resultJson.getString("fileName") + "-" + Constant.SHEET_NAME_MOCA + ".xlsx";
            name = Constant.SHEET_NAME_MOCA;
            resultMap.put("总分", String.valueOf(score));
            resultDBJson.put("总分", String.valueOf(score));
        }else {
            //学历：1-小学及以下；2-中学；3-大学及以上；
            int qualification = 1;
            if (jsonObject.containsKey("qualification")){
                qualification = jsonObject.getInteger("qualification");
            }
            fileName = resultJson.getString("fileName") + "-" + Constant.SHEET_NAME_MOCA_v2 + ".xlsx";
            name = Constant.SHEET_NAME_MOCA_v2;
            resultMap.put("总分", String.valueOf(score));
            resultDBJson.put("总分", String.valueOf(score));
            switch (qualification){
                case 1:{
                    if (score <= 19){
                        resultMap.put("结论", "结果异常");
                        resultDBJson.put("结论", "结果异常");
                    }else {
                        resultMap.put("结论", "结果正常");
                        resultDBJson.put("结论", "结果正常");
                    }
                    break;
                }
                case 2:{
                    if (score <= 22){
                        resultMap.put("结论", "结果异常");
                        resultDBJson.put("结论", "结果异常");
                    }else {
                        resultMap.put("结论", "结果正常");
                        resultDBJson.put("结论", "结果正常");
                    }
                    break;
                }
                case 3:{
                    if (score <= 24){
                        resultMap.put("结论", "结果异常");
                        resultDBJson.put("结论", "结果异常");
                    }else {
                        resultMap.put("结论", "结果正常");
                        resultDBJson.put("结论", "结果正常");
                    }
                    break;
                }

            }
        }

        resultMapList.add(resultMap);
        String datas = jsonArray.toString();
        ExcelUtil.createExcelFile(ObjectEntity.class, jsonArray.toJavaList(ObjectEntity.class), resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_MOCA), 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;
    }


}
