package cn.reghao.oss.store.rpc.media;

import cn.reghao.jutil.media.FFmpegWrapper;
import cn.reghao.jutil.media.model.MediaProps;
import cn.reghao.oss.api.constant.UploadChannel;
import cn.reghao.oss.api.dto.ObjectMeta;
import cn.reghao.oss.store.db.repository.ObjectRepository;
import cn.reghao.oss.store.db.repository.VideoRepository;
import cn.reghao.oss.store.model.po.VideoFile;
import cn.reghao.oss.store.rpc.SignService;
import cn.reghao.oss.api.dto.media.VideoInfo;
import cn.reghao.oss.api.dto.media.VideoUrlDto;
import cn.reghao.oss.api.iface.media.VideoFileService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author reghao
 * @date 2023-08-28 15:00:43
 */
@DubboService
@Service
public class VideoFileServiceImpl implements VideoFileService {
    private final LocalDateTime epoch = LocalDateTime.parse("1970-01-01T08:00");
    private final VideoRepository videoRepository;
    private final ObjectRepository objectRepository;
    private final SignService signService;

    public VideoFileServiceImpl(VideoRepository videoRepository, ObjectRepository objectRepository, SignService signService) {
        this.videoRepository = videoRepository;
        this.objectRepository = objectRepository;
        this.signService = signService;
    }

    @Override
    public void deleteVideoFile(String videoFileId) {
        List<String> objectIds = videoRepository.deleteVideoFile(videoFileId);
        objectRepository.deleteByObjectIds(objectIds);
    }

    @Override
    public VideoInfo getVideoInfo(String videoFileId) {
        List<VideoInfo> list = videoRepository.getVideoInfos(videoFileId);
        return list.isEmpty() ? null : list.get(0);
    }

    public List<VideoFile> getVideoFiles(String videoFileId) {
        List<VideoFile> list = videoRepository.getVideoFiles(videoFileId);
        return list;
    }

    @Override
    public List<VideoUrlDto> getVideoUrls(String videoFileId, long loginUser) {
        int channelId = UploadChannel.video.getCode();
        List<VideoUrlDto> list = videoRepository.findVideoUrls(videoFileId);
        list.forEach(videoUrlDto -> {
            String url = videoUrlDto.getUrl();
            String signedUrl = signService.getSignedUrl(loginUser, url, channelId, 3600);
            videoUrlDto.setUrl(signedUrl);
        });
        return list;
    }

    @Override
    public LocalDateTime getCreateTime(String videoFileId) {
        ObjectMeta objectMeta = objectRepository.getObjectMetaById(videoFileId);
        if (objectMeta == null) {
            return null;
        }

        String absolutePath = objectMeta.getAbsolutePath();
        MediaProps mediaProps = FFmpegWrapper.getMediaProps(absolutePath);
        if (mediaProps == null) {
            return null;
        }

        LocalDateTime localDateTime = mediaProps.getCreateTime();
        if (localDateTime != null && !localDateTime.equals(epoch)) {
            return localDateTime;
        }

        return null;
    }
}
