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

import com.tzld.piaoquan.recommend.server.model.Video;
import com.tzld.piaoquan.recommend.server.service.FeatureService;
import com.tzld.piaoquan.recommend.server.service.ServiceBeanFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExtractVideoMergeCate {

    public static void addMergeCate(List<Video> rovList, List<Video> flowVideos) {

        FeatureService featureService = ServiceBeanFactory.getBean(FeatureService.class);

        List<String> vids = new ArrayList<>();
        rovList.stream().map(Video::getVideoId).map(String::valueOf).forEach(vids::add);
        flowVideos.stream().map(Video::getVideoId).map(String::valueOf).forEach(vids::add);
        Map<String, Map<String, Map<String, String>>> videoBaseInfo = featureService.getVideoBaseInfo(null, vids);

        for (Video video : rovList) {
            String vidStr = String.valueOf(video.getVideoId());

            String mergeCate2 = ExtractVideoMergeCate.parseMergeCate2(vidStr, videoBaseInfo);
            video.getMergeCateList().add(mergeCate2);

        }
    }

    public static String parseMergeCate2(String vidStr, Map<String, Map<String, Map<String, String>>> featureMap) {
        if (!featureMap.containsKey(vidStr)) {
            return "";
        }

        Map<String, Map<String, String>> vidFeature = featureMap.get(vidStr);
        if (!vidFeature.containsKey("alg_vid_feature_basic_info")) {
            return "";
        }

        Map<String, String> basicInfoMap = vidFeature.get("alg_vid_feature_basic_info");
        return basicInfoMap.getOrDefault("merge_second_level_cate", "");
    }

    public static void addOtherParam(List<Video> videos, Map<String, Map<String, Map<String, String>>> featureMap) {
        for (Video video : videos) {
            String videoId = String.valueOf(video.getVideoId());
            if (!featureMap.containsKey(videoId)) {
                continue;
            }
            Map<String, Map<String, String>> vidFeature = featureMap.get(videoId);
            Map<String, String> otherParam = vidFeature.getOrDefault("alg_vid_feature_basic_info", new HashMap<>());
            video.getOtherParam().putAll(otherParam);
        }
    }
}
