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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.cognizeModel.FDOTEntity;
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:情绪测试3：点探测(儿童版)
 * @date 2021/7/20 15:55
 */
public class FDOTCHILDCognize extends BaseScale {

    public FDOTCHILDCognize(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);

        List<FDOTEntity> block = jsonObject.getJSONArray("data").toJavaList(FDOTEntity.class);
        JSONObject result = jsonObject.getJSONObject("result");
        JSONObject block1Result = result.getJSONObject("block1");
        String block1TotalCorrectRate = block1Result.getString("totalCorrectRate");
        String block1PosCorrectRate = block1Result.getString("posCorrectRate");
        String block1PosAverageTime = block1Result.getString("posAverageTime");
        String block1NegCorrectRate = block1Result.getString("negCorrectRate");
        String block1NegAverageTime = block1Result.getString("negAverageTime");
        String block1NeuCorrectRate = block1Result.getString("neuCorrectRate");
        String block1NeuAverageTime = block1Result.getString("neuAverageTime");
        //积极偏向
        String positiveBias = block1Result.getString("positiveBias");
        //消极偏向
        String negativeBias = block1Result.getString("negativeBias");

        resultDBJson.put("versionNo",Constant.COGNITION_RESULT_VERSION);
        JSONArray totalScores = new JSONArray();
        JSONObject totalScore = new JSONObject(true);
        String desc = null;
        totalScore.put("totalScore",block1TotalCorrectRate.replace("%",""));
        double k = Double.valueOf(block1NegAverageTime) - Double.valueOf(block1PosAverageTime);
        if (k >= 0){
            desc = "没有焦虑情绪和情绪障碍";
        }else if(k >= -200 && k < 0){
            desc = "可能存在焦虑情绪和情绪障碍";
        }else if(k >= -500 && k < -200){
            desc = "存在一定的焦虑情绪或情绪障碍";
        }else if(k < -500){
            desc = "存在较严重的焦虑情绪或情绪障碍，建议求助专业医生";
        }
        totalScore.put("totalScoreDesc",desc);
        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(block1TotalCorrectRate+";"+block1PosCorrectRate+";"+block1PosAverageTime+";"+block1NegCorrectRate+";"+block1NegAverageTime+";"+block1NeuCorrectRate+";"+block1NeuAverageTime+";"+positiveBias+";"+negativeBias);
        table1.put("tableName","table1");
        table1.put("columnName",columnName);
        table1.put("columnValue",columnValue);
        tableModule.add(table1);
        resultDBJson.put("tableModule", tableModule);

        //图表模块
        JSONObject chartModule = new JSONObject(true);
        chartModule.put("chartType","radar");
        JSONObject chartBody = new JSONObject(true);
        JSONArray indicator = new JSONArray();
        JSONObject indicatorA = new JSONObject(true);
        indicatorA.put("name","积极正确率");
        indicatorA.put("max",100);
        indicator.add(indicatorA);
        JSONObject indicatorB = new JSONObject(true);
        indicatorB.put("name","消极正确率");
        indicatorB.put("max",100);
        indicator.add(indicatorB);
        JSONObject indicatorC = new JSONObject(true);
        indicatorC.put("name","中性正确率");
        indicatorC.put("max",100);
        indicator.add(indicatorC);
        chartBody.put("indicator",indicator);
        JSONArray groupData = new JSONArray();
        JSONObject group1 = new JSONObject(true);
        JSONArray group1Value = new JSONArray();
        group1Value.add(block1PosCorrectRate.replace("%",""));
        group1Value.add(block1NegCorrectRate.replace("%",""));
        group1Value.add(block1NeuCorrectRate.replace("%",""));
        group1.put("index",1);
        group1.put("value",group1Value);
        group1.put("name","group1");
        groupData.add(group1);
        chartBody.put("groupData",groupData);
        chartModule.put("chartBody",chartBody);
        resultDBJson.put("chartModule", chartModule);

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

        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("总体正确率", block1TotalCorrectRate);
        resultDBJson.put("总体正确率", block1TotalCorrectRate);

        resultMap.put("积极正确率", block1PosCorrectRate);
        resultMap.put("积极反应时", block1PosAverageTime);
        resultMap.put("消极正确率", block1NegCorrectRate);
        resultMap.put("消极反应时", block1NegAverageTime);
        resultMap.put("中性正确率", block1NeuCorrectRate);
        resultMap.put("中性反应时", block1NeuAverageTime);
        resultMap.put("积极偏向", positiveBias);
        resultMap.put("消极偏向", negativeBias);

        resultDBJson.put("积极正确率", block1PosCorrectRate);
        resultDBJson.put("积极反应时", block1PosAverageTime);
        resultDBJson.put("消极正确率", block1NegCorrectRate);
        resultDBJson.put("消极反应时", block1NegAverageTime);
        resultDBJson.put("中性正确率", block1NeuCorrectRate);
        resultDBJson.put("中性反应时", block1NeuAverageTime);
        resultDBJson.put("积极偏向", positiveBias);
        resultDBJson.put("消极偏向", negativeBias);

        resultMapList = new ArrayList<>();
        resultMapList.add(resultMap);


        fileName = fileName + "-" + Constant.SHEET_NAME_FDOT_CHILD + ".xlsx";
        name = Constant.SHEET_NAME_FDOT_CHILD;
        String datas = jsonObject.getJSONArray("data").toString();
        ExcelUtil.createExcelFile(FDOTEntity.class, block, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_FDOT), 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;
    }


}
