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

import com.tzld.piaoquan.recommend.server.service.PreViewedService;
import com.tzld.piaoquan.recommend.server.service.filter.FilterParam;
import com.tzld.piaoquan.recommend.server.service.filter.FilterStrategy;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @author dyp
 */
@Component
public class PreViewedStrategy implements FilterStrategy {
    @Autowired
    private PreViewedService preViewedService;

    @Override
    public List<Long> filter(FilterParam param) {

        if (param == null) {
            return Collections.emptyList();
        }
        if (StringUtils.isBlank(param.getMid())
                || CollectionUtils.isEmpty(param.getVideoIds())) {
            return param.getVideoIds();
        }
        Set<Long> preViewedVideoIds = preViewedService.getVideoIds(param.getAppType(), param.getMid());
        return param.getVideoIds().stream()
                .filter(l -> !preViewedVideoIds.contains(l))
                .collect(Collectors.toList());
    }
}
