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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.model.ANTEntity;
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 SHAPECognize extends BaseScale {

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

        String userScore = jsonObject.getString("userScore");
        JSONArray jsonArray = jsonObject.getJSONArray("userResponseRecords");
        List<SHAPE2Entity> shape2Entities = jsonArray.toJavaList(SHAPE2Entity.class);
        if (Constant.QUEST_TYPE_SHAPE.equals(type)){
            fileName = fileName + "-" + Constant.SHEET_NAME_SHAPE + ".xlsx";
            name = Constant.SHEET_NAME_SHAPE;
        }else if (Constant.QUEST_TYPE_SHAPE_RANDOM.equals(type)){
            fileName = fileName + "-" + Constant.SHEET_NAME_SHAPE_RANDOM + ".xlsx";
            name = Constant.SHEET_NAME_SHAPE_RANDOM;
        }else if (Constant.QUEST_TYPE_SHAPE2.equals(type)){
            fileName = fileName + "-" + Constant.SHEET_NAME_SHAPE2 + ".xlsx";
            name = Constant.SHEET_NAME_SHAPE2;
        }else {
            fileName = fileName + "-" + Constant.SHEET_NAME_SHAPE_CHILD + ".xlsx";
            name = Constant.SHEET_NAME_SHAPE_CHILD;
        }

        double diffOne = 0;
        double diffTwo = 0;
        double diffThree = 0;
        double diffFour = 0;
        double diffFive = 0;
        double diffSix = 0;
        int diffOneTotal = 0;
        int diffTwoTotal = 0;
        int diffThreeTotal = 0;
        int diffFourTotal = 0;
        int diffFiveTotal = 0;
        int diffSixTotal = 0;
        for (SHAPE2Entity shape2Entity : shape2Entities){
            if (shape2Entity.getRightOrWrong().equals("Right")){
                if (shape2Entity.getDiff().equals("0")) {
                    diffOne ++;
                }else if (shape2Entity.getDiff().equals("30")){
                    diffTwo ++;
                }else if (shape2Entity.getDiff().equals("60")){
                    diffThree ++;
                }else if (shape2Entity.getDiff().equals("90")){
                    diffFour ++;
                }else if (shape2Entity.getDiff().equals("180")){
                    diffFive ++;
                }else {
                    diffSix ++;
                }
            }else {
                if (shape2Entity.getDiff().equals("0")) {
                    diffOneTotal ++;
                }else if (shape2Entity.getDiff().equals("30")){
                    diffTwoTotal ++;
                }else if (shape2Entity.getDiff().equals("60")){
                    diffThreeTotal ++;
                }else if (shape2Entity.getDiff().equals("90")){
                    diffFourTotal ++;
                }else if (shape2Entity.getDiff().equals("180")){
                    diffFiveTotal ++;
                }else {
                    diffSixTotal ++;
                }
            }
        }
        DecimalFormat df = new DecimalFormat("######0.00");
        diffOne = Double.parseDouble(df.format(diffOne/(diffOne+diffOneTotal) * 100));
        diffTwo = Double.parseDouble(df.format(diffTwo/(diffTwo+diffTwoTotal) * 100));
        diffThree = Double.parseDouble(df.format(diffThree/(diffThree+diffThreeTotal) * 100));
        diffFour = Double.parseDouble(df.format(diffFour/(diffFour+diffFourTotal) * 100));
        diffFive = Double.parseDouble(df.format(diffFive/(diffFive+diffFiveTotal) * 100));
        diffSix = Double.parseDouble(df.format(diffSix/(diffSix+diffSixTotal) * 100));

        double score = 0;
        score = Double.valueOf(userScore.replace("%",""));
        resultDBJson.put("versionNo",Constant.COGNITION_RESULT_VERSION);
        JSONArray totalScores = new JSONArray();
        JSONObject totalScore = new JSONObject(true);
        String desc = null;
        totalScore.put("totalScore",score);
        if (score>90){
            desc = "优秀等级：在测验中的图片，图片中“鸭蛋”的颜色、明度与邻近的部分都接近相同，您仍然能够以很高的准确率辨认出鸭蛋尖的朝向，说明您的视觉形状辨认能力很强，在生活中辨认物体时，不容易产生视错觉。";
        }else if(score>80 && score<=90){
            desc = "较优秀等级：在测验中的图片，图片中“鸭蛋”的颜色、明度与邻近的部分接近相同，您能够以较高准确率辨认出鸭蛋尖的朝向，说明您具有较强的视觉形状辨认能力，在生活中辨认物体时，一般很少产生视错觉。";
        }else if(score>60 && score<=80){
            desc = "一般等级：在测验中的图片，图片中“鸭蛋”的颜色、明度与邻近的部分接近相同，您辨认出鸭蛋尖的朝向的准确率不是很高也不是很低，说明您的视觉形状辨认能力处于一般水平，在生活中辨认物体时，会产生一定的视错觉但不会很多。";
        }else if(score>50 && score<=60){
            desc = "较差等级：在测验中的图片，图片中“鸭蛋”的颜色、明度与邻近的部分接近相同，您辨认出鸭蛋尖的朝向的正确率较低，说明您的视觉形状辨认能力较差，在生活中辨认物体时，有时会产生视错觉。";
        }else if(score<=50){
            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(score+"%"+";"+diffOne+"%"+";"+diffTwo+"%"+";"+diffThree+"%"+";"+diffFour+"%"+";"+diffFive+"%"+";"+diffSix+"%");
        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);
        JSONObject indicatorD = new JSONObject(true);
        indicatorD.put("name","难度四正确率");
        indicatorD.put("max",100);
        indicator.add(indicatorD);
        JSONObject indicatorE = new JSONObject(true);
        indicatorE.put("name","难度五正确率");
        indicatorE.put("max",100);
        indicator.add(indicatorE);
        JSONObject indicatorF = new JSONObject(true);
        indicatorF.put("name","难度六正确率");
        indicatorF.put("max",100);
        indicator.add(indicatorF);
        chartBody.put("indicator",indicator);
        JSONArray groupData = new JSONArray();
        JSONObject group1 = new JSONObject(true);
        JSONArray group1Value = new JSONArray();
        group1Value.add(diffOne);
        group1Value.add(diffTwo);
        group1Value.add(diffThree);
        group1Value.add(diffFour);
        group1Value.add(diffFive);
        group1Value.add(diffSix);
        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("总分", String.valueOf(score));
        resultDBJson.put("总分", score);
        resultMap.put("难度一正确率", String.valueOf(diffOne));
        resultDBJson.put("难度一正确率", diffOne);
        resultMap.put("难度二正确率", String.valueOf(diffTwo));
        resultDBJson.put("难度二正确率", diffTwo);
        resultMap.put("难度三正确率", String.valueOf(diffThree));
        resultDBJson.put("难度三正确率", diffThree);
        resultMap.put("难度四正确率", String.valueOf(diffFour));
        resultDBJson.put("难度四正确率", diffFour);
        resultMap.put("难度五正确率", String.valueOf(diffFive));
        resultDBJson.put("难度五正确率", diffFive);
        resultMap.put("难度六正确率", String.valueOf(diffSix));
        resultDBJson.put("难度六正确率", diffSix);
        resultMapList = new ArrayList<>();
        resultMapList.add(resultMap);

        String datas = jsonArray.toString();
        ExcelUtil.createExcelFile(SHAPE2Entity.class, shape2Entities, resultMapList, new ExcelClass().contentExcel(Constant.QUEST_TYPE_SHAPE), 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;
    }


}
