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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.cognizeModel.EMEMEntity;
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.text.DecimalFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zsy
 * @description:Faces Dot Probe Task (FDOT)
 * @date 2021/7/20 15:55
 */
public class FDOTCognize extends BaseScale {

    public FDOTCognize(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");
        JSONObject block2Result = result.getJSONObject("block2");
        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 block2TotalCorrectRate = block2Result.getString("totalCorrectRate");
        String block2PosCorrectRate = block2Result.getString("posCorrectRate");
        String block2PosAverageTime = block2Result.getString("posAverageTime");
        String block2NegCorrectRate = block2Result.getString("negCorrectRate");
        String block2NegAverageTime = block2Result.getString("negAverageTime");
        String block2NeuCorrectRate = block2Result.getString("neuCorrectRate");
        String block2NeuAverageTime = block2Result.getString("neuAverageTime");

        DecimalFormat df = new DecimalFormat("######0.00");
        double TotalCorrectRate = Double.parseDouble(df.format((Double.valueOf(block1TotalCorrectRate.replace("%",""))+Double.valueOf(block2TotalCorrectRate.replace("%","")))/2));

        resultDBJson.put("versionNo",Constant.COGNITION_RESULT_VERSION);
        JSONArray totalScores = new JSONArray();
        JSONObject totalScore = new JSONObject(true);
        String desc = null;
        totalScore.put("totalScore",TotalCorrectRate);
        double k = Double.parseDouble(df.format(((Double.valueOf(block1NegAverageTime)+Double.valueOf(block2NegAverageTime)) - (Double.valueOf(block1NeuAverageTime)+Double.valueOf(block2NeuAverageTime)))/2));
        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("block1总体正确率;block1积极正确率;block1积极反应时;block1消极正确率;block1消极反应时;block1中性正确率;block1中性反应时;block2总体正确率" +
                ";block2积极正确率;block2积极反应时;block2消极正确率;block2消极反应时;block2中性正确率;block2中性反应时");
        columnValue.add(block1TotalCorrectRate+";"+block1PosCorrectRate+";"+block1PosAverageTime+";"+block1NegCorrectRate+";"+block1NegAverageTime
                +";"+block1NeuCorrectRate+";"+block1NeuAverageTime+";"+block2TotalCorrectRate+";"+block2PosCorrectRate+";"+block2PosAverageTime
                +";"+block2NegCorrectRate+";"+block2NegAverageTime+";"+block2NeuCorrectRate+";"+block2NeuAverageTime);
        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","block1总体正确率");
        indicatorA.put("max",100);
        indicator.add(indicatorA);
        JSONObject indicatorB = new JSONObject(true);
        indicatorB.put("name","block1积极正确率");
        indicatorB.put("max",100);
        indicator.add(indicatorB);
        JSONObject indicatorC = new JSONObject(true);
        indicatorC.put("name","block1消极正确率");
        indicatorC.put("max",100);
        indicator.add(indicatorC);
        chartBody.put("indicator",indicator);
        JSONObject indicatorD = new JSONObject(true);
        indicatorD.put("name","block1中性正确率");
        indicatorD.put("max",100);
        indicator.add(indicatorD);
        JSONObject indicatorE = new JSONObject(true);
        indicatorE.put("name","block2总体正确率");
        indicatorE.put("max",100);
        indicator.add(indicatorE);
        JSONObject indicatorF = new JSONObject(true);
        indicatorF.put("name","block2积极正确率");
        indicatorF.put("max",100);
        indicator.add(indicatorF);
        chartBody.put("indicator",indicator);
        JSONObject indicatorG = new JSONObject(true);
        indicatorG.put("name","block2消极正确率");
        indicatorG.put("max",100);
        indicator.add(indicatorG);
        JSONObject indicatorH = new JSONObject(true);
        indicatorH.put("name","block2中性正确率");
        indicatorH.put("max",100);
        indicator.add(indicatorH);
        JSONArray groupData = new JSONArray();
        JSONObject group1 = new JSONObject(true);
        JSONArray group1Value = new JSONArray();
        group1Value.add(block1TotalCorrectRate.replace("%",""));
        group1Value.add(block1PosCorrectRate.replace("%",""));
        group1Value.add(block1NegCorrectRate.replace("%",""));
        group1Value.add(block1NeuCorrectRate.replace("%",""));
        group1Value.add(block2TotalCorrectRate.replace("%",""));
        group1Value.add(block2PosCorrectRate.replace("%",""));
        group1Value.add(block2NegCorrectRate.replace("%",""));
        group1Value.add(block2NeuCorrectRate.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("block1总体正确率", block1TotalCorrectRate);
        resultMap.put("block1积极正确率", block1PosCorrectRate);
        resultMap.put("block1积极反应时", block1PosAverageTime);
        resultMap.put("block1消极正确率", block1NegCorrectRate);
        resultMap.put("block1消极反应时", block1NegAverageTime);
        resultMap.put("block1中性正确率", block1NeuCorrectRate);
        resultMap.put("block1中性反应时", block1NeuAverageTime);

        resultMap.put("block2总体正确率", block2TotalCorrectRate);
        resultMap.put("block2积极正确率", block2PosCorrectRate);
        resultMap.put("block2积极反应时", block2PosAverageTime);
        resultMap.put("block2消极正确率", block2NegCorrectRate);
        resultMap.put("block2消极反应时", block2NegAverageTime);
        resultMap.put("block2中性正确率", block2NeuCorrectRate);
        resultMap.put("block2中性反应时", block2NeuAverageTime);

        resultDBJson.put("block1总体正确率", block1TotalCorrectRate);
        resultDBJson.put("block1积极正确率", block1PosCorrectRate);
        resultDBJson.put("block1积极反应时", block1PosAverageTime);
        resultDBJson.put("block1消极正确率", block1NegCorrectRate);
        resultDBJson.put("block1消极反应时", block1NegAverageTime);
        resultDBJson.put("block1中性正确率", block1NeuCorrectRate);
        resultDBJson.put("block1中性反应时", block1NeuAverageTime);

        resultDBJson.put("block2总体正确率", block2TotalCorrectRate);
        resultDBJson.put("block2积极正确率", block2PosCorrectRate);
        resultDBJson.put("block2积极反应时", block2PosAverageTime);
        resultDBJson.put("block2消极正确率", block2NegCorrectRate);
        resultDBJson.put("block2消极反应时", block2NegAverageTime);
        resultDBJson.put("block2中性正确率", block2NeuCorrectRate);
        resultDBJson.put("block2中性反应时", block2NeuAverageTime);

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


        fileName = fileName + "-" + Constant.SHEET_NAME_FDOT + ".xlsx";
        name = Constant.SHEET_NAME_FDOT;
        String datas = jsonObject.getJSONArray("data").toString();
        ExcelUtil.createExcelFile(FDOTEntity.class, block, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_FDOT), fileName, Constant.SHEET_NAME_FDOT);

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

        return returnJson;
    }


}
