package com.qmth.themis.exam.api;

import com.qmth.themis.business.bean.exam.QrMobileMonitorParamBean;
import com.qmth.themis.business.bean.exam.QrUploadParamBean;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.enums.MobileModeEnum;
import com.qmth.themis.business.enums.MonitorVideoSourceEnum;
import com.qmth.themis.business.service.TEQrcodeService;
import com.qmth.themis.common.exception.BusinessException;
import com.qmth.themis.common.util.Result;
import com.qmth.themis.common.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;

@Api(tags = "二维码接口")
@RestController
@RequestMapping(SystemConstant.PREFIX_URL_OE + "/qrcode")
public class TEQrcodeController {

    @Resource
    TEQrcodeService qrcodeService;

    @ApiOperation(value = "获取移动端监考二维码")
    @RequestMapping(value = "/mobile_monitor", method = RequestMethod.POST)
    public Result mobileMonitor(@RequestBody QrMobileMonitorParamBean param) throws NoSuchAlgorithmException {
        if (param.getRecordId() == null) {
            throw new BusinessException("recordId不能为空");
        }
        if (param.getSource() == null) {
            throw new BusinessException("source不能为空");
        }
        if (MonitorVideoSourceEnum.MOBILE_FIRST != param.getSource() && MonitorVideoSourceEnum.MOBILE_SECOND != param
                .getSource()) {
            throw new BusinessException("source 错误");
        }
        return ResultUtil.ok(qrcodeService.mobileMonitor(param));
    }

    @ApiOperation(value = "获取移动端拍照/录音上传二维码")
    @RequestMapping(value = "/{uploadType}", method = RequestMethod.POST)
    public Result upload(@PathVariable String uploadType, @RequestBody QrUploadParamBean param) {
        if (param.getRecordId() == null) {
            throw new BusinessException("recordId不能为空");
        }
        if (param.getMainNumber() == null) {
            throw new BusinessException("MainNumber不能为空");
        }
        if (param.getSubNumber() == null) {
            throw new BusinessException("SubNumber不能为空");
        }
        if (param.getQuestionNumber() == null) {
            throw new BusinessException("QuestionNumber不能为空");
        }
        MobileModeEnum mode = MobileModeEnum.valueOf(uploadType.toUpperCase());
        if (!MobileModeEnum.PHOTO_UPLOAD.equals(mode) && !MobileModeEnum.AUDIO_UPLOAD.equals(mode)) {
            throw new BusinessException("uploadType 错误");
        }
        return ResultUtil.ok(qrcodeService.upload(param, mode));
    }
}
