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

import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
import com.tzld.piaoquan.recommend.server.model.Video;
import com.tzld.piaoquan.recommend.server.service.filter.FilterService;
import com.tzld.piaoquan.recommend.server.service.recall.RecallParam;
import com.tzld.piaoquan.recommend.server.service.recall.RecallStrategy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author zhangbo
 */
@Slf4j
@Component
public class RegionRealtimeRecallStrategyV5Hand implements RecallStrategy {
    @ApolloJsonValue("${alg.recall.hand.videos:[]}")
    private List<Long> handVideos;
    @ApolloJsonValue("${alg.recall.special.app&vid:{}}")
    protected Map<String, List<Long>> specialAppVid;
    @Autowired
    protected FilterService filterService;
    @Override
    public List<Video> recall(RecallParam param) {
        List<Long> vids = specialAppVid != null ? specialAppVid.getOrDefault("vid", new ArrayList<>()) : new ArrayList<>();
        List<Video> videosResult = new ArrayList<>();
        if (vids.isEmpty()){
            return videosResult;
        }
        vids.forEach(vid -> {
            Video video = new Video();
            video.setVideoId(vid);
            video.setRovScore(RandomUtils.nextFloat(0, 1));
            video.setPushFrom(pushFrom());
            videosResult.add(video);
        });
        videosResult.sort(Comparator.comparingDouble(o -> -o.getRovScore()));
        long limit = specialAppVid != null ? specialAppVid.getOrDefault("limit", new ArrayList<>(
                Collections.singletonList(1000L)
        )).get(0) : 1000L;
        return videosResult.subList(0, (int) Math.min(videosResult.size(), limit));
    }
    public static final String PUSH_FORM = "recall_strategy_hand";
    @Override
    public String pushFrom(){
        return PUSH_FORM;
    }

}
