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

import com.google.common.collect.Lists;
import com.tzld.piaoquan.recommend.server.model.Video;
import com.tzld.piaoquan.recommend.server.service.filter.FilterParam;
import com.tzld.piaoquan.recommend.server.service.filter.FilterResult;
import com.tzld.piaoquan.recommend.server.service.filter.RegionFilterService;
import com.tzld.piaoquan.recommend.server.service.recall.FilterParamFactory;
import com.tzld.piaoquan.recommend.server.service.recall.RecallParam;
import com.tzld.piaoquan.recommend.server.service.recall.RecallStrategy;
import com.tzld.piaoquan.recommend.server.service.score.ScorerUtils;
import com.tzld.piaoquan.recommend.server.service.score4recall.ScorerPipeline4Recall;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author sunxy
 */
@Component
public class ShareDeepRecallStrategy implements RecallStrategy {

    @Value("${ShareDeepRecallStrategyRecallNumber:500}")
    private String shareDeepRecallStrategyRecallNumber;
    @Value("${ShareDeepRecallStrategyRecallNumberAfterFilter:20}")
    private String shareDeepRecallStrategyRecallNumberAfterFilter;

    public static final String PUSH_FORM = "share_deep_recall_strategy";

    @Autowired
    private RegionFilterService filterService;


    @Override
    public List<Video> recall(RecallParam param) {
        Map<String, String> param4Model = new HashMap<>(1);
        param4Model.put("limit", shareDeepRecallStrategyRecallNumber);
        ScorerPipeline4Recall pipeline = ScorerUtils.getScorerPipeline4Recall("feeds_score_config_share_deep.conf");
        List<List<Pair<Long, Double>>> results = pipeline.recall(param4Model);
        Map<Long, Double> videoMap = new LinkedHashMap<>();
        List<Pair<Long, Double>> result = results.get(0);
        for (int i = 1; i < results.size(); ++i) {
            result.addAll(results.get(i));
        }
        for (Pair<Long, Double> v : result) {
            videoMap.put(v.getLeft(), v.getRight());
        }
        FilterParam filterParam = FilterParamFactory.create(param, Lists.newArrayList(videoMap.keySet()));
        FilterResult filterResult = filterService.filter(filterParam);
        List<Video> videosResult = new ArrayList<>();
        if (filterResult != null && CollectionUtils.isNotEmpty(filterResult.getVideoIds())) {
            filterResult.getVideoIds().stream()
                    .sorted(Comparator.comparingDouble(vid -> videoMap.getOrDefault(vid, 0.0)).reversed())
                    .limit(Integer.parseInt(shareDeepRecallStrategyRecallNumberAfterFilter)).forEach(vid -> {
                        Video video = new Video();
                        video.setVideoId(vid);
                        video.setAbCode(param.getAbCode());
                        video.setPushFrom(pushFrom());
                        videosResult.add(video);
                    });
        }
        return videosResult;
    }

    @Override
    public String pushFrom() {
        return PUSH_FORM;
    }
}
