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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.dto.scale.NewResultDto;
import com.rf.psychological.dao.model.AnswerEntity;
import com.rf.psychological.dao.model.DimensionEntity;
import com.rf.psychological.scale.dao.model.MBTIResultDetail;
import com.rf.psychological.scale.dao.model.ScaleEntity;
import com.rf.psychological.scale.dao.model.ScaleMarksEntity;
import com.rf.psychological.scale.dao.repository.MBTIResultDetailRepository;
import com.rf.psychological.scale.scaleresult.ScaleConstant;
import com.rf.psychological.scale.service.MBTIResultDetailService;
import com.rf.psychological.scale.service.impl.MBTIResultDetailServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zsy
 * @description:职业性格测试
 * @date 2021/7/20 15:55
 */
@Slf4j
public class MBTIScale  {

    private JSONArray jsonArray;
    private JSONObject resultJson;
    private MBTIResultDetailService detailService;

    public MBTIScale(JSONArray jsonArray, JSONObject resultJson,MBTIResultDetailService detailService) {
        this.jsonArray = jsonArray;
        this.resultJson = resultJson;
        this.detailService = detailService;
    }

    public JSONObject scaleCalculate() throws Exception {
        return null;
    }
    public JSONObject scaleCalculateV2() throws Exception {
        //获取答案列表
        List<AnswerEntity> answerEntities = (List<AnswerEntity>) resultJson.get("answerEntities");
        //获取维度信息列表
        List<DimensionEntity> dimensionEntities = (List<DimensionEntity>) resultJson.get("dimensionEntities");
        String scoreResult = getAnswerScore(answerEntities,dimensionEntities);
        resultJson.put("scoreResult",scoreResult);
        if (scoreResult != null){
            MBTIResultDetail detail = detailService.findByFlag(scoreResult);
            resultJson.put("scoreDetail",detail);
        }
        JSONObject returnJson = new JSONObject(true);
        returnJson.put("resultJson", resultJson);
        return returnJson;
    }

    private String getAnswerScore(List<AnswerEntity> answerEntities, List<DimensionEntity> dimensionEntities) {
        //外倾(E)得分,内倾(I)得分
        int scoreE = 0,scoreI = 0;
        //直觉(N)得分,感觉(S)得分
        int scoreN = 0,scoreS = 0;
        //情感(F)得分,思维(T)得分
        int scoreF = 0,scoreT = 0;
        //判断(J)得分,知觉(P)得分
        int scoreJ = 0,scoreP = 0;

        if (CollectionUtils.isEmpty(dimensionEntities)){
            log.error("维度参数为空");
            return null;
        }
        //获取各维度选项得分
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            for (int y = 0; y < dimensionEntities.size(); y++) {
                DimensionEntity dimensionEntity = dimensionEntities.get(y);
                String questionNos = dimensionEntity.getQuestionNo();
                String[] questionNo = questionNos.split("；");
                for (String question : questionNo) {
                    if (question.equals(jsonObject1.getString("questionNo"))) {
                        for (AnswerEntity answerEntity : answerEntities) {
                            if (answerEntity.getQuestionNo().equals(question) && answerEntity.getName().equals(jsonObject1.getString("checkItems"))) {
                                if (dimensionEntity.getName().equals("外倾-内倾")){
                                    if (answerEntity.getOptions().equals("A")){
                                        scoreE += Double.parseDouble(answerEntity.getScore());
                                    }else {
                                        scoreI += Double.parseDouble(answerEntity.getScore());
                                    }
                                }else if (dimensionEntity.getName().equals("感觉-直觉")){
                                    if (answerEntity.getOptions().equals("A")){
                                        scoreS += Double.parseDouble(answerEntity.getScore());
                                    }else {
                                        scoreN += Double.parseDouble(answerEntity.getScore());
                                    }
                                }else if (dimensionEntity.getName().equals("思维-情感")){
                                    if (answerEntity.getOptions().equals("A")){
                                        scoreT += Double.parseDouble(answerEntity.getScore());
                                    }else {
                                        scoreF += Double.parseDouble(answerEntity.getScore());
                                    }
                                }else {
                                    if (answerEntity.getOptions().equals("A")){
                                        scoreJ += Double.parseDouble(answerEntity.getScore());
                                    }else {
                                        scoreP += Double.parseDouble(answerEntity.getScore());
                                    }
                                }
                            }
                        }
                        break;
                    }
                }
            }
        }
        return getPersonalityType(scoreE,scoreI,scoreN,scoreS,scoreF,scoreT,scoreJ,scoreP);
    }

    /**
     * 根据各个指标得分计算维度
     * @param scoreE 外倾(E)得分
     * @param scoreI 内倾(I)得分
     * @param scoreN 直觉(N)得分
     * @param scoreS 感觉(S)得分
     * @param scoreF 情感(F)得分
     * @param scoreT 思维(T)得分
     * @param scoreJ 判断(J)得分
     * @param scoreP 知觉(P)得分
     * @return
     */
    private String getPersonalityType(int scoreE, int scoreI, int scoreN, int scoreS, int scoreF, int scoreT, int scoreJ, int scoreP) {
        StringBuffer scaleResult = new StringBuffer();
        //“外倾/内倾”=(内倾-外倾)/21*10 (正分为内倾I， 负分为外倾E)
        if (((double)(scoreI-scoreE)/21*10)>0){
            scaleResult.append("I");
        }else {
            scaleResult.append("E");
        }
        //“感觉/直觉”=(感觉-直觉)/26*10(正分为感觉S，负分为直觉N)
        if (((double)(scoreS-scoreN)/26*10)>0){
            scaleResult.append("S");
        }else {
            scaleResult.append("N");
        }
        //“思考/情感”=(思考-情感)/24*10(正分为思考T，负分为情感F)
        if (((double)(scoreT-scoreF)/24*10)>0){
            scaleResult.append("T");
        }else {
            scaleResult.append("F");
        }
        //“知觉/判断”=(知觉-判断)/22*10(正分为感性P，负分为判断J)
        if (((double)(scoreP-scoreJ)/22*10)>0){
            scaleResult.append("P");
        }else {
            scaleResult.append("J");
        }
        return scaleResult.toString();
    }


}
