package com.tzld.piaoquan.recommend.server.util;

import com.tzld.piaoquan.recommend.server.model.Video;
import com.tzld.piaoquan.recommend.server.service.rank.RankParam;
import com.tzld.piaoquan.recommend.server.service.recall.RecallResult;
import com.tzld.piaoquan.recommend.server.service.recall.strategy.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

public class RecallUtils {
    private static void removeDuplicate(List<Video> rovRecallRank) {
        if (CollectionUtils.isNotEmpty(rovRecallRank)) {
            Set<Long> videoIds = new HashSet<>();
            Iterator<Video> ite = rovRecallRank.iterator();
            while (ite.hasNext()) {
                Video v = ite.next();
                if (videoIds.contains(v.getVideoId())) {
                    ite.remove();
                    continue;
                }
                videoIds.add(v.getVideoId());
            }
        }
    }

    private static List<Video> extractAndSort(RankParam param, String pushFrom) {
        if (param == null
                || param.getRecallResult() == null
                || CollectionUtils.isEmpty(param.getRecallResult().getData())) {
            return Collections.emptyList();
        }
        Optional<RecallResult.RecallData> data = param.getRecallResult().getData().stream()
                .filter(d -> d.getPushFrom().equals(pushFrom))
                .findFirst();
        if (data.isPresent()
                && data.get() != null
                && CollectionUtils.isNotEmpty(data.get().getVideos())) {
            List<Video> result = data.get().getVideos();
            Collections.sort(result, Comparator.comparingDouble(o -> -o.getRovScore()));
            return result;
        }
        return Collections.emptyList();
    }

    public static void extractOldSpecialRecall(int sizeReturn, RankParam param, Set<Long> setVideo, List<Video> rovRecallRank) {
        List<Video> oldRovs = new ArrayList<>();
        oldRovs.addAll(extractAndSort(param, RegionHRecallStrategy.PUSH_FORM));
        oldRovs.addAll(extractAndSort(param, RegionHDupRecallStrategy.PUSH_FORM));
        oldRovs.addAll(extractAndSort(param, Region24HRecallStrategy.PUSH_FORM));
        oldRovs.addAll(extractAndSort(param, RegionRelative24HRecallStrategy.PUSH_FORM));
        oldRovs.addAll(extractAndSort(param, RegionRelative24HDupRecallStrategy.PUSH_FORM));
        removeDuplicate(oldRovs);
        oldRovs = oldRovs.stream().filter(r -> !setVideo.contains(r.getVideoId())).collect(Collectors.toList());
        List<Video> v0 = oldRovs.size() <= sizeReturn
                ? oldRovs
                : oldRovs.subList(0, sizeReturn);

        rovRecallRank.addAll(v0);
        setVideo.addAll(v0.stream().map(Video::getVideoId).collect(Collectors.toSet()));
    }

    public static void extractRecall(int recallNum, RankParam param, String pushFrom, Set<Long> setVideo, List<Video> rovRecallRank) {
        if (recallNum > 0) {
            List<Video> list = extractAndSort(param, pushFrom);
            list = list.stream().filter(r -> !setVideo.contains(r.getVideoId())).collect(Collectors.toList());
            list = list.subList(0, Math.min(recallNum, list.size()));
            rovRecallRank.addAll(list);
            setVideo.addAll(list.stream().map(Video::getVideoId).collect(Collectors.toSet()));
        }
    }

    public static String douHotProvinceConvert(String province) {
        if (StringUtils.isBlank(province)) {
            return "";
        }
        if (StringUtils.startsWith(province, "西藏")) {
            return "西藏";
        }
        if (StringUtils.startsWith(province, "新疆")) {
            return "新疆";
        }

        if (StringUtils.startsWith(province, "内蒙古")) {
            return "内蒙古";
        }

        if (StringUtils.endsWith(province, "市")) {
            return province.replaceAll("市$", "");
        }
        return province.replaceAll("省$", "");
    }
}
