package cn.reghao.tnb.content.app.vod.service;

import cn.reghao.tnb.content.api.dto.VideoCard;
import cn.reghao.tnb.content.app.ContentApplication;
import cn.reghao.tnb.content.app.vod.db.mapper.*;
import cn.reghao.tnb.content.app.vod.model.po.*;
import cn.reghao.tnb.content.app.util.redis.ds.RedisHash;
import cn.reghao.tnb.content.app.util.redis.ds.RedisSet;
import cn.reghao.tnb.content.app.util.redis.ds.RedisSortedSet;
import cn.reghao.tnb.content.app.vod.model.vo.BannerVideoVO;
import cn.reghao.tnb.content.app.vod.service.rcmd.RedisKeys;
import cn.reghao.tnb.content.app.vod.service.rcmd.UserInterestBased;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

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

/**
 * @author reghao
 * @date 2023-04-24 03:32:04
 */
@Slf4j
@ActiveProfiles("dev")
@SpringBootTest(classes = ContentApplication.class)
public class VideoPostTest {
    @Autowired
    VideoPostMapper videoPostMapper;
    @Autowired
    VideoStatisticMapper videoStatisticMapper;
    @Autowired
    VideoCategoryMapper videoCategoryMapper;
    @Autowired
    VideoPostService videoPostService;

    @Autowired
    RedisSortedSet sortedSet;
    @Autowired
    RedisHash<VideoCard> redisHash;
    @Autowired
    VideoPostQuery videoPostQuery;
    @Test
    public void getPageByTag() {
        int pageSize = 100;
        int nextId = 0;
        // List<VideoPost> videoPosts = videoPostMapper.findAllById(pageSize, nextId);

        String bannerKey = "tnb:content:video:banner";
        List<VideoStatistic> list = videoStatisticMapper.findAll();
        for (VideoStatistic videoStatistic : list) {
            String videoId = videoStatistic.getVideoId();
            int hotIndex = getHotIndex(videoStatistic);
            sortedSet.add(bannerKey, videoId, hotIndex);
        }
    }

    private int getHotIndex(VideoStatistic videoStatistic) {
        String videoId = videoStatistic.getVideoId();
        VideoPost videoPost = videoPostMapper.findByVideoId(videoId);
        LocalDateTime publishAt = videoPost.getPublishAt();
        long publishBy = videoPost.getPublishBy();

        double coin = videoStatistic.getCoin().doubleValue();
        double favorite = videoStatistic.getFavorite().doubleValue();
        double danmaku = videoStatistic.getDanmaku().doubleValue();
        double comment = videoStatistic.getComment().doubleValue();
        double view = videoStatistic.getView().doubleValue();
        double thumbUp = videoStatistic.getThumbUp().doubleValue();
        double share = videoStatistic.getShare().doubleValue();

        double weight = coin*0.4 + favorite*0.3 + danmaku*0.4 + comment*0.4 + view*0.25 + thumbUp*0.4 + share*0.6;
        if (publishAt.plusDays(1).isAfter(LocalDateTime.now())) {
            weight = weight*1.5;
        }

        int weight1 = (int) Math.floor(weight);
        return weight1;
    }

    @Autowired
    VideoTagMapper videoTagMapper;
    @Autowired
    VideoPostTagMapper videoPostTagMapper;
    @Autowired
    UserInterestBased userInterestBased;
    @Test
    public void addTags() {
        /*List<GroupCount> list = videoPostTagMapper.findByGroupBy();
        List<String> list0 = list.stream().map(groupCount -> {
            String tagId = groupCount.getId();
            VideoTag videoTag = videoTagMapper.findByTagId(tagId);
            if (videoTag == null) {
                return tagId;
            }

            return null;
        }).filter(Objects::nonNull).collect(Collectors.toList());

        list0.forEach(tagId -> {
            videoTagMapper.deleteByTagId(tagId);
            videoPostTagMapper.deleteByTagId(tagId);
            log.info("delete tag id -> {}", tagId);
        });*/

        /*Map<String, List<String>> map = new HashMap<>();
        List<VideoTag> list = videoTagMapper.findAll();
        for (VideoTag videoTag : list) {
            String tagId = videoTag.getTagId();
            List<String> videoIds = videoPostTagMapper.findByTagId(tagId);
            if (videoIds.size() > 3) {
                userInterestBased.addCategoryItems1(tagId, videoIds);
                log.info("add {} items with tag id {}", videoIds.size(), tagId);
            }
        }*/
    }

    @Test
    public void addInterests() {
        long userId = 10002L;
        List<String> tagIds = videoTagMapper.findRandomTags(1000);
        for (String tagId : tagIds) {
            userInterestBased.addUserInterests(userId, tagId);
        }
    }

    @Autowired
    RedisSet redisSet;
    @Test
    public void setRcmd() {
        long userId = 10002L;
        String itemsKey = RedisKeys.getUserItemKey(userId);
        Set<String> videoIds = userInterestBased.getUserInterestItems(userId);
        if (!videoIds.isEmpty()) {
            List<VideoCard> videoCards = videoPostQuery.getVideoCards(new ArrayList<>(videoIds));
            VideoCard[] arr = videoCards.toArray(new VideoCard[0]);
            redisSet.sadd(itemsKey, arr);
        }
    }

    @Test
    public void getRcmd() {
        long userId = 10002L;
        String itemsKey = RedisKeys.getUserItemKey(userId);
        List<VideoCard> videoCards = new ArrayList<>();
        List<Object> list = redisSet.spop(itemsKey, 12);
        for (Object obj : list) {
            if (obj instanceof VideoCard) {
                videoCards.add((VideoCard) obj);
            }
        }

        System.out.println();
    }

    @Autowired
    BannerVideoMapper bannerVideoMapper;
    @Test
    public void getBannerVideo() {
        List<VideoPost> list = videoPostMapper.findAll();
        long createAt = System.currentTimeMillis();
        List<BannerVideo> bannerVideoList = list.stream().map(videoPost -> {
            return new BannerVideo(videoPost, createAt);
        }).collect(Collectors.toList());

        if (!bannerVideoList.isEmpty()) {
            bannerVideoMapper.saveAll(bannerVideoList.subList(0, 100));
        }

        List<BannerVideoVO> list1 =  bannerVideoMapper.findBannerVideos();
        System.out.println();
    }
}
