package com.tzld.piaoquan.recommend.server.service.recall.strategy;

import com.tzld.piaoquan.recommend.server.service.flowpool.FlowPoolConfigService;
import com.tzld.piaoquan.recommend.server.service.flowpool.FlowPoolConstants;
import com.tzld.piaoquan.recommend.server.service.recall.RecallParam;
import com.tzld.piaoquan.recommend.server.util.JSONUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

import static com.tzld.piaoquan.recommend.server.service.flowpool.FlowPoolConstants.KEY_WITH_LEVEL_FORMAT;

/**
 * @author dyp
 */
@Service
@Slf4j
public class FlowPoolWithLevelRecallStrategy extends AbstractFlowPoolWithLevelRecallStrategy {

    @Autowired
    private FlowPoolConfigService flowPoolConfigService;

    @Override
    Pair<String, String> flowPoolKeyAndLevel(RecallParam param) {
        //# 1. 获取流量池各层级分发概率权重
        Map<String, Double> levelWeightMap = flowPoolConfigService.getLevelWeight();

        // 2. 判断各层级是否有视频需分发
        List<LevelWeight> availableLevels = new ArrayList<>();
        for (Map.Entry<String, Double> entry : levelWeightMap.entrySet()) {
            String levelKey = String.format(KEY_WITH_LEVEL_FORMAT, param.getAppType(), entry.getKey());
            if (redisTemplate.hasKey(levelKey)) {
                LevelWeight lw = new LevelWeight();
                lw.setLevel(entry.getKey());
                lw.setLevelKey(levelKey);
                lw.setWeight(entry.getValue());
                availableLevels.add(lw);
            }
        }
        if (CollectionUtils.isEmpty(availableLevels)) {
            return Pair.of("", "");
        }

        // 3. 根据可分发层级权重设置分发概率
        Collections.sort(availableLevels, Comparator.comparingDouble(LevelWeight::getWeight));

        double weightSum = availableLevels.stream().mapToDouble(o -> o.getWeight()).sum();
        BigDecimal weightSumBD = new BigDecimal(weightSum);
        double level_p_low = 0;
        double weight_temp = 0;
        double level_p_up = 0;
        Map<String, LevelP> level_p_mapping = new HashMap<>();
        for (LevelWeight lw : availableLevels) {
            BigDecimal bd = new BigDecimal(weight_temp + lw.getWeight());
            level_p_up = bd.divide(weightSumBD, 2, RoundingMode.HALF_UP).doubleValue();
            LevelP levelP = new LevelP();
            levelP.setMin(level_p_low);
            levelP.setMax(level_p_up);
            levelP.setLevelKey(lw.getLevelKey());
            level_p_mapping.put(lw.level, levelP);
            level_p_low = level_p_up;

            weight_temp += lw.getWeight();
        }

        // 4. 随机生成[0,1)之间数，返回相应概率区间的key
        double random_p = RandomUtils.nextDouble(0, 1);
        for (Map.Entry<String, LevelP> entry : level_p_mapping.entrySet()) {
            if (random_p >= entry.getValue().getMin()
                    && random_p <= entry.getValue().getMax()) {
                return Pair.of(entry.getValue().getLevelKey(), entry.getKey());
            }
        }
        return Pair.of("", "");
    }

    @Data
    static class LevelWeight {
        private String level;
        private String levelKey;
        private Double weight;
    }

    @Data
    static class LevelP {
        private String levelKey;
        private double min;
        private double max;
    }

    @Override
    public String pushFrom() {
        return FlowPoolConstants.PUSH_FORM;
    }
}
