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

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.*;

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

@RestController
@RequestMapping("/new/api/app/course")
public class AppCourseController {

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

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

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

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

    @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));
    }

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

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

    @GetMapping("/list/type")
    @ApiOperation(value = "获取 type分类下的 Course列表(分页)", 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));
    }


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


    @PostMapping("/flush")
    @ApiOperation(value = "添加一个视频播放记录dataBase", response = Result.class)
    public Result insertAppPositionData(@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("/getCourseVideoPosition")
    @ApiOperation(value = "获取一个课程下所有的视频以及某个用户的播放进度", response = Result.class)
    public Result getAppCourseChapterAndVideoPosition(@RequestParam Integer courseId,
                                                      @RequestParam Integer userId,
                                                      @RequestParam Integer classId) {
        return Result.success(courseService.getCourseChapterAndVideoPosition(courseId, userId, classId));
    }
}