package com.rf.psychological.scale.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.rf.psychological.scale.dao.model.MBTIResultDetail;
import com.rf.psychological.scale.dao.repository.MBTIResultDetailRepository;
import com.rf.psychological.scale.service.MBTIResultDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Description:MBTI结论详情
 * @Author: mimang
 * @Date: 2024/6/3
 */
@Service
public class MBTIResultDetailServiceImpl implements MBTIResultDetailService {

    @Autowired
    private MBTIResultDetailRepository detailRepository;

    @Override
    public MBTIResultDetail findByFlag(String flag) {
        MBTIResultDetail detail1 = detailRepository.findByFlag(flag);
        MBTIResultDetail detail = new MBTIResultDetail();
        BeanUtil.copyProperties(detail1,detail);
        if (detail != null){
            detail.setConclusion(this.getRandomResult(detail.getConclusion()));
            detail.setCharacteristic(this.getRandomResultByNum(detail.getCharacteristic(),3));
            detail.setEvaluate(this.getRandomResult(detail.getEvaluate()));
            detail.setPursuit(this.getRandomResult(detail.getPursuit()));
            detail.setAdvantage(this.getRandomResult(detail.getAdvantage()));
            detail.setDisadvantages(this.getRandomResult(detail.getDisadvantages()));
            detail.setRecommendation(this.getRandomResult(detail.getRecommendation()));
            detail.setSuggest(this.getRandomResult(detail.getSuggest()));
            detail.setRevered(this.getRandomResult(detail.getRevered()));
            detail.setRepresentative(this.getRandomResultByNum(detail.getRepresentative(),4));
        }
        return detail;
    }

    private String getRandomResult(String str) {
        List<String> list = Arrays.asList(str.split("#;"));
        if (list.size()>1){
            int size = list.size();
            int num = RandomUtil.randomInt(0,size-1);
            String text = list.get(num);
            return text.contains("#")?text.replace("#",""):text;
        }else {
            return str;
        }
    }

    /**
     * 随机获取到一定数量的字符串
     * @param str 待处理字符串
     * @param num 数量
     * @return
     */
    private String getRandomResultByNum(String str,int num) {
        List<String> list = Arrays.asList(str.split("、"));
        if (list.size()>num){
            String result = null;
            int random =  RandomUtil.randomInt(0,list.size()-num);
            for (int i =random;i<random+num;i++){
                String text = list.get(i);
                if (result == null){
                    result = text;
                }else {
                    result = result+"、"+text;
                }
            }
            return result;
        }else {
            return str;
        }
    }



    private int getOnlyRandom(List numList,int size){
        int random =  RandomUtil.randomInt(0,size-1);
        if (numList.contains(random)){
            return  getOnlyRandom(numList,size);
        }else {
            numList.add(random);
            return random;
        }
    }

    @Override
    public void save(MBTIResultDetail detail) {
        detailRepository.save(detail);
    }
}
