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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.model.AnswerEntity;
import com.rf.psychological.dao.model.DimensionEntity;
import com.rf.psychological.scale.dao.model.ScaleMarksEntity;
import com.rf.psychological.scale.dao.model.WarnEntity;
import com.rf.psychological.scale.service.DimensionOrgService;
import com.rf.psychological.utils.Constant;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mimang
 * @description:颜色影响力量表
 */
public class ColorImpactScale extends BaseScale {

    public ColorImpactScale(JSONArray jsonArray, JSONObject resultJson) {
        super(jsonArray, resultJson);
    }

    public  JSONObject scaleCalculate() throws Exception {

        //总分
        double score = 0;
        Map<String, String> resultMap0 = new LinkedHashMap<>();
        List<Map<String, String>> resultMapList = new ArrayList<>();
        //获取答案列表
        List<AnswerEntity> answerEntities = (List<AnswerEntity>) resultJson.get("answerEntities");
        //获取评分规则列表
        List<ScaleMarksEntity> scaleMarksEntities = (List<ScaleMarksEntity>) resultJson.get("scaleMarksEntities");
        //获取维度信息列表
        List<DimensionEntity> dimensionEntities = (List<DimensionEntity>) resultJson.get("dimensionEntities");
        String institutionNo = resultJson.getString("institutionNo");
        String userId = resultJson.getString("userId");
        DimensionOrgService dimensionOrgService = (DimensionOrgService) resultJson.get("dimensionOrgService");
        //List<WarnEntity> warnEntityList = new ArrayList<>();
        double[] dimensionScore = new double[dimensionEntities.size()];
        //已知该量表无维度信息，暂不做维度处理
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            for (AnswerEntity answerEntity : answerEntities) {
                if (answerEntity.getQuestionNo().equals(String.valueOf(i + 1))) {
                    //该量表中16，17题为填空题，只要排序了就给1分
                    String questionType = jsonObject1.getString("questionType");
                    if (Constant.QUESTION_TYPE_TWO.equals(questionType)){
                        score += Double.valueOf(answerEntity.getScore());
                    }else {
                        if (answerEntity.getName().replaceAll("\\s", "").equals(jsonObject1.getString("checkItems").replaceAll("\\s", ""))) {
                            score += Double.valueOf(answerEntity.getScore());
                        }
                    }

                }
            }
        }

        resultMap0.put("总分", String.valueOf(score));
        resultJson.put("总分", String.valueOf(score));
        if (scaleMarksEntities.size() > 0) {
            for (ScaleMarksEntity scaleMarksEntity : scaleMarksEntities) {
                if (score <= Integer.valueOf(scaleMarksEntity.getScoreEnd()) && score >= Integer.valueOf(scaleMarksEntity.getScoreStart())) {
                    if (!scaleMarksEntity.getSymptom().equals("无")) {
                        resultMap0.put("结论", scaleMarksEntity.getSymptom()+":"+scaleMarksEntity.getImprovementSuggestions());
                        resultJson.put("结论", scaleMarksEntity.getSymptom()+":"+scaleMarksEntity.getImprovementSuggestions());
                    }
//                    if (scaleMarksEntity.getImprovementSuggestions() != null && !scaleMarksEntity.getImprovementSuggestions().equals("无")) {
//                        resultMap0.put("建议", scaleMarksEntity.getImprovementSuggestions());
//                        resultJson.put("建议", scaleMarksEntity.getImprovementSuggestions());
//                    }
                }
            }
        }
        resultMapList.add(resultMap0);

        JSONObject returnJson = new JSONObject(true);
        returnJson.put("resultMapList", resultMapList);
        //resultJson.put("warnEntityList",warnEntityList);
        returnJson.put("resultJson", resultJson);
        return returnJson;
    }
}
