package com.fs.app.controller;

import com.fs.app.annotation.Login;
import com.fs.common.core.domain.R;
import com.fs.common.core.domain.ResponseResult;
import com.fs.course.param.newfs.FsUserCourseListParam;
import com.fs.course.param.newfs.UserCourseVideoPageParam;
import com.fs.course.service.IFsUserCourseService;
import com.fs.course.service.IFsUserCourseVideoService;
import com.fs.course.vo.FsUserCourseParticipationRecordVO;
import com.fs.course.vo.newfs.FsUserCourseListVO;
import com.fs.course.vo.newfs.FsUserCourseVideoDetailsVO;
import com.fs.course.vo.newfs.FsUserCourseVideoPageListVO;
import com.fs.course.vo.newfs.FsUserVideoListVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api("课程库相关接口")
@RestController
@RequestMapping("/app/fs/course")
@Slf4j
public class FsUserCourseVideoController extends AppBaseController {

    @Autowired
    private IFsUserCourseVideoService fsUserCourseVideoService;

    @Autowired
    private IFsUserCourseService fsUserCourseService;

    @GetMapping("/pageList")
    @ApiOperation("课程分页列表")
    public ResponseResult<PageInfo<FsUserCourseVideoPageListVO>> list(UserCourseVideoPageParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        param.setCompanyId(getCompanyId());
        List<FsUserCourseVideoPageListVO> list = fsUserCourseVideoService.pageListCourseVideo(param);
        PageInfo<FsUserCourseVideoPageListVO> pageInfo = new PageInfo<>(list);
        return ResponseResult.ok(pageInfo);
    }

    @ApiOperation("课程视频详情")
    @GetMapping(value = "/videoDetails")
    public ResponseResult<FsUserCourseVideoDetailsVO> getVideoDetails(Long videoId) {
        return fsUserCourseVideoService.getVideoDetails(videoId);
    }

    @GetMapping("/courseList")
    @ApiOperation("获取课程下拉列表")
    public ResponseResult<PageInfo<FsUserCourseListVO>> getAllCourseList(FsUserCourseListParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        param.setCompanyId(getCompanyId());
        List<FsUserCourseListVO> fsUserCourseList = fsUserCourseService.getFsUserCourseList(param);
        PageInfo<FsUserCourseListVO> pageInfo = new PageInfo<>(fsUserCourseList);
        return ResponseResult.ok(pageInfo);
    }

    @GetMapping("/videoList")
    @ApiOperation("获取视频下拉列表")
    public ResponseResult<PageInfo<FsUserVideoListVO>> getAllVideoList(UserCourseVideoPageParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        param.setCompanyId(getCompanyId());
        List<FsUserVideoListVO> listCourseVideo = fsUserCourseVideoService.getListCourseVideo(param);
        PageInfo<FsUserVideoListVO> result = new PageInfo<>(listCourseVideo);
        return ResponseResult.ok(result);
    }

    @Login
    @ApiOperation("参与记录")
    @GetMapping("/participationRecord")
    public ResponseResult<Object> participationRecord(@RequestParam Long courseId,
                                                      @RequestParam Integer type,
                                                      @RequestParam(required = false) String keyword,
                                                      @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                      @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        log.debug("参与记录 keyword: {}, pageNum: {}, pageSize: {}", keyword, pageNum, pageSize);
        Map<String, Object> params = new HashMap<>();
        params.put("courseId", courseId);
        params.put("type", type);
        params.put("keyword", keyword);

        PageHelper.startPage(pageNum, pageSize);
        List<FsUserCourseParticipationRecordVO> record = fsUserCourseService.getParticipationRecordByMap(params);
        return ResponseResult.ok(new PageInfo<>(record));
    }
}
