package com.tzld.piaoquan.recommend.server.implement.recall;


import com.tzld.piaoquan.recommend.server.framework.candidiate.Candidate;
import com.tzld.piaoquan.recommend.server.framework.common.User;
import com.tzld.piaoquan.recommend.server.framework.recaller.AbstractFilter;
import com.tzld.piaoquan.recommend.server.framework.recaller.FilterConfigInfo;
import com.tzld.piaoquan.recommend.server.gen.recommend.RecommendRequest;
import com.tzld.piaoquan.recommend.server.model.Video;
import com.tzld.piaoquan.recommend.server.service.SpringContextHolder;
import com.tzld.piaoquan.recommend.server.service.filter.FilterParam;
import com.tzld.piaoquan.recommend.server.service.filter.strategy.RecommendStatusStrategy;
import org.apache.commons.collections4.CollectionUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class RecommendStatusFilter extends AbstractFilter<Video> {

    private final RecommendStatusStrategy recommendStatusStrategy;

    public RecommendStatusFilter(FilterConfigInfo filterConfigInfo,
                                 RecommendRequest recommendRequest,
                                 User user) {
        super(filterConfigInfo, recommendRequest, user);
        recommendStatusStrategy = SpringContextHolder.getBean(RecommendStatusStrategy.class);
    }

    @Override
    public void doFilter(Candidate candidate, List<Video> videos) {
        if (CollectionUtils.isEmpty(videos)) {
            return;
        }
        FilterParam filterParam = new FilterParam();
        filterParam.setVideoIds(videos.stream().map(Video::getVideoId).collect(Collectors.toList()));
        List<Long> videoIdList = recommendStatusStrategy.filter(filterParam);
        Set<Long> videoIdSet = new HashSet<>(videoIdList);
        videos.removeIf(video -> !videoIdSet.contains(video.getVideoId()));
    }

}




