package com.yonge.cooleshow.student.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.search.VideoLessonEvaluateSearch;
import com.yonge.cooleshow.biz.dal.dto.search.VideoLessonGroupSearch;
import com.yonge.cooleshow.biz.dal.entity.VideoLessonEvaluate;
import com.yonge.cooleshow.biz.dal.service.AppVersionInfoService;
import com.yonge.cooleshow.biz.dal.service.VideoLessonEvaluateService;
import com.yonge.cooleshow.biz.dal.service.VideoLessonGroupDetailService;
import com.yonge.cooleshow.biz.dal.service.VideoLessonGroupService;
import com.yonge.cooleshow.biz.dal.vo.LessonGroupVo;
import com.yonge.cooleshow.biz.dal.vo.VideoLessonEvaluateVo;
import com.yonge.cooleshow.biz.dal.vo.VideoLessonStudentVo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;

/**
 * @Author: cy
 * @Date: 2022/4/2
 */
@RestController
@RequestMapping("/videoLesson")
@Api(tags = "视频课")
@Validated
public class VideoLessonController extends BaseController {
    @Autowired
    private VideoLessonGroupDetailService detailService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private VideoLessonEvaluateService evaluateService;
    @Autowired
    private VideoLessonGroupService videoLessonGroupService;
    @Autowired
    private AppVersionInfoService appVersionInfoService;
    /**
     * @Description: 根据组id查询视频课
     * @Author: cy
     * @Date: 2022/4/2
     */
    @ApiOperation(value = "根据组id查询视频课")
    @GetMapping(value = "/selectVideoLesson")
    public HttpResponseResult<VideoLessonStudentVo> selectVideoLesson(@NotNull(message = "视频组id不能为空") Long groupId) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(detailService.selectVideoLesson(groupId,user.getId()));
    }

    /**
     * @Description: 视频课评价
     * @Author: cy
     * @Date: 2022/4/11
     */
    @ApiOperation(value = "视频课评价", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/evaluate", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<Object> evaluate(@Validated @RequestBody VideoLessonEvaluate evaluate) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }

        evaluateService.evaluate(evaluate, sysUser);
        return succeed();
    }

    /**
     * @Description: 根据视频课id查评论
     * @Author: cy
     * @Date: 2022/4/11
     */
    @ApiOperation(value = "根据视频课id查评论")
    @PostMapping(value = "/page")
    public HttpResponseResult<PageInfo<VideoLessonEvaluateVo>> selectEvaluate(@RequestBody VideoLessonEvaluateSearch query) {
        IPage<VideoLessonEvaluateVo> pages = evaluateService.selectEvaluate(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }

    /**
     * @Description: 查询视频课组
     * @Author: cy
     * @Date: 2022/4/11
     */
    @ApiOperation(value = "查询视频课组")
    @PostMapping(value = "/selectGroup")
    public HttpResponseResult<PageInfo<LessonGroupVo>> page(@RequestBody VideoLessonGroupSearch query) {
        // 检查app版本
        query.setAuditVersion(appVersionInfoService.getAppAuditVersion(query.getPlatform(),query.getVersion()));

        IPage<LessonGroupVo> pages = videoLessonGroupService.selectLessonGroup(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }

    @ApiOperation(value = "学生端-我的-我的课程-购买的视频课组")
    @PostMapping("/selectMyGroup")
    public HttpResponseResult<PageInfo<LessonGroupVo>> myLessonGroup(@RequestBody VideoLessonGroupSearch query) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        query.setStudentId(user.getId());
        IPage<LessonGroupVo> pages = videoLessonGroupService.selectLessonGroupById(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }
}
