package cn.exploring.engine.server.wx.web;

import cn.exploring.engine.server.core.util.ResponseUtil;
import cn.exploring.engine.server.db.service.LessonInfoService;
import cn.exploring.engine.server.wx.annotation.LoginUser;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/wx/lessonInfo")
@Validated
public class WxLessonInfoController {
    @Resource
    private LessonInfoService lessonInfoService;

    /**
     * 提交用户的学习进度
     * @param userId
     * @param chaptersId
     * @param selectId
     * @return
     */
    @GetMapping("commitUserLessonRecord")
    public Object commitUserLessonRecord(@LoginUser Integer userId,
                                         @RequestParam Integer chaptersId,
                                         @RequestParam Integer sessionId,
                                         @RequestParam(required = false) Integer selectId) {
        if (userId == null || userId < 0) {
            return ResponseUtil.unlogin();
        }
        return ResponseUtil.ok(lessonInfoService.commitUserLessonRecord(userId, chaptersId, sessionId, selectId));
    }

    /**
     * 获取用户课程  未登陆时 默认拿到第一节课  登陆则拿到学完的课程的下一节  为空时则学完了
     * @param userId
     * @return
     */
    @GetMapping("getLesson")
    public Object getLesson(@LoginUser Integer userId) {
        return ResponseUtil.ok(lessonInfoService.getUserLessonInfo(userId));
    }

    /**
     * 获取所有课程的目录
     * @return
     */
    @GetMapping("getIndex")
    public Object getAllLessonIndex() {
        return ResponseUtil.ok(lessonInfoService.getAllLessonIndex());
    }

    /**
     * 用户历史上学完的课程名称及课程积分
     * @param userId
     * @return
     */
    @GetMapping("historyPoint")
    public Object getHistoryPoint(@LoginUser Integer userId) {
        return ResponseUtil.ok(lessonInfoService.getUserHistoryLessonPoint(userId));
    }

    @GetMapping("pointRankInfo")
    public Object getPointRankInfo(@LoginUser Integer userId) {
        return ResponseUtil.ok(lessonInfoService.getUserLessonPointRank(userId));
    }

    @GetMapping("getLessonById")
    public Object getLessonInfo(@LoginUser Integer userId, @RequestParam Integer lessonId) {

        if (userId == null || userId < 0) {
            return ResponseUtil.unlogin();
        }
        return ResponseUtil.ok(lessonInfoService.getUserLessonInfoByLessonId(userId, lessonId));
    }




}
