package com.fs.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.R;
import com.fs.course.domain.FsUserCourse;
import com.fs.course.param.FsCourseLinkCreateParam;
import com.fs.course.param.FsCourseLinkMiniParam;
import com.fs.course.param.FsCourseLinkRoomParam;
import com.fs.course.param.FsCourseListBySidebarParam;
import com.fs.course.param.newfs.UserCourseVideoPageParam;
import com.fs.course.service.IFsCourseLinkService;
import com.fs.course.service.IFsUserCoursePeriodService;
import com.fs.course.service.IFsUserCourseService;
import com.fs.course.service.IFsUserCourseVideoService;
import com.fs.course.vo.FsCourseListBySidebarVO;
import com.fs.course.vo.FsCourseVideoListBySidebarVO;
import com.fs.course.vo.newfs.FsUserCourseVideoPageListVO;
import com.fs.qw.domain.QwUser;
import com.fs.qw.service.IQwExternalContactService;
import com.fs.voice.utils.StringUtil;
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.List;


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

    @Autowired
    private IFsUserCourseVideoService fsUserCourseVideoService;

    @Autowired
    private IFsUserCourseService fsUserCourseService;

    @Autowired
    private IFsUserCoursePeriodService fsUserCoursePeriodService;

    @Autowired
    private IQwExternalContactService qwExternalContactService;

    @Autowired
    private IFsCourseLinkService courseLinkService;


    @ApiOperation("课程视频详情")
    @GetMapping(value = "/videoDetails")
    public R getVideoDetails(Long videoId) {
        return R.ok().put("data",fsUserCourseVideoService.getVideoDetails(videoId));
    }


    @PostMapping("/getFsCourseListBySidebar")
    @ApiOperation("获取视频课程下拉列表 侧边栏")
    public R getFsCourseListBySidebar(@RequestBody FsCourseListBySidebarParam param) {

        QwUser qwUser = qwExternalContactService.getQwUserByRedis(param.getCorpId().trim(),param.getQwUserId().trim());

        if (qwUser == null || qwUser.getCompanyId() == null) {
            return R.error("员工未绑定 销售公司 或 未获取到员工信息，请重试！");
        }
        param.setCompanyId(qwUser.getCompanyId());

        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsCourseListBySidebarVO> fsCourseListBySidebar = fsUserCourseService.getFsCourseListBySidebar(param);
        PageInfo<FsCourseListBySidebarVO> result = new PageInfo<>(fsCourseListBySidebar);
        return R.ok().put("data", result);
    }

    @PostMapping("/getFsCourseVideoListBySidebar")
    @ApiOperation("获取视频课程的课节下拉列表 侧边栏")
    public R getFsCourseVideoListBySidebar(@RequestBody FsCourseListBySidebarParam param) {

        if (param.getCourseId()==null){
            return R.error("课程id不能为空");
        }

        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsCourseVideoListBySidebarVO> videoListBySidebar = fsUserCourseVideoService.getFsCourseVideoListBySidebar(param);
        PageInfo<FsCourseVideoListBySidebarVO> result = new PageInfo<>(videoListBySidebar);
        return R.ok().put("data", result);
    }

    /**
    * 创建 发客户小程序
    */

    @RepeatSubmit
    @PostMapping("/createMiniLink")
    public R createMiniLink(@RequestBody FsCourseLinkMiniParam param) {

        if (param.getCourseId()==null){
            return R.error("课程id不能为空");
        }
        if (param.getVideoId()==null){
            return R.error("视频id不能为空");
        }
        if (StringUtil.strIsNullOrEmpty(param.getQwUserId())){
            return R.error("用户id不能为空");
        }
        if (StringUtil.strIsNullOrEmpty(param.getCorpId())){
            return R.error("企业id不能为空");
        }

        if (param.getExternalUserId()==null){
            return R.error("客户id不能为空");
        }

        return fsUserCourseVideoService.createMiniLink(param);
    }

    /**
    * 创建发卡片
    */
    @RepeatSubmit
    @PostMapping("/createCartLink")
    public R createCartLink(@RequestBody  FsCourseLinkMiniParam param) {

        if (param.getCourseId()==null){
            return R.error("课程id不能为空");
        }
        if (param.getVideoId()==null){
            return R.error("视频id不能为空");
        }
        if (StringUtil.strIsNullOrEmpty(param.getQwUserId())){
            return R.error("用户id不能为空");
        }
        if (StringUtil.strIsNullOrEmpty(param.getCorpId())){
            return R.error("企业id不能为空");
        }

        if (param.getExternalUserId()==null){
            return R.error("客户id不能为空");
        }

        if (param.getType()==null || param.getType()==1){
            return fsUserCourseVideoService.createCartLink(param);
        }else if (param.getType()==2){
            return fsUserCourseVideoService.createMiniLink(param);
        } else {
            return fsUserCourseVideoService.createRoomMiniLink(param);
        }

    }

    @GetMapping("/createRoomLink")
    @ApiOperation("创建发群链接")
    public R createRoomLink(FsCourseLinkMiniParam param) {
        QwUser qwUser = qwExternalContactService.getQwUserByRedis(param.getCorpId().trim(),param.getQwUserId().trim());
        if (qwUser==null||qwUser.getCompanyId()==null){
            return R.error("无权限");
        }
        if (param.getCourseId()==null){
            return R.error("课程id不能为空");
        }
        if (param.getVideoId()==null){
            return R.error("视频id不能为空");
        }
        if (StringUtil.strIsNullOrEmpty(param.getQwUserId())){
            return R.error("用户id不能为空");
        }
        if (StringUtil.strIsNullOrEmpty(param.getCorpId())){
            return R.error("企业id不能为空");
        }

        return fsUserCourseVideoService.createRoomMiniLink(param);
    }

}
