package com.ruoyi.sim.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.sim.domain.vo.RealExamVo;
import com.ruoyi.sim.service.impl.RealExamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sim.domain.RealExam;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考试Controller
 *
 * @author tom
 * @date 2024-12-15
 */
@RestController
@RequestMapping("/sim/real-exam")
@Api("考试Controller")
public class RealExamController extends BaseController {
    @Autowired
    private RealExamService realExamService;

    /**
     * 查询考试列表
     */
    // @GetMapping("/teacher/list")
    // @ApiOperation("[老师]查询学生考试列表")
    public TableDataInfo list(RealExam realExam) {
        startPage();
        List<RealExam> list = realExamService.selectRealExamList(realExam);
        return getDataTable(list);
    }

    @GetMapping("/student/exam/listByUserId/")
    @ApiOperation("[学生]查询userId学生考试列表")
    public TableDataInfo listByUserId() {
        // todo:
        RealExam q = new RealExam();
        q.setUserId(SecurityUtils.getUserId());
        startPage();
        List<RealExamVo> list = realExamService.list(q);
        // todo:
        return getDataTable(list);
    }

    @GetMapping("/student/exam/enter/{examId}")
    @ApiOperation("[学生]进入考试")
    public AjaxResult studentEnterRealExam(@PathVariable("examId") Long examId) {
        return realExamService.studentEnterRealExam(examId);
    }

    @GetMapping("/student/exam/prepare/{examId}")
    @ApiOperation("[轮询][学生]准备考试界面")
    public AjaxResult studentLoopPrepareRealExam(@PathVariable("examId") Long examId) {
        return realExamService.studentLoopPrepareRealExam(examId);
    }

    @GetMapping("/student/exam/start/{examId}")
    @ApiOperation("[学生]开始考试")
    public AjaxResult studentStartRealExam(@PathVariable("examId") Long examId) {
        return realExamService.studentStartRealExam(examId);
    }

    @GetMapping("/student/exam/answering/{examId}")
    @ApiOperation("[轮询][学生]正在考试界面")
    public AjaxResult studentLoopAnsweringRealExam(@PathVariable("examId") Long examId) {
        return realExamService.studentLoopAnsweringRealExam(examId);
    }

    @GetMapping("/student/exam/submit/{examId}")
    @ApiOperation("[学生]交卷")
    public AjaxResult studentSubmitRealExam(@PathVariable("examId") Long examId) {
        return realExamService.studentSubmitRealExam(examId);
    }

    @GetMapping("/student/exam/report/{examId}")
    @ApiOperation("[轮询][学生]结束考试界面")
    public AjaxResult studentLoopPostRealExam(@PathVariable("examId") Long examId) {
        return realExamService.studentLoopPostRealExam(examId);
    }

    //    @GetMapping(value = "/student/{examId}")
//    @ApiOperation("[学生][轮询]获取考试详细信息")
    public AjaxResult getInfoStudent(@PathVariable("examId") Long examId) {
        return success(realExamService.selectRealExamByExamId(examId));
    }

    // @PreAuthorize("@ss.hasPermi('sim:real-exam:query')")
    // @GetMapping(value = "/teacher/{examId}")
    // @ApiOperation("[老师]获取考试详细信息")
    public AjaxResult getInfoTeacher(@PathVariable("examId") Long examId) {
        return success(realExamService.selectRealExamByExamId(examId));
    }

    /**
     * 新增考试
     */
    // @PreAuthorize("@ss.hasPermi('sim:real-exam:add')")
    @Log(title = "考试", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RealExam realExam) {
        return toAjax(realExamService.insertRealExam(realExam));
    }

    /**
     * 修改考试
     */
    // @PreAuthorize("@ss.hasPermi('sim:real-exam:edit')")
    @Log(title = "考试", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RealExam realExam) {
        return toAjax(realExamService.updateRealExam(realExam));
    }

    /**
     * 删除考试
     */
    // @PreAuthorize("@ss.hasPermi('sim:real-exam:remove')")
    @Log(title = "考试", businessType = BusinessType.DELETE)
    // @DeleteMapping("/{examIds}")
    public AjaxResult remove(@PathVariable Long[] examIds) {
        return toAjax(realExamService.deleteRealExamByExamIds(examIds));
    }
}
