package com.platform.modules.live.controller;

import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.platform.common.annotation.SysLog;
import com.platform.common.utils.R;
import com.platform.modules.live.service.TranscodingTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 视频转码服务
 * @Author: yikaihong
 * @CreateDate: 2019/1/28 17:33
 * @Version: 1.0
 */
@RestController
@RequestMapping("/transcoding")
@Api(value = "视频转码任务管理", tags = "视频转码任务管理")
@AllArgsConstructor
public class TranscodingController {
    @Autowired
    private TranscodingTaskService transcodingTaskService;

//    @PostMapping("/submit")
//    @ApiOperation("提交任务")
//    public R submit( @RequestBody TranscodingTaskDto dto) {
//        return transcodingTaskService.submit(dto);
//    }

    @SysLog("提交视频转码任务")
    @PostMapping("/submit")
    @ApiOperation("提交任务")
    public R upload(@RequestParam("file") MultipartFile file) {
        return transcodingTaskService.upload(file);
    }

    @SysLog("取消视频转码任务")
    @PostMapping("/cancel")
    @ApiOperation("转码任务取消")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "taskId", name = "taskId", paramType = "query", dataType = "String")})
    public R cancel(String taskId) {
        return transcodingTaskService.cancel(taskId);
    }

    @GetMapping("/query")
    @ApiOperation("转码任务查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "taskId", name = "taskId", paramType = "query", dataType = "String")})
    public R query(String taskId) {
        return transcodingTaskService.queryTask(taskId);
    }

    @GetMapping("/token")
    @ApiOperation("获取token")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "appKey", name = "appKey", paramType = "query", dataType = "String"),
            @ApiImplicitParam(value = "appSecret", name = "appSecret", paramType = "query", dataType = "String")
    })
    public R token(@RequestParam("appKey") String appKey, @RequestParam("appSecret") String appSecret) {
        if (StringUtils.isEmpty(appKey) || StringUtils.isEmpty(appSecret)) {
            return R.error("请求参数为null");
        }
        return transcodingTaskService.token(appKey,appSecret);
    }

    @PostMapping("/notify")
    public R notification(HttpServletRequest request) {

        return transcodingTaskService.saveTranscodingMessage(request);
    }
}