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

import ink.wgink.exceptions.ParamsException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.file.media.service.video.IVideoService;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.properties.media.video.VideoProperties;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: VideoRouteController
 * @Description: 视频路由
 * @Author: wanggeng
 * @Date: 2021/6/12 11:16 下午
 * @Version: 1.0
 */
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "视频管理接口")
@Controller
@RequestMapping(ISystemConstant.ROUTE_PREFIX + "/file/media/video")
public class VideoRouteController {

    @Autowired
    private VideoProperties videoProperties;
    @Autowired
    private IVideoService videoService;

    @GetMapping("list")
    public ModelAndView list() {
        return new ModelAndView("video/list");
    }

    @GetMapping("upload")
    public ModelAndView upload() {
        ModelAndView modelAndView = new ModelAndView("video/upload");
        modelAndView.addObject("types", videoProperties.getTypes());
        modelAndView.addObject("maxUploadCount", videoProperties.getMaxUploadCount());
        return modelAndView;
    }

    @GetMapping("preview")
    public ModelAndView preview() {
        return new ModelAndView("video/preview");
    }

    @ApiOperation(value = "下载视频", notes = "下载视频接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isOpen", value = "是否打开,true和false", paramType = "path"),
            @ApiImplicitParam(name = "fileId", value = "文件ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping(value = "download/{isOpen}/{fileId}", produces = "video/mp4")
    public void downLoad(HttpServletRequest request,
                         HttpServletResponse response,
                         @PathVariable("isOpen") Boolean isOpen,
                         @PathVariable("fileId") String fileId) {
        if (isOpen == null) {
            throw new ParamsException("参数错误");
        }
        if (fileId.indexOf(".") > 0) {
            fileId = fileId.split("\\.")[0];
        }
        videoService.downLoad(request, response, isOpen, fileId);
    }

}
