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

import cn.reghao.file.api.iface.OssService;
import cn.reghao.jutil.jdk.result.Result;
import cn.reghao.oss.sdk.model.dto.media.VideoInfo;
import cn.reghao.oss.sdk.model.dto.media.VideoUrlDto;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.content.api.constant.VideoErrorType;
import cn.reghao.tnb.content.api.constant.VideoStatus;
import cn.reghao.tnb.content.app.vod.db.mapper.*;
import cn.reghao.tnb.content.app.vod.model.dto.*;
import cn.reghao.tnb.content.app.vod.model.po.*;
import cn.reghao.tnb.content.app.vod.model.vo.DownloadUrl;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author reghao
 * @date 2022-08-05 13:49:28
 */
@Slf4j
@Service
public class VideoService {
    @DubboReference(check = false)
    private OssService ossService;

    private final VideoPostMapper videoPostMapper;
    private final VideoFileMapper videoFileMapper;
    private final VideoErrorMapper videoErrorMapper;
    private final Set<Long> adminUsers;
    private VideoStatisticMapper videoStatisticMapper;

    public VideoService(VideoPostMapper videoPostMapper, VideoFileMapper videoFileMapper,
                        VideoErrorMapper videoErrorMapper, VideoStatisticMapper videoStatisticMapper) {
        this.videoPostMapper = videoPostMapper;
        this.videoFileMapper = videoFileMapper;
        this.videoErrorMapper = videoErrorMapper;
        this.videoStatisticMapper = videoStatisticMapper;
        this.adminUsers = Set.of(10001L, 10002L);
    }

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

    public Result errorReport(VideoErrorReport videoErrorReport) {
        long loginUser = UserContext.getUser();
        if (!adminUsers.contains(loginUser)) {
            return Result.fail("no permission");
        }

        String videoId = videoErrorReport.getVideoId();
        int errCode = videoErrorReport.getErrorCode();
        VideoErrorType errorType = VideoErrorType.getDescByCode(errCode);
        int videoStatus;
        switch (errorType) {
            case noCover:
            case noAudio:
            case noVideo:
                videoStatus = VideoStatus.needRepair.getCode();
                break;
            case noResource:
                videoStatus = VideoStatus.revoke.getCode();
                break;
            case hasAd:
                videoStatus = VideoStatus.hasAd.getCode();
                break;
            case needRestore:
                videoStatus = VideoStatus.needRestore.getCode();
                break;
            default:
                return Result.fail("unknown errorType");
        }

        videoPostMapper.updateVideoStatus(videoId, videoStatus);
        if (videoStatus != VideoStatus.revoke.getCode() || videoStatus != VideoStatus.needRestore.getCode()) {
            VideoError videoError = new VideoError(videoErrorReport);
            videoErrorMapper.save(videoError);
        }

        return Result.success();
    }

    public String getShareLink(String videoId) {
        String shortUrl = String.format("/s/%s", videoId);
        return shortUrl;
    }

    public DownloadUrl downloadVideo(String videoId) {
        long userId = UserContext.getUser();
        VideoPost videoPost = videoPostMapper.findByVideoId(videoId);
        if (videoPost == null) {
            return null;
        }

        List<VideoUrlDto> list = videoFileMapper.findVideoUrls(videoId);
        for (VideoUrlDto videoUrlDto : list) {
            try {
                int channelId = videoUrlDto.getChannelCode();
                String videoFileId = videoUrlDto.getObjectId();
                String signedUrl = ossService.getSignedUrl(channelId, videoFileId);
                return new DownloadUrl(signedUrl, "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return null;
    }
}
