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.*;

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

/**
 * @author zsy
 * @description:画钟测试
 * @date 2021/7/20 15:55
 */
public class CDTCognize extends BaseScale {

    public CDTCognize(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") + "-" + Constant.SHEET_NAME_RIVEN + ".xlsx";
        UserEntity userEntity = (UserEntity) resultJson.get("userEntity");
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        int score = jsonObject.getInteger("Grade");
        String b64Img = jsonObject.getString("Img");
        String name = Constant.SHEET_NAME_CDT;
        String testTime = resultJson.getString("testTime");

        JSONObject resultDBJson = new JSONObject(true);
        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("总分", String.valueOf(score));
        resultDBJson.put("总分", String.valueOf(score));
        resultMapList.add(resultMap);
        String datas = jsonArray.toString();
        List<ObjectEntity> objectEntities = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject object = jsonArray.getJSONObject(i);
            Iterator<String> iterator = object.keySet().iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                ObjectEntity objectEntity = new ObjectEntity(key, object.getString(key));
                objectEntities.add(objectEntity);
            }
        }
        //ExcelUtil.createExcelFile(ObjectEntity.class, jsonArray.toJavaList(ObjectEntity.class), resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_CDT), fileName, Constant.SHEET_NAME_CDT);
        ExcelUtil.createExcelFile(ObjectEntity.class, objectEntities, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_CDT), fileName, Constant.SHEET_NAME_CDT);
        String imgFilePath;
        if (OSUtil.isLinux()) {
            File file = new File("./心理检测/检测结果/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + Constant.SHEET_NAME_CDT);
            if (!file.exists()) {
                //file.createNewFile();
                file.mkdirs();
            }
            imgFilePath = "./心理检测/检测结果/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + Constant.SHEET_NAME_CDT + "/" + userEntity.getPetName() + Constant.SPLIT_CHAR + userEntity.getBirthday() + Constant.SPLIT_CHAR + userEntity.getGender() + Constant.SPLIT_CHAR + testTime + "-" + Constant.SHEET_NAME_CDT + ".jpg";
        } else {
            File file = new File("./心理检测/检测结果/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + Constant.SHEET_NAME_CDT);
            if (!file.exists()) {
                //file.createNewFile();
                file.mkdirs();
            }
            imgFilePath = "./心理检测/检测结果/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + Constant.SHEET_NAME_CDT + "/" + userEntity.getPetName() + Constant.SPLIT_CHAR + userEntity.getBirthday() + Constant.SPLIT_CHAR + userEntity.getGender() + Constant.SPLIT_CHAR + testTime + "-" + Constant.SHEET_NAME_CDT + ".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",datas);
        returnJson.put("b64Img",b64Img);

        return returnJson;
    }


}
