package com.qmth.distributed.print.api.mark;

import java.util.List;

import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import com.qmth.teachcloud.common.util.ServletUtil;
import com.qmth.teachcloud.mark.dto.ScanOmrTaskResultDto;
import com.qmth.teachcloud.mark.enums.OmrTaskStatus;
import com.qmth.teachcloud.mark.service.ScanOmrTaskService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 识别对照 前端控制器
 * </p>
 *
 * @author xf
 * @since 2023-09-22
 */
@Api(tags = "扫描-识别对照")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_SCAN + "/task/omr")
public class ScanOmrTaskController extends BaseController {
    protected static final Logger log = LoggerFactory.getLogger(ScanOmrTaskController.class);

    @Autowired
    private ScanOmrTaskService omrTaskService;

    @ApiOperation(value = "识别对照任务获取")
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    public Result get(@RequestParam Long studentId) {
        return ResultUtil.ok(omrTaskService.getTask(studentId));
    }

    @ApiOperation(value = "识别对照任务提交")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@Validated @RequestBody List<ScanOmrTaskResultDto> result) {
        SysUser user = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(omrTaskService.submitTask(result, user.getId()));
    }

    @ApiOperation(value = "识别对照任务状态")
    @RequestMapping(value = "/status", method = RequestMethod.POST)
    public Result status(@ApiParam(value = "考试ID", required = true) @RequestParam Long examId,
                         @ApiParam(value = "课程代码") @RequestParam(required = false) String courseCode,
                         @ApiParam(value = "课程试卷编号") @RequestParam(required = false) String coursePaperId) {
        return ResultUtil.ok(omrTaskService.getStatus(examId, courseCode, coursePaperId));
    }

//    @ApiOperation(value = "识别对照任务状态")
//    @RequestMapping(value = "/release", method = RequestMethod.POST)
//    public Result release(@RequestParam Long examId) {
//        SysUser user = (SysUser) ServletUtil.getRequestUser();
//        omrTaskService.releaseByUser(examId, user.getId());
//        return ResultUtil.ok(true);
//    }

    @ApiOperation(value = "识别对照任务列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result list(@ApiParam(value = "考试ID", required = true) @RequestParam Long examId,
                       @ApiParam(value = "课程代码") @RequestParam(required = false) String courseCode,
                       @ApiParam(value = "课程试卷编号") @RequestParam(required = false) String coursePaperId,
                       @ApiParam(value = "状态") @RequestParam OmrTaskStatus status) {
        return ResultUtil.ok(omrTaskService.list(examId, courseCode, coursePaperId, status));
    }
}
