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

import cn.reghao.file.api.dto.VideoMeta;
import cn.reghao.file.api.iface.OssService;
import cn.reghao.tnb.auth.api.dto.AccountInfo;
import cn.reghao.tnb.auth.api.dto.CrawledUser;
import cn.reghao.tnb.auth.api.iface.AccountQuery;
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.repository.VideoRepository;
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;

/**
 * @author reghao
 * @date 2023-05-31 11:15:22
 */
@Deprecated
@Slf4j
@Service
public class CrawledDataService {
    @DubboReference(check = false, retries = 0, timeout = 5_000)
    private AccountQuery accountQuery;
    @DubboReference(check = false, retries = 0, timeout = 5_000)
    private OssService ossService;

    private final VideoPostService videoPostService;
    private final VideoRepository videoRepository;

    public CrawledDataService(VideoPostService videoPostService, VideoRepository videoRepository) {
        this.videoPostService = videoPostService;
        this.videoRepository = videoRepository;
    }

    public synchronized void publishVideo(CrawledVideo crawledVideo) {
        String videoId = crawledVideo.getVideoId();
        VideoPost videoPost1 = videoRepository.getVideoPost(videoId);
        if (videoPost1 != null ) {
            log.error("VideoPost {} exists", videoPost1.getVideoId());
            return;
        }

        CrawledUser crawledUser = crawledVideo.getCrawledUser();
        String username = crawledUser.getUsername();
        AccountInfo accountInfo = accountQuery.getByUsername(username);
        Long publishBy = accountInfo != null ? accountInfo.getUserId() : null;
        if (publishBy == null) {
            publishBy = accountQuery.createCrawledAccount(crawledUser);
        }

        VideoPost videoPost = new VideoPost(crawledVideo, publishBy);
        VideoStatistic videoStatistic = new VideoStatistic(crawledVideo);
        String tags = crawledVideo.getTags();
        videoPostService.saveCrawledVideo(videoPost, videoStatistic, tags);
    }

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

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

        int channelCode = 101;
        String videoFileId = videoFileUpdate.getVideoFileId();
        try {
            VideoMeta videoMeta = ossService.getVideoMeta(videoFileId);
            if (videoMeta == null) {
                log.error("{} not exist", videoFileId);
                return;
            }
            videoRepository.updateBiliVideoFile(videoId, videoMeta, channelCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
