package cn.reghao.oss.web.app.controller;

import cn.reghao.jutil.jdk.result.WebResult;
import cn.reghao.oss.api.dto.media.VideoInfo;
import cn.reghao.oss.api.dto.media.VideoUrlDto;
import cn.reghao.oss.web.app.service.media.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author reghao
 * @date 2024-02-23 09:05:58
 */
@Api(tags = "视频文件接口")
@RestController
@RequestMapping("/api/oss/media/video")
public class VideoFileController {
    private final VideoService videoService;

    public VideoFileController(VideoService videoService) {
        this.videoService = videoService;
    }

    @ApiOperation(value = "删除视频文件")
    @DeleteMapping(value = "/delete/{videoFileId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteVideoFile(@PathVariable("videoFileId") String videoFileId) throws Exception {
        videoService.deleteVideoFile(videoFileId);
        return WebResult.success();
    }

    @ApiOperation(value = "获取视频文件信息")
    @GetMapping(value = "/info/{videoFileId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getVideoInfo(@PathVariable("videoFileId") String videoFileId) {
        VideoInfo videoInfo = videoService.getVideoInfo(videoFileId);
        return WebResult.success(videoInfo);
    }

    @ApiOperation(value = "获取视频文件URL")
    @GetMapping(value = "/url", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getVideoUrl(@RequestParam("videoFileId") String videoFileId,
                              @RequestParam("userId") Long userId) {
        List<VideoUrlDto> list = videoService.getVideoUrls(videoFileId, userId);
        return WebResult.success(list);
    }

    @ApiOperation(value = "获取视频文件创建时间")
    @GetMapping(value = "/time/{videoFileId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getVideoCreateTime(@PathVariable("videoFileId") String videoFileId) {
        LocalDateTime localDateTime = videoService.getCreateTime(videoFileId);
        return WebResult.success(localDateTime);
    }
}
