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.PreViewedService;
import com.tzld.piaoquan.recommend.server.service.ServiceBeanFactory;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class PreViewedFilter extends AbstractFilter<Video> {

    private static final Logger LOGGER = LoggerFactory.getLogger(PreViewedFilter.class);

    private final Set<Long> preViewedVideoIds;

    public PreViewedFilter(FilterConfigInfo filterConfigInfo,
                           RecommendRequest recommendRequest,
                           User user) {
        super(filterConfigInfo, recommendRequest, user);
        PreViewedService preViewedService = ServiceBeanFactory.getBean(PreViewedService.class);
        preViewedVideoIds = preViewedService.getVideoIds(this.requestContext.getAppType(), user.getMid());
    }

    @Override
    public void doFilter(Candidate candidate, List<Video> list) {
        if (StringUtils.isBlank(user.getMid())
                || CollectionUtils.isEmpty(list)) {
            return;
        }

        if (CollectionUtils.isEmpty(preViewedVideoIds)) {
            return;
        }
        list.removeIf(video -> preViewedVideoIds.contains(video.getVideoId()));
    }


}




