package com.fs.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.fs.common.core.domain.R;
import com.fs.common.core.domain.ResponseResult;
import com.fs.course.domain.FsUserCourse;
import com.fs.course.param.FsCourseLinkCreateParam;
import com.fs.course.param.FsCourseLinkRoomParam;
import com.fs.course.param.newfs.FsUserCourseListParam;
import com.fs.course.param.newfs.UserCourseVideoPageParam;
import com.fs.course.service.IFsCourseLinkService;
import com.fs.course.service.IFsUserCourseService;
import com.fs.course.service.IFsUserCourseVideoService;
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.fs.qw.domain.QwUser;
import com.fs.qw.service.IQwUserService;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


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

    @Autowired
    private IFsUserCourseVideoService fsUserCourseVideoService;

    @Autowired
    private IFsUserCourseService fsUserCourseService;

    @Autowired
    private IQwUserService qwUserService;

    @GetMapping("/pageList")
    @ApiOperation("课程分页列表")
    public ResponseResult<PageInfo<FsUserCourseVideoPageListVO>> list(UserCourseVideoPageParam param) {
        QwUser qwUser = qwUserService.getByQwUserIdAndCorId(param.getQwUserId(), param.getCorpId());
        if (qwUser==null||qwUser.getCompanyId()==null){
            return ResponseResult.fail(500,"无权限");
        }
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        param.setCompanyId(qwUser.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) {
        QwUser qwUser = qwUserService.getByQwUserIdAndCorId(param.getQwUserId(), param.getCorpId());
        if (qwUser==null||qwUser.getCompanyId()==null){
            return ResponseResult.fail(500,"无权限");
        }
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        param.setCompanyId(qwUser.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) {
        QwUser qwUser = qwUserService.getByQwUserIdAndCorId(param.getQwUserId(), param.getCorpId());
        if (qwUser==null||qwUser.getCompanyId()==null){
            return ResponseResult.fail(500,"无权限");
        }
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        param.setCompanyId(qwUser.getCompanyId());
        List<FsUserVideoListVO> listCourseVideo = fsUserCourseVideoService.getListCourseVideo(param);
        PageInfo<FsUserVideoListVO> result = new PageInfo<>(listCourseVideo);
        return ResponseResult.ok(result);
    }

    @Autowired
    private IFsCourseLinkService courseLinkService;

    @GetMapping("/createRoomLink")
    @ApiOperation("创建发群链接")
    public R createRoomLink(FsCourseLinkRoomParam param) {
        QwUser qwUser = qwUserService.getByQwUserIdAndCorId(param.getQwUserId(), param.getCorpId());
        if (qwUser==null||qwUser.getCompanyId()==null){
            return R.error("无权限");
        }
        FsCourseLinkCreateParam createParam = new FsCourseLinkCreateParam();
        createParam.setCourseId(param.getCourseId());
        createParam.setVideoId(param.getVideoId());
        createParam.setCorpId(param.getCorpId());
        createParam.setCompanyUserId(qwUser.getCompanyUserId());
        createParam.setCompanyId(qwUser.getCompanyId());
        createParam.setQwUserId(qwUser.getId().toString());
        String linkUrl;
        R createLink = courseLinkService.createRoomLinkUrl(createParam);
        if (createLink.get("code").equals(500)){
            return R.error("链接生成失败！");
        }
        linkUrl = (String) createLink.get("url");

        FsUserCourse course = fsUserCourseService.selectFsUserCourseByCourseId(param.getCourseId());

        JSONObject news = new JSONObject(true); // true 表示保持字段顺序
        news.put("link", linkUrl);
        news.put("title", course.getCourseName());
        news.put("desc", param.getTitle());
        news.put("imgUrl", course.getImgUrl());
        return R.ok().put("news",news);
    }

}
