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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.cognizeModel.ETB01Entity;
import com.rf.psychological.dao.model.XiongYingZhanChiEntity;
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:etb1:面部表情识别任务 && ETB抑郁筛查（成瘾版）
 * @date 2021/7/20 15:55
 */
public class ETB01Cognize extends BaseScale {

    public ETB01Cognize(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;
//        String type = resultJson.getString("type");
//
//        JSONObject resultDBJson = new JSONObject(true);
//
//        List<ETB01Entity> etb01Entities = jsonObject.getJSONArray("data").toJavaList(ETB01Entity.class);
//        JSONObject result = jsonObject.getJSONObject("result");
//        String totalResult = result.getString("totalResult");
//        JSONObject happy = result.getJSONObject("happy");
//        JSONObject fear = result.getJSONObject("fear");
//        JSONObject angry = result.getJSONObject("angry");
//        JSONObject disgust = result.getJSONObject("disgust");
//        JSONObject sad = result.getJSONObject("sad");
//        JSONObject surprise = result.getJSONObject("surprise");
//
//        Map<String, String> resultMap = new LinkedHashMap<>();
//        resultMap.put("总正确率", totalResult);
//        resultDBJson.put("总正确率", totalResult);
//        if (Constant.QUEST_TYPE_ETB01.equals(type)){
//            fileName = fileName + "-" + Constant.SHEET_NAME_ETB01 + ".xlsx";
//            name = Constant.SHEET_NAME_ETB01;
//            resultMap.put("开心强度1正确率", String.valueOf(happy.get("level1")));
//            resultMap.put("开心强度2正确率", String.valueOf(happy.get("level2")));
//            resultMap.put("开心强度3正确率", String.valueOf(happy.get("level3")));
//            resultMap.put("开心强度4正确率", String.valueOf(happy.get("level4")));
//
//            resultMap.put("害怕强度1正确率", String.valueOf(fear.get("level1")));
//            resultMap.put("害怕强度2正确率", String.valueOf(fear.get("level2")));
//            resultMap.put("害怕强度3正确率", String.valueOf(fear.get("level3")));
//            resultMap.put("害怕强度4正确率", String.valueOf(fear.get("level4")));
//
//            resultMap.put("生气强度1正确率", String.valueOf(angry.get("level1")));
//            resultMap.put("生气强度2正确率", String.valueOf(angry.get("level2")));
//            resultMap.put("生气强度3正确率", String.valueOf(angry.get("level3")));
//            resultMap.put("生气强度4正确率", String.valueOf(angry.get("level4")));
//
//            resultMap.put("厌恶强度1正确率", String.valueOf(disgust.get("level1")));
//            resultMap.put("厌恶强度2正确率", String.valueOf(disgust.get("level2")));
//            resultMap.put("厌恶强度3正确率", String.valueOf(disgust.get("level3")));
//            resultMap.put("厌恶强度4正确率", String.valueOf(disgust.get("level4")));
//
//            resultMap.put("悲伤强度1正确率", String.valueOf(sad.get("level1")));
//            resultMap.put("悲伤强度2正确率", String.valueOf(sad.get("level2")));
//            resultMap.put("悲伤强度3正确率", String.valueOf(sad.get("level3")));
//            resultMap.put("悲伤强度4正确率", String.valueOf(sad.get("level4")));
//
//            resultMap.put("惊讶强度1正确率", String.valueOf(surprise.get("level1")));
//            resultMap.put("惊讶强度2正确率", String.valueOf(surprise.get("level2")));
//            resultMap.put("惊讶强度3正确率", String.valueOf(surprise.get("level3")));
//            resultMap.put("惊讶强度4正确率", String.valueOf(surprise.get("level4")));
//
//            resultDBJson.put("开心强度1正确率", String.valueOf(happy.get("level1")));
//            resultDBJson.put("开心强度2正确率", String.valueOf(happy.get("level2")));
//            resultDBJson.put("开心强度3正确率", String.valueOf(happy.get("level3")));
//            resultDBJson.put("开心强度4正确率", String.valueOf(happy.get("level4")));
//
//            resultDBJson.put("害怕强度1正确率", String.valueOf(fear.get("level1")));
//            resultDBJson.put("害怕强度2正确率", String.valueOf(fear.get("level2")));
//            resultDBJson.put("害怕强度3正确率", String.valueOf(fear.get("level3")));
//            resultDBJson.put("害怕强度4正确率", String.valueOf(fear.get("level4")));
//
//            resultDBJson.put("生气强度1正确率", String.valueOf(angry.get("level1")));
//            resultDBJson.put("生气强度2正确率", String.valueOf(angry.get("level2")));
//            resultDBJson.put("生气强度3正确率", String.valueOf(angry.get("level3")));
//            resultDBJson.put("生气强度4正确率", String.valueOf(angry.get("level4")));
//
//            resultDBJson.put("厌恶强度1正确率", String.valueOf(disgust.get("level1")));
//            resultDBJson.put("厌恶强度2正确率", String.valueOf(disgust.get("level2")));
//            resultDBJson.put("厌恶强度3正确率", String.valueOf(disgust.get("level3")));
//            resultDBJson.put("厌恶强度4正确率", String.valueOf(disgust.get("level4")));
//
//            resultDBJson.put("悲伤强度1正确率", String.valueOf(sad.get("level1")));
//            resultDBJson.put("悲伤强度2正确率", String.valueOf(sad.get("level2")));
//            resultDBJson.put("悲伤强度3正确率", String.valueOf(sad.get("level3")));
//            resultDBJson.put("悲伤强度4正确率", String.valueOf(sad.get("level4")));
//
//            resultDBJson.put("惊讶强度1正确率", String.valueOf(surprise.get("level1")));
//            resultDBJson.put("惊讶强度2正确率", String.valueOf(surprise.get("level2")));
//            resultDBJson.put("惊讶强度3正确率", String.valueOf(surprise.get("level3")));
//            resultDBJson.put("惊讶强度4正确率", String.valueOf(surprise.get("level4")));
//        }else {
//            fileName = fileName + "-" + Constant.SHEET_NAME_ETB_ADDICTION + ".xlsx";
//            name = Constant.SHEET_NAME_ETB_ADDICTION;
//        }
//
//        resultMapList = new ArrayList<>();
//        resultMapList.add(resultMap);
//
//        String datas = JSONArray.toJSONString(etb01Entities);
//
//
//        ExcelUtil.createExcelFile(ETB01Entity.class, etb01Entities, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_ETB01), 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;
//    }

    public  JSONObject scaleCalculate() throws Exception {
        List<Map<String, String>> resultMapList = new ArrayList<>();
        JSONObject jsonObject = resultJson.getJSONObject("jsonObject");
        String fileName = resultJson.getString("fileName");
        String name;
        String type = resultJson.getString("type");

        JSONObject resultDBJson = new JSONObject(true);

        List<ETB01Entity> etb01Entities = jsonObject.getJSONArray("data").toJavaList(ETB01Entity.class);
        JSONObject result = jsonObject.getJSONObject("result");
        String totalResult = result.getString("totalResult");
        JSONObject happy = result.getJSONObject("happy");
        JSONObject fear = result.getJSONObject("fear");
        JSONObject angry = result.getJSONObject("angry");
        JSONObject disgust = result.getJSONObject("disgust");
        JSONObject sad = result.getJSONObject("sad");
        JSONObject surprise = result.getJSONObject("surprise");

        Map<String, String> resultMap = new LinkedHashMap<>();
        if(totalResult.contains("%")){
            totalResult = totalResult.substring(0,totalResult.indexOf("%"));
        }
        resultMap.put("总正确率", totalResult);
        resultDBJson.put("总正确率", totalResult);
        if (Constant.QUEST_TYPE_ETB01.equals(type) || Constant.QUEST_TYPE_ETB01_V2.equals(type)){
            if (Constant.QUEST_TYPE_ETB01.equals(type)){
                fileName = fileName + "-" + Constant.SHEET_NAME_ETB01 + ".xlsx";
                name = Constant.SHEET_NAME_ETB01;
            }else {
                fileName = fileName + "-" + Constant.SHEET_NAME_ETB01_V2 + ".xlsx";
                name = Constant.SHEET_NAME_ETB01_V2;
            }

            double happyLevel1 = this.getAvgValue(String.valueOf(happy.get("level1")));
            double happyLevel2 = this.getAvgValue(String.valueOf(happy.get("level2")));
            double happyLevel3 = this.getAvgValue(String.valueOf(happy.get("level3")));
            double happyLevel4 = this.getAvgValue(String.valueOf(happy.get("level4")));

            double surpriseLevel1 = this.getAvgValue(String.valueOf(surprise.get("level1")));
            double surpriseLevel2 = this.getAvgValue(String.valueOf(surprise.get("level2")));
            double surpriseLevel3 = this.getAvgValue(String.valueOf(surprise.get("level3")));
            double surpriseLevel4 = this.getAvgValue(String.valueOf(surprise.get("level4")));
            DecimalFormat decimalFormat   = new DecimalFormat("######0.00");
            double happyLevel =Double.parseDouble(decimalFormat.format((happyLevel1+happyLevel2+happyLevel3+happyLevel4)/4));
            double surpriseLevel =Double.parseDouble(decimalFormat.format((surpriseLevel1+surpriseLevel2+surpriseLevel3+surpriseLevel4)/4));


            resultMap.put("抑郁情绪指标1", String.valueOf(happyLevel));
            resultMap.put("抑郁情绪指标2", String.valueOf(surpriseLevel));
            resultDBJson.put("抑郁情绪指标1", String.valueOf(happyLevel));
            resultDBJson.put("抑郁情绪指标2", String.valueOf(surpriseLevel));
            if (happyLevel > 79 && surpriseLevel > 68){
                resultMap.put("结论", "您的心理健康状态良好，无明显抑郁情绪，请继续保持。");
                resultDBJson.put("结论", "您的心理健康状态良好，无明显抑郁情绪，请继续保持。");
            }else {
                resultMap.put("结论", "测试表明，您有明显抑郁情绪，请您咨询专业人员，并到指定医院复查。");
                resultDBJson.put("结论", "测试表明，您有明显抑郁情绪，请您咨询专业人员，并到指定医院复查。");
            }


        }else {
            fileName = fileName + "-" + Constant.SHEET_NAME_ETB_ADDICTION + ".xlsx";
            name = Constant.SHEET_NAME_ETB_ADDICTION;
        }

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

        String datas = JSONArray.toJSONString(etb01Entities);


        ExcelUtil.createExcelFile(ETB01Entity.class, etb01Entities, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_ETB01), 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;
    }

    private double getAvgValue(String value){
        if (value.contains("%")){
            value = value.substring(0,value.indexOf("%"));
        }
        return Double.parseDouble(value);
    }


}
