package ink.wgink.module.file.media.controller.api.video;

import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.exceptions.PropertiesException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.file.media.pojo.dtos.video.VideoDTO;
import ink.wgink.module.file.media.pojo.dtos.video.VideoUploadSuccessDTO;
import ink.wgink.module.file.media.service.video.IVideoService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultData;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.properties.media.MediaProperties;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: VideoController
 * @Description: 视频
 * @Author: WangGeng
 * @Date: 2021/6/8 22:05
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "视频接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/file/media/video")
public class VideoController extends DefaultBaseController {

    @Autowired
    private MediaProperties mediaProperties;
    @Autowired
    private IVideoService videoService;

    @ApiOperation(value = "上传视频", notes = "上传视频接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("upload")
    public SuccessResultData<VideoUploadSuccessDTO> upload(@RequestParam("video") MultipartFile video) {
        if (StringUtils.isBlank(mediaProperties.getUploadPath())) {
            throw new PropertiesException("未设置文件上传路径");
        }
        if (StringUtils.isBlank(mediaProperties.getFfmpegPath())) {
            throw new PropertiesException("未设置解码器路径");
        }
        return new SuccessResultData<>(videoService.upload(video));
    }

    @ApiOperation(value = "删除文件类别(id列表)", notes = "删除文件类别(id列表)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("delete/{ids}")
    public synchronized SuccessResult delete(@PathVariable("ids") String ids) {
        videoService.delete(Arrays.asList(ids.split("\\_")));
        return new SuccessResult();
    }

    @ApiOperation(value = "视频转码", notes = "视频转码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("convert/{fileId}")
    public synchronized SuccessResult updateConvert(@PathVariable("fileId") String fileId) {
        videoService.updateConvert(fileId);
        return new SuccessResult();
    }

    @ApiOperation(value = "视频转换进度", notes = "视频转换进度接口")
    @GetMapping(value = "get-convert-progress", produces = "text/event-stream")
    public SseEmitter getConvertProgress() {
        return videoService.getConvertProgress();
    }

    @ApiOperation(value = "视频分页列表", notes = "视频分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage")
    public SuccessResultList<List<VideoDTO>> listPageInfo(ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return videoService.listPage(page);
    }

}
