package cn.reghao.tnb.content.app.vod.db.repository;

import cn.reghao.file.api.dto.VideoMeta;
import cn.reghao.tnb.common.db.GroupCount;
import cn.reghao.tnb.content.app.vod.db.mapper.VideoFileMapper;
import cn.reghao.tnb.content.app.vod.db.mapper.VideoPostMapper;
import cn.reghao.tnb.content.app.vod.db.mapper.VideoStatisticMapper;
import cn.reghao.tnb.content.app.vod.model.po.VideoFile;
import cn.reghao.tnb.content.app.vod.model.po.VideoPost;
import cn.reghao.tnb.content.app.vod.model.po.VideoStatistic;
import cn.reghao.tnb.content.app.vod.model.dto.VideoUrlDto;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author reghao
 * @date 2023-08-28 15:00:43
 */
@Repository
public class VideoRepository {
    private final VideoPostMapper videoPostMapper;
    private final VideoFileMapper videoFileMapper;
    private final VideoStatisticMapper videoStatisticMapper;

    public VideoRepository(VideoPostMapper videoPostMapper, VideoFileMapper videoFileMapper,
                           VideoStatisticMapper videoStatisticMapper) {
        this.videoPostMapper = videoPostMapper;
        this.videoStatisticMapper = videoStatisticMapper;
        this.videoFileMapper = videoFileMapper;
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveVideo(VideoFile videoFile, VideoPost videoPost) {
        String videoId = videoPost.getVideoId();
        VideoStatistic videoStatistic = new VideoStatistic(videoId);

        videoPostMapper.save(videoPost);
        videoStatisticMapper.save(videoStatistic);
        videoFileMapper.save(videoFile);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateVideoPublish(VideoPost videoPost) {
        videoPostMapper.updateVideoPublish(videoPost);
    }

    @Transactional(rollbackFor = Exception.class)
    @Deprecated
    public void saveVideoCrawled(VideoPost videoPost, VideoStatistic videoStatistic) {
        videoPostMapper.save(videoPost);
        videoStatisticMapper.save(videoStatistic);
    }

    @Transactional(rollbackFor = Exception.class)
    @Deprecated
    public void updateBiliVideoFile(String videoId, VideoMeta videoMeta, int channelCode) {
        VideoFile videoFile = new VideoFile(videoId, videoMeta, channelCode);
        videoFileMapper.save(videoFile);
        videoPostMapper.updateVideoCached(videoId, videoMeta, channelCode);
    }

    public void updateVideoStatistic(VideoStatistic videoStatistic) {
        videoStatisticMapper.update(videoStatistic);
    }

    public List<VideoUrlDto> getVideoUrls(String videoId) {
        return videoFileMapper.findVideoUrls(videoId);
    }

    public List<VideoFile> getVideoFiles(String videoId) {
        return videoFileMapper.findByVideoId(videoId);
    }

    //@Cacheable(cacheNames = "tnb:content:publishBy", key = "publishBy", unless = "#result == null")
    public List<GroupCount> getPublishUsers(int publishType) {
        List<GroupCount> list;
        if (publishType == 1) {
            list = videoPostMapper.findPublishByVideo();
        } else if (publishType == 2) {
            list = videoPostMapper.findPublishByImage();
        } else {
            list = videoPostMapper.findPublishByPost();
        }

        return list;
    }

    @Cacheable(cacheNames = "tnb:content:short_video", unless = "#result == null")
    public List<String> getShortVideoIds(List<Integer> userScopes) {
        List<String> videoIds = videoPostMapper.findShortVideo(userScopes);
        return videoIds;
    }

    public VideoPost getVideoPost(String videoId) {
        return videoPostMapper.findByVideoId(videoId);
    }

    public VideoStatistic getVideoStatistic(String videoId) {
        return videoStatisticMapper.findByVideoId(videoId);
    }

    public void updateIncrView(String videoId) {
        videoStatisticMapper.updateIncrView(videoId);
    }
}
