package com.tzld.piaoquan.recommend.server.implement.recall;


import com.google.common.collect.Lists;
import com.google.common.hash.Hashing;
import com.tzld.piaoquan.recommend.server.common.enums.AppTypeEnum;
import com.tzld.piaoquan.recommend.server.framework.candidiate.Candidate;
import com.tzld.piaoquan.recommend.server.framework.common.User;
import com.tzld.piaoquan.recommend.server.framework.recaller.AbstractFilter;
import com.tzld.piaoquan.recommend.server.framework.recaller.FilterConfigInfo;
import com.tzld.piaoquan.recommend.server.gen.recommend.RecommendRequest;
import com.tzld.piaoquan.recommend.server.model.Video;
import com.tzld.piaoquan.recommend.server.service.PreViewedService;
import com.tzld.piaoquan.recommend.server.service.filter.FilterParam;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.*;

public class AllowListFilter extends AbstractFilter<Video> {

    @Autowired
    @Qualifier("longVideoRedisTemplate")
    private RedisTemplate<String, String> redisTemplate;

    private static final String VIDEO_ALLOW_LIST_BITMAP_KEY_SET_PREFIX = "movie:videoid:allowSet:";
    private static final String RELIGION_VIDEO_ALLOW_LIST_BITMAP_KEY = "mp:religion:allowlist:videoid:bitmap";

    public AllowListFilter(FilterConfigInfo filterConfigInfo,
                           RecommendRequest recommendRequest,
                           User user) {
        super(filterConfigInfo, recommendRequest, user);

    }



    @Override
    public void doFilter(Candidate candidate, List<Video> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        List<Video> result = Lists.newArrayList();
        for (Video video : list) {
            if (!isAllowList(user, video)) {
                result.add(video);
            }
        }
        list.clear();
        list.addAll(result);
    }


    public boolean isAllowList(User user, Video video) {
        //不是新小程序 在白名单则不显示
        Set<Long> retainVideoIds = new LinkedHashSet<>();
        int appType = requestContext.getAppType();
        if ( appType != AppTypeEnum.WAN_NENG_VIDEO.getCode()
                && appType != AppTypeEnum.LAO_HAO_KAN_VIDEO.getCode()
                && appType != AppTypeEnum.ZUI_JING_QI.getCode()
                && appType!= AppTypeEnum.H5.getCode()) {
            // 如果不在新小程序白名单，则允许
            return !isMemberOfVideoAllowList(video.getVideoId());

        } else if (appType == AppTypeEnum.WAN_NENG_VIDEO.getCode()
                || appType == AppTypeEnum.LAO_HAO_KAN_VIDEO.getCode()
                || appType == AppTypeEnum.ZUI_JING_QI.getCode()
                || appType == AppTypeEnum.H5.getCode()) {
            return !isMemberOfReligionVideoAllowList(video.getVideoId());

        }
        return true;
    }

    private boolean isMemberOfVideoAllowList(Long videoId) {
        if (Objects.isNull(videoId)) {
            return false;
        }
        try {
            int newIdx = Math.abs(Hashing.murmur3_32().hashLong(videoId).asInt()) % 100;
            String newPrefix = VIDEO_ALLOW_LIST_BITMAP_KEY_SET_PREFIX + newIdx;
            Boolean result = redisTemplate.opsForSet().isMember(newPrefix, String.valueOf(videoId));
            return result;
        } catch (Exception e) {
            LOGGER.error("isMemberOfVideoAllowList error {}", videoId, e);
        }
        return false;
    }

    public boolean isMemberOfReligionVideoAllowList(Long videoId) {
        if (Objects.isNull(videoId)) {
            return false;
        }
        try {
            return redisTemplate.opsForValue().getBit(RELIGION_VIDEO_ALLOW_LIST_BITMAP_KEY, videoId);
        } catch (Exception e) {
            LOGGER.error("isMemberOfReligionVideoAllowList error {}", e, videoId);
        }
        return false;
    }





}




