package com.hc.web.controller.browser.front;

import com.hc.business.model.ZccmUserVideoPosition;
import com.hc.business.service.ZccmCourseService;
import com.hc.business.service.ZccmUserVideoPositionService;
import com.hc.core.utils.result.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

/**
 * 课程表(ZccmCourse)表控制层
 *
 * @author java
 * @since 2020-11-09 10:04:46
 */

@RestController
@RequestMapping("/new/api/front/course")
public class FrontCourseController {

    /**
     * 服务对象
     */
    private final ZccmCourseService courseService;
    private final ZccmUserVideoPositionService userVideoPositionService;

    public FrontCourseController(ZccmCourseService courseServices,
                                 ZccmUserVideoPositionService userVideoPositionService) {
        this.courseService = courseServices;
        this.userVideoPositionService = userVideoPositionService;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "依据主键获取 ZccmCourse", response = Result.class)
    public Result selectFrontDetail(@PathVariable("id") Integer id) {
        return Result.success(this.courseService.queryById(id));
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取 ZccmCourse列表(分页)", response = Result.class)
    public Result listFrontCourse(@RequestParam(defaultValue = "0") Integer page,
                                  @RequestParam(defaultValue = "0") Integer size,
                                  @RequestParam(defaultValue = "1") Integer valid) {
        return Result.success(courseService.queryAllByLimit(page, size, valid));
    }

    @GetMapping("/list/teacher")
    @ApiOperation(value = "依据老师名称获取 ZccmCourse列表(分页)", response = Result.class)
    public Result queryTeacherName(@ApiParam(value = "老师名称") @RequestParam String name) {
        return Result.success(courseService.queryTeacherName(name));
    }

    @GetMapping("/list/name")
    @ApiOperation(value = "依据课程名称获取 ZccmCourse列表(分页)", response = Result.class)
    public Result queryZccmCourseName(@ApiParam(value = "课程名称") @RequestParam String name) {
        return Result.success(courseService.queryCourseName(name));
    }

    @GetMapping("/list/type")
    @ApiOperation(value = "获取 type分类下的 ZccmCourse列表(分页)", response = Result.class)
    public Result queryAllByLimitAndTypeId(@RequestParam(defaultValue = "0") Integer page,
                                           @RequestParam(defaultValue = "0") Integer size,
                                           @ApiParam(value = "类别id") @RequestParam Integer typeId) {
        return Result.success(courseService.queryAllByLimitAndTypeId(page, size, typeId));
    }

    @GetMapping("/list/video")
    @ApiOperation(value = "获取 课程下得video list", response = Result.class)
    public Result queryAllVideos(@ApiParam(value = "课程id") @RequestParam Integer courseId) {
        return Result.success(courseService.getAllVideos(courseId));
    }

    @PostMapping("/position")
    @ApiOperation(value = "添加一个视频播放记录redis 视频播放时一直调用", response = Result.class)
    public Result insertFrontPosition(@RequestBody ZccmUserVideoPosition userVideoPosition,
                                      @RequestParam Integer courseId) {
        return Result.success(userVideoPositionService.insertPositionToRedis(userVideoPosition, courseId));
    }


    @PostMapping("/flush")
    @ApiOperation(value = "添加一个视频播放记录dataBase 视频停止或者返回时调用 ", response = Result.class)
    public Result insertFrontPositionData(@RequestParam Integer courseId,
                                          @RequestParam Integer videoId,
                                          @RequestParam Integer userId,
                                          @RequestParam String years,
                                          @RequestParam Integer classId) {
        return Result.success(userVideoPositionService.flushToDb(userId, videoId, courseId, years, classId));
    }

    @GetMapping("/getZccmCourseVideoPosition")
    @ApiOperation(value = "获取一个课程下所有的视频以及某个用户的播放进度", response = Result.class)
    public Result getZccmCourseChapterAndVideoPosition(@RequestParam Integer courseId,
                                                   @RequestParam Integer userId,
                                                   @RequestParam Integer classId) {
        return Result.success(courseService.getCourseChapterAndVideoPosition(courseId, userId, classId));
    }


}