package com.cool.store.controller.webb;

import com.alibaba.fastjson.JSONObject;
import com.cool.store.dto.trtc.callback.VideoCallBackDTO;
import com.cool.store.response.ResponseResult;
import com.cool.store.service.TRTCVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("pc/video")
@Api(tags = "腾讯音视频接口")
@Slf4j
public class VideoController {

    @Autowired
    private TRTCVideoService videoService;

    @PostMapping("/callback")
    @ApiOperation("音视频回调（腾讯云回调）")
    public ResponseResult videoCallback(@RequestBody VideoCallBackDTO videoCallBackDTO) {
        try {
            log.info("腾讯音视频上传回调开始，request{}", JSONObject.toJSONString(videoCallBackDTO));
            //不是音视频上传的回调
            if (311 != videoCallBackDTO.getEventType()) {
                return null;
            }
            if (0 != videoCallBackDTO.getEventInfo().getPayload().getStatus()) {
                log.error("腾讯音视频录制视频上传错误：request：\t{}", videoCallBackDTO);
                return null;
            }
            videoService.handleVideoCallBack(videoCallBackDTO);
            return new ResponseResult(0, "腾讯云回调音视频回调数据接收成功");
        } catch (Exception e) {
            log.error("腾讯音视频录制视频上传回调错误：e：\t{}", e.toString());
        }
        return null;
    }

}
