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

import com.google.common.collect.Lists;
import com.tzld.piaoquan.recommend.server.common.enums.TabType;
import com.tzld.piaoquan.recommend.server.model.Tab;
import com.tzld.piaoquan.recommend.server.model.Video;
import com.tzld.piaoquan.recommend.server.service.rank.RankParam;
import com.tzld.piaoquan.recommend.server.service.rank.RankResult;
import com.tzld.piaoquan.recommend.server.service.rank.RankService;
import com.tzld.piaoquan.recommend.server.service.recall.strategy.*;
import com.tzld.piaoquan.recommend.server.util.RecallUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class RankSequenceStrategy extends RankService {

    private static final List<String> territoryRecall = Lists.newArrayList(
            CityRovnAllRovRecallStrategy.PUSH_FROM,
            CityRovnRecallStrategy.PUSH_FROM,
            RegionHRecallStrategy.PUSH_FORM,
            RegionRealtimeRecallStrategyV1.PUSH_FORM
    );
    private static final List<String> personalizationRecall = Lists.newArrayList(
            PersonalizationTabRovnRecallStrategy.PUSH_FROM
    );

    @Override
    public List<Video> mergeAndRankRovRecall(RankParam param) {
        List<Video> rovRecallRank = new ArrayList<>();
        Tab tab = param.getTab();
        if (Objects.nonNull(tab) && StringUtils.isNotBlank(tab.getType())) {
            TabType type = TabType.getByType(tab.getType());
            if (Objects.equals(TabType.territory, type)) {
                this.extractRecall(param, territoryRecall, rovRecallRank);
            } else if (Objects.equals(TabType.personalization, type)) {
                this.extractRecall(param, personalizationRecall, rovRecallRank);
            }
        }

        // 记录召回源中的视频
        this.rankBeforePostProcessor(rovRecallRank);
        return rovRecallRank;
    }

    private void extractRecall(RankParam param, List<String> recalls, List<Video> videos) {
        Set<Long> setVideo = new HashSet<>();
        setVideo.add(param.getHeadVid());
        for (String recall : recalls) {
            RecallUtils.extractRecall(100, param, recall, setVideo, videos);
        }
    }

    @Override
    public RankResult mergeAndSort(RankParam param, List<Video> rovRecallRank, List<Video> flowPoolRank, List<Video> douHotFlowPoolRank) {
        return new RankResult(rovRecallRank);
    }
}
