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

import cn.reghao.file.api.iface.OssService;
import cn.reghao.jutil.jdk.web.db.Page;
import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.jutil.jdk.serializer.JsonConverter;
import cn.reghao.tnb.common.db.GroupCount;
import cn.reghao.tnb.content.api.dto.*;
import cn.reghao.tnb.content.api.iface.AdminVideoService;
import cn.reghao.tnb.content.app.vod.db.mapper.VideoPostMapper;
import cn.reghao.tnb.content.app.vod.db.repository.VideoRepository;
import cn.reghao.tnb.content.app.vod.model.po.BannerVideo;
import cn.reghao.tnb.content.app.vod.model.query.VideoQuery;
import cn.reghao.tnb.content.app.vod.service.CategoryService;
import cn.reghao.tnb.content.app.vod.service.VideoPostQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

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

/**
 * @author reghao
 * @date 2024-02-15 09:39:08
 */
@Slf4j
@DubboService
@Service
public class AdminVideoServiceImpl implements AdminVideoService {
    @DubboReference(check = false)
    private OssService ossService;

    private final RabbitTemplate rabbitTemplate;
    private final VideoPostMapper videoPostMapper;
    private final VideoPostQuery videoPostQuery;
    private final VideoRepository videoRepository;
    private final CategoryService categoryService;
    private int pageSize = 100;

    public AdminVideoServiceImpl(RabbitTemplate rabbitTemplate, VideoPostMapper videoPostMapper,
                                 VideoPostQuery videoPostQuery, VideoRepository videoRepository,
                                 CategoryService categoryService) {
        this.rabbitTemplate = rabbitTemplate;
        this.videoPostMapper = videoPostMapper;
        this.videoPostQuery = videoPostQuery;
        this.videoRepository = videoRepository;
        this.categoryService = categoryService;
    }

    public PageList<UserVideoPost> getVideoPosts(VideoSearch videoSearch) {
        int pageNumber = videoSearch.getPageNumber();
        int pageSize = videoSearch.getPageSize();
        Page page = new Page(pageNumber, pageSize);
        String content = videoSearch.getContent();

        VideoQuery videoQuery = new VideoQuery.Builder()
                .status(null)
                .scope(null)
                .title(content)
                .orderBy(videoSearch.getOrderBy())
                .orderDirection(videoSearch.getOrderDirection())
                .build();
        int total = videoPostMapper.countAdminVideo(videoQuery);
        List<UserVideoPost>list = videoPostMapper.findAdminVideoByPage(page, videoQuery).stream()
                .map(videoPostQuery::getUserVideoPost)
                .collect(Collectors.toList());
        return PageList.pageList(pageNumber, 20, total, list);
    }

    @Deprecated
    public void setHotVideo() {
        String routeKey = "tnb.data";
        List<BannerVideo> list = new ArrayList<>();
        rabbitTemplate.convertAndSend(routeKey, JsonConverter.objectToJson(list));
    }

    @Override
    public void addConvertedVideo(VideoConvertedDto videoConvertedDto) {
        /*String uploadId = videoConvertedDto.getUploadId();
        int channelCode = videoConvertedDto.getChannelCode();
        try {
            VideoInfo videoInfo = ossService.getVideoInfo(uploadId);
            if (videoInfo == null) {
                String errMsg = String.format("视频文件 %s 在 oss 中不存在", uploadId);
                log.info(errMsg);
                return;
            }

            String videoFileId = videoConvertedDto.getVideoFileId();
            VideoPost videoPost = videoRepository.getVideoPostByFileId(videoFileId);
            int scope = videoPost.getScope();
            if (scope != ObjectScope.PRIVATE.getCode()) {
                ossService.setObjectScope(channelCode, uploadId, scope);
            }

            String videoId = videoPost.getVideoId();
            VideoFile videoFile = new VideoFile(videoId, videoFileId, channelCode, videoInfo);
            videoRepository.updateVideoPublish(videoId, videoFile);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
    }

    @Override
    public List<Long> getContentUser(int publishType) {
        List<GroupCount> list = videoRepository.getPublishUsers(publishType);
        return list.stream()
                .map(groupCount -> Long.valueOf(groupCount.getId()))
                .collect(Collectors.toList());
    }

    @Override
    public Result setVideoPostPublish(String videoId) {
        return Result.fail("not implement");
    }
}
