package com.tzld.piaoquan.recommend.server.service.rank.processor;

import com.tzld.piaoquan.recommend.server.model.Video;
import com.tzld.piaoquan.recommend.server.util.ProbabilityCalculator;

import java.util.*;
public class RankProcessorTagFilter {

    public static void processor(List<Video> rov, List<Video> flow,List<Video> douHot, Map<String, Map<String, String>> rules) {

        Set<String> filterTags = new HashSet<>();
        Random random = new Random();

        // 遍历每一个规则条目
        for (Map.Entry<String, Map<String, String>> entry : rules.entrySet()) {
            String key = entry.getKey();
            Map<String, String> rule = entry.getValue();
            String filterRate = rule.get("filter_rate");
            String filterDate = rule.get("filter_date");

            // 使用 calculateCurrentProbability 方法计算当前概率
            double currentProbability = ProbabilityCalculator.calculateCurrentProbability(filterRate, filterDate);

            // 生成随机浮点数并与 filter_rate 比较
            if (currentProbability > 0) { // 确保有有效的概率
                double randomValue = random.nextDouble();
                if (randomValue < currentProbability) {
                    filterTags.add(key);
                }
            }
        }

        if (filterTags.isEmpty()) {
            return;
        }

        //执行过滤
        Iterator<Video> iterator = rov.iterator();
        while (iterator.hasNext()) {
            Video video = iterator.next();
            List<String> tags = video.getTags();
            for (String tag : tags) {
                if (filterTags.contains(tag)) {
                    iterator.remove();
                }
            }
        }

        iterator = flow.iterator();
        while (iterator.hasNext()) {
            Video video = iterator.next();
            List<String> tags = video.getTags();
            for (String tag : tags) {
                if (filterTags.contains(tag)) {
                    iterator.remove();
                }
            }
        }

        iterator = douHot.iterator();
        while (iterator.hasNext()) {
            Video video = iterator.next();
            List<String> tags = video.getTags();
            for (String tag : tags) {
                if (filterTags.contains(tag)) {
                    iterator.remove();
                }
            }
        }
    }

}
