package com.td.Dawa.util;

import com.td.Dawa.game.excelcharactercreate.pojo.ExcelCharacterCreate;
import com.td.Dawa.game.excelcharactercreate.vo.ExcelCharacterCreateClientVo;
import com.td.Dawa.game.excelprop.vo.ExcelPropAndProVo;
import org.apache.poi.ss.formula.functions.T;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 概率计算工具类
 */
public class ProbabilityIntervalUtil {

    /**
     * ExcelPropAndProVo 抽中的道具
     *
     * @param list 列表
     * @param unit 单位
     * @return
     */
    public static ExcelPropAndProVo getProp(List<ExcelPropAndProVo> list, Integer unit) {
        if (list.isEmpty()) {
            throw new RuntimeException("list is empty!");
        }
        //奖品总数
        int size = list.size();

        //计算总概率
        double sumProbability = 0d;
        for (ExcelPropAndProVo e : list) {
            sumProbability += e.getProbability();
        }

        //计算每个奖品的概率区间
        //例如奖品A概率区间0-0.1 奖品B概率区间 0.1-0.5 奖品C概率区间0.5-1
        //每个奖品的中奖率越大，所占的概率区间就越大
        List<Double> sortAwardProbabilityList = new ArrayList<Double>(size);
        Double tempSumProbability = 0d;
        for (ExcelPropAndProVo e : list) {
            tempSumProbability += e.getProbability();
            sortAwardProbabilityList.add(tempSumProbability / sumProbability);
        }

        //产生0-1之间的随机数
        //随机数在哪个概率区间内，则是哪个奖品
        double randomDouble = Math.random();
        //加入到概率区间中，排序后，返回的下标则是awardList中中奖的下标
        sortAwardProbabilityList.add(randomDouble);
        Collections.sort(sortAwardProbabilityList);
        int lotteryIndex = sortAwardProbabilityList.indexOf(randomDouble);
        return list.get(lotteryIndex);
    }


    /**
     * ExcelPropAndProVo 生成模型时候权重计算
     *
     * @param list 列表
     * @return
     */
    public static ExcelCharacterCreate getRolePos(List<ExcelCharacterCreate> list) {
        if (list.isEmpty()) {
            throw new RuntimeException("list is empty!");
        }
        //奖品总数
        int size = list.size();

        //计算总概率
        double sumProbability = 0d;
        for (ExcelCharacterCreate e : list) {
            sumProbability += Double.parseDouble(e.getWeight());
        }

        //计算每个奖品的概率区间
        //例如奖品A概率区间0-0.1 奖品B概率区间 0.1-0.5 奖品C概率区间0.5-1
        //每个奖品的中奖率越大，所占的概率区间就越大
        List<Double> sortAwardProbabilityList = new ArrayList<Double>(size);
        Double tempSumProbability = 0d;
        for (ExcelCharacterCreate e : list) {
            tempSumProbability += Double.parseDouble(e.getWeight());
            sortAwardProbabilityList.add(tempSumProbability / sumProbability);
        }

        //产生0-1之间的随机数
        //随机数在哪个概率区间内，则是哪个奖品
        double randomDouble = Math.random();
        //加入到概率区间中，排序后，返回的下标则是awardList中中奖的下标
        sortAwardProbabilityList.add(randomDouble);
        Collections.sort(sortAwardProbabilityList);
        int lotteryIndex = sortAwardProbabilityList.indexOf(randomDouble);
        return list.get(lotteryIndex);
    }
}
