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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.model.GBEEntity;
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.text.DecimalFormat;
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 GBECognize extends BaseScale {

    public GBECognize(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;

        JSONObject resultDBJson = new JSONObject(true);

        JSONObject userResponseData = jsonObject.getJSONObject("userResponseData");
        JSONObject responseTime = userResponseData.getJSONObject("responseTime");
        String responseTimeDanger1 = responseTime.getString("response1");
        String responseTimeDanger2 = responseTime.getString("response2");
        String responseTimeDanger3 = responseTime.getString("response3");
        String responseTimeDanger4 = responseTime.getString("response4");
        String level = userResponseData.getString("level");
        String isPassed = userResponseData.getString("isPassed");
        String score = userResponseData.getString("score");
        String errorNum = userResponseData.getString("errorNum");
        String roadNum = userResponseData.getString("roadNum");
        String rightRate = userResponseData.getString("rightRate");
        GBEEntity gbeEntity = new GBEEntity(level, isPassed, responseTimeDanger1, responseTimeDanger2, responseTimeDanger3, responseTimeDanger4, score, errorNum, roadNum, rightRate);

        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("关卡", String.valueOf(level));
        resultDBJson.put("关卡", String.valueOf(level));
        resultMap.put("是否通过", isPassed.equals("true") ? "通过" : "未通过");
        resultDBJson.put("是否通过", isPassed.equals("true") ? "通过" : "未通过");
        resultMap.put("总分", String.valueOf(score));
        resultDBJson.put("总分", String.valueOf(score));
        resultMap.put("错误个数", String.valueOf(errorNum));
        resultDBJson.put("错误个数", String.valueOf(errorNum));
        resultMap.put("道路个数", String.valueOf(roadNum));
        resultDBJson.put("道路个数", String.valueOf(rightRate));
        resultMap.put("正确率", String.valueOf(rightRate));
        resultDBJson.put("正确率", String.valueOf(roadNum));
        resultMapList = new ArrayList<>();
        resultMapList.add(resultMap);

        List<GBEEntity> gbeEntities = new ArrayList<>();
        gbeEntities.add(gbeEntity);
        fileName = fileName + "-" + Constant.SHEET_NAME_GBE + ".xlsx";
        name = Constant.SHEET_NAME_GBE;
        String datas = userResponseData.toString();
        ExcelUtil.createExcelFile(GBEEntity.class, gbeEntities, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_GBE), fileName, Constant.SHEET_NAME_GBE);

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

        return returnJson;
    }


}
