package jnpf.cultivate.v2.controller.app.exam;

import com.github.pagehelper.PageInfo;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.constant.MsgCode;
import jnpf.cultivate.v2.service.V2CultivateExamService;
import jnpf.model.cultivate.v2.exam.req.*;
import jnpf.model.cultivate.v2.exam.vo.*;
import jnpf.util.FtbUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 我的考试[app]
 *
 * @author yanwenfu
 */
@RestController
@RequestMapping("/v2/app/exam")
public class V2CultivateUserExamAppController {

    @Resource
    private V2CultivateExamService v2CultivateExamService;

    /**
     * 我的考试[tab count]
     * @return jnpf.base.ActionResult<jnpf.model.cultivate.v2.exam.vo.MyExamTabCountVo>
     */
    @GetMapping(value = "/tab-count")
    public ActionResult<MyExamTabCountVo> getMyExamTabCount() {

        MyExamTabCountVo vo = v2CultivateExamService.getMyExamTabCount();
        return ActionResult.success(vo);
    }

    /**
     * 我的考试[分页]
     * @param req 请求参数
     * @return jnpf.base.ActionResult<jnpf.base.vo.PageListVO<jnpf.model.cultivate.v2.exam.vo.MyExamVo>>
     */
    @GetMapping("/myExamList")
    public ActionResult<PageListVO<MyExamVo>> myExamList(V2AppQueryExamListReq req) {

        PageInfo<MyExamVo> pageVo = v2CultivateExamService.myExamList(req);
        return ActionResult.page(pageVo.getList(), FtbUtil.getPagination(pageVo));
    }

    /**
     * 重新考试[web]
     * @param userExamId 用户考试id
     * @return jnpf.base.ActionResult<java.lang.String>
     */
    @PutMapping(value = "/restart/{userExamId}")
    public ActionResult<String> restartExam(@PathVariable("userExamId") String userExamId) throws Exception {

        String newUserExamId = v2CultivateExamService.restartExam(userExamId);
        return ActionResult.success(MsgCode.SU000.get(), newUserExamId);
    }

    /**
     * 重新考试 - 带次数验证[app]
     * @param userExamId 用户考试id
     * @return jnpf.base.ActionResult<java.lang.String>
     */
    @PutMapping(value = "/restartAndCheckNum/{userExamId}")
    public ActionResult<String> restartExamWithValid(@PathVariable("userExamId") String userExamId) throws Exception {

        String newUserExamId = v2CultivateExamService.restartExamWithValid(userExamId);
        return ActionResult.success(MsgCode.SU000.get(), newUserExamId);
    }


    /**
     * 验证是否可以重新考试[app&web]
     * @param userExamId 用户考试id
     * @return jnpf.base.ActionResult<java.lang.Boolean>
     */
    @GetMapping(value = "/restart/check/{userExamId}")
    public ActionResult<Boolean> checkRestartExam(@PathVariable("userExamId") String userExamId) {

        try {
            Boolean b = v2CultivateExamService.checkRestartExam(userExamId);
            return ActionResult.success(b);
        } catch (Exception e) {
            return ActionResult.success(Boolean.FALSE);
        }
    }

    /**
     * 我的考试 - 详情
     * @param userExamId 用户考试id
     * @return jnpf.base.ActionResult<jnpf.model.cultivate.v2.exam.vo.MyExamDetailVo>
     */
    @GetMapping("/my-exam/detail/{userExamId}")
    public ActionResult<MyExamDetailVo> myExamDetail(@PathVariable("userExamId") String userExamId) {

        MyExamDetailVo vo = v2CultivateExamService.myExamDetail(userExamId);
        return ActionResult.success(vo);
    }

    /**
     * 开始考试 - 查询试题
     * @param userExamId 用户考试id
     * @return jnpf.base.ActionResult<java.util.List<jnpf.model.cultivate.v2.exam.vo.V2AppQuestionVo>>
     */
    @GetMapping("/exam-question/list/{userExamId}")
    public ActionResult<List<V2AppQuestionVo>> queryQuestionListForExam(@PathVariable("userExamId") String userExamId) throws Exception {

        List<V2AppQuestionVo> questionList = v2CultivateExamService.getExamQuestionList(userExamId);
        return ActionResult.success(questionList);
    }

    /**
     * 批阅考试[tab count]
     * @return jnpf.base.ActionResult<jnpf.model.cultivate.v2.exam.vo.MyExamTabCountVo>
     */
    @PostMapping(value = "/read-over/tab-count")
    public ActionResult<ReadOverTabCountVo> getReadOverTabCount(@RequestBody QueryReadOverExamListReq req) {

        ReadOverTabCountVo vo = v2CultivateExamService.getReadOverTabCount(req.getOrganizeList());
        return ActionResult.success(vo);
    }

    /**
     * 批阅考试 - 考试列表[分页]
     * @param req 请求参数
     * @return jnpf.base.ActionResult<jnpf.base.vo.PageListVO<jnpf.model.cultivate.v2.exam.vo.V2AppExamListVo>>
     */
    @PostMapping("/read-over/exam/list")
    public ActionResult<PageListVO<ReadOverExamVo>> readOverExamList(@RequestBody QueryReadOverExamListReq req) {

        PageInfo<ReadOverExamVo> pageVo = v2CultivateExamService.readOverExamList(req);
        return ActionResult.page(pageVo.getList(), FtbUtil.getPagination(pageVo));
    }

    /**
     * 批阅考试 - 排行榜[分页]
     * @param req 请求参数
     * @return jnpf.base.ActionResult<java.util.List<jnpf.model.cultivate.v2.exam.vo.V2AppExamRankingVo>>
     */
    @PostMapping("/result-rank")
    public ActionResult<UserExamRankVo> getExamRankList(@RequestBody ExamRankReq req) {

        UserExamRankVo rankVo = v2CultivateExamService.getExamRankList(req);
        return ActionResult.success(rankVo);
    }
}
