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

import cn.reghao.file.api.iface.OssService;
import cn.reghao.oss.sdk.model.dto.media.VideoInfo;
import cn.reghao.tnb.content.api.constant.PostScope;
import cn.reghao.tnb.content.api.dto.spider.BiliRegion;
import cn.reghao.tnb.content.api.dto.VideoFileUpdate;
import cn.reghao.tnb.content.api.dto.spider.CrawledVideo;
import cn.reghao.tnb.content.app.vod.db.mapper.VideoCategoryMapper;
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.db.repository.VideoRepository;
import cn.reghao.tnb.content.app.vod.model.po.VideoCategory;
import cn.reghao.tnb.content.app.vod.model.po.VideoPost;
import cn.reghao.tnb.content.app.vod.model.po.VideoStatistic;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author reghao
 * @date 2023-05-31 11:15:22
 */
@Deprecated
@Slf4j
@Service
public class CrawledDataService {
    @DubboReference(check = false)
    private OssService ossService;

    private final VideoPostMapper videoPostMapper;
    private final VideoPostService videoPostService;
    private final VideoCategoryMapper videoCategoryMapper;
    private final VideoStatisticMapper videoStatisticMapper;
    private final VideoRepository videoRepository;

    public CrawledDataService(VideoPostMapper videoPostMapper, VideoPostService videoPostService,
                                  VideoCategoryMapper videoCategoryMapper, VideoStatisticMapper videoStatisticMapper,
                                  VideoRepository videoRepository) {
        this.videoPostMapper = videoPostMapper;
        this.videoPostService = videoPostService;
        this.videoCategoryMapper = videoCategoryMapper;
        this.videoStatisticMapper = videoStatisticMapper;
        this.videoRepository = videoRepository;
    }

    public List<BiliRegion> getCategories() {
        List<BiliRegion> biliRegions = videoCategoryMapper.findAllCategoryIds();
        return biliRegions;
    }

    public VideoPost videoExist(String videoId) {
        VideoPost videoPost = videoPostMapper.findByVideoId(videoId);
        return videoPost;
    }

    public synchronized void publishVideo(CrawledVideo crawledVideo) {
        int categoryPid = 3;
        int categoryId = 4;
        Integer rid = crawledVideo.getRid();
        if (rid != 0) {
            VideoCategory videoCategory = videoCategoryMapper.findByRid(rid);
            if (videoCategory != null) {
                categoryPid = videoCategory.getPid();
                categoryId = videoCategory.getId();
            }
        }

        VideoPost videoPost1 = videoPostMapper.findByVideoId(crawledVideo.getVideoId());
        if (videoPost1 != null ) {
            return;
        }

        VideoPost videoPost = new VideoPost(crawledVideo, categoryPid, categoryId);
        VideoStatistic videoStatistic = new VideoStatistic(crawledVideo);
        String tags = crawledVideo.getTags();
        videoPostService.saveVideo(videoPost, videoStatistic, tags);
    }

    public void updateVideoStatistic(CrawledVideo crawledVideo) {
        VideoStatistic videoStatistic = new VideoStatistic(crawledVideo);
        videoStatisticMapper.update(videoStatistic);
    }

    public void updateVideoFile(VideoFileUpdate videoFileUpdate) {
        String videoId = videoFileUpdate.getVideoId();
        VideoPost videoPost = videoPostMapper.findByVideoId(videoId);
        if (videoPost == null) {
            return;
        }

        int channelId = 101;
        String videoFileId = videoFileUpdate.getVideoFileId();
        try {
            VideoInfo videoInfo = ossService.getVideoInfo(channelId, videoFileId);
            if (videoInfo == null) {
                log.error("{} not exist", videoFileId);
                return;
            }

            videoRepository.updateBiliVideoFile(videoId, videoInfo, channelId);
            int scope = videoPost.getScope();
            if (scope != PostScope.PRIVATE.getCode()) {
                ossService.setObjectScope(channelId, videoFileId, scope);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
