package com.fs.app.controller.store;

import com.fs.app.annotation.Login;
import com.fs.app.controller.AppBaseController;
import com.fs.common.core.domain.R;
import com.fs.common.utils.StringUtils;
import com.fs.course.domain.FsUserVideoComment;
import com.fs.course.param.*;
import com.fs.course.service.*;
import com.fs.course.vo.FsUserVideoCommentListUVO;
import com.fs.course.vo.FsUserVideoListUVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api("短视频接口")
@RestController
@RequestMapping(value="/store/app/video")
public class VideoScrmController extends AppBaseController {
    @Autowired
    private IFsUserVideoService videoService;
    @Autowired
    private IFsUserVideoLikeService videoLikeService;
    @Autowired
    private IFsUserVideoFavoriteService videoFavoriteService;
    @Autowired
    private IFsUserVideoCommentService videoCommentService;
    @Autowired
    private IFsUserVideoCommentLikeService videoCommentLikeService;
    @Autowired
    private IFsUserVideoViewService videoViewService;

    @Autowired
    private IRecommendationService recommendationService;

    @ApiOperation("视频列表")
    @GetMapping("/getVideoList")
    public R getVideoList(FsUserVideoListUParam param)
    {
        if (StringUtils.isNotEmpty(getUserId())) {
            param.setUserId(Long.parseLong(getUserId()));
        }
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsUserVideoListUVO> list= videoService.selectFsUserVideoListUVO(param);
        PageInfo<FsUserVideoListUVO> listPageInfo=new PageInfo<>(list);
        if (param.getUserId() != null) {
            // 对分页后的数据进行推荐排序
            list = recommendationService.recommendVideos(param.getUserId(), list);
             //更新PageInfo中的列表
            listPageInfo.setList(list);
        }
        if (param.getVideoId() != null && param.getPageNum()==1) {
            // 获取该 videoId 对应的视频数据
            FsUserVideoListUVO video = videoService.getVideoById(param.getVideoId(), param.getUserId());
            if (video != null) {
                // 将视频数据放到列表的第一个位置
                list.removeIf(v -> v.getId().equals(param.getVideoId())); // 删除原列表中的该视频
                list.add(0, video); // 添加到列表的第一个位置
                listPageInfo.setList(list);
            }
        }
        return R.ok().put("data",listPageInfo);
    }

    @Login
    @ApiOperation("我的收藏视频列表")
    @GetMapping("/getFavoriteVideoList")
    public R getFavoriteVideoList(FsUserVideoFavoriteListUParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsUserVideoListUVO> list= videoService.selectFsUserFavoriteVideoListUVO(param);
        PageInfo<FsUserVideoListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }


    @Login
    @ApiOperation("点赞")
    @PostMapping("/doLike")
    public R doLike(@RequestBody FsUserVideoFollowUParam param, HttpServletRequest request){
        R r=videoLikeService.checkLike(param.getVideoId(),Long.parseLong(getUserId()));
        if(r.get("code").equals(200)){
            //取消点赞
            videoLikeService.unlikeVideo(param.getVideoId(),Long.parseLong(getUserId()));
            videoService.updateLikes(param.getVideoId(),-1);
            return R.ok("已取消喜欢");
        }
        else{
            videoLikeService.likeVideo(param.getVideoId(),Long.parseLong(getUserId()));
            videoService.updateLikes(param.getVideoId(),1);
            return R.ok("已喜欢");
        }
    }


    @Login
    @ApiOperation("收藏")
    @PostMapping("/doFavorite")
    public R doFavorite(@RequestBody FsUserVideoFollowUParam param, HttpServletRequest request){
        R r=videoFavoriteService.checkFavorite(param.getVideoId(),Long.parseLong(getUserId()));
        if(r.get("code").equals(200)){
            //取消点赞
            videoFavoriteService.deleteFavorite(param.getVideoId(),Long.parseLong(getUserId()));
            videoService.updateFavorite(param.getVideoId(),-1);
            return R.ok("已取消收藏");
        }
        else{
            //关注
            videoFavoriteService.favoriteVideo(param.getVideoId(),Long.parseLong(getUserId()));
            //更新课程点赞数
            videoService.updateFavorite(param.getVideoId(),1);
            return R.ok("已收藏");
        }
    }

    @Login
    @ApiOperation("评论")
    @PostMapping("/addComment")
    public R addComment(@RequestBody FsUserVideoCommentAddUParam param , HttpServletRequest request)
    {
        param.setUserId(Long.parseLong(getUserId()));
        return videoCommentService.addComment(param);
    }


    @ApiOperation("评论列表")
    @GetMapping("/getComments")
    public R getCommentsByVideoId( FsUserVideoCommentUParam param)
    {
        if (StringUtils.isNotEmpty(getUserId())){
            param.setUserId(Long.parseLong(getUserId()));
        }
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsUserVideoCommentListUVO> list= videoCommentService.getComments(param);
        PageInfo<FsUserVideoCommentListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }

    @ApiOperation("增加分享数")
    @PostMapping("/shareVideo")
    public R shareVideo( @RequestParam("videoId")Long videoId)
    {
        videoService.updateShares(videoId);
        return R.ok();
    }


    @ApiOperation("评论回复列表")
    @GetMapping("/getReplies")
    public R getRepliesByParentId(FsUserVideoCommentUParam param)
    {
        if (StringUtils.isNotEmpty(getUserId())){
            param.setUserId(Long.parseLong(getUserId()));
        }
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsUserVideoCommentListUVO> list= videoCommentService.getReplies(param);
        PageInfo<FsUserVideoCommentListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }

    @Login
    @ApiOperation("评论点赞")
    @PostMapping("/doLikeComment")
    public R doLikeComment(@Validated @RequestBody FsUserVideoCommentLikeUParam param, HttpServletRequest request){
        boolean isLike =videoCommentLikeService.isCommentLiked(param.getCommentId(),Long.parseLong(getUserId()));
        if(isLike){
            //取消点赞
            videoCommentLikeService.unlikeComment(param.getCommentId(),Long.parseLong(getUserId()));
//            videoCommentService.updateLikes(param.getCommentCode(),-1);
            return R.ok("已取消喜欢");
        }
        else{
            videoCommentLikeService.likeComment(param.getCommentId(),Long.parseLong(getUserId()));
//            videoCommentService.updateLikes(param.getCommentCode(),1);
            return R.ok("已喜欢");
        }
    }

    @Login
    @ApiOperation("删除评论")
    @PostMapping("/delComment")
    public R delComment(@Validated @RequestBody FsUserVideoCommentDelUParam param , HttpServletRequest request)
    {
        FsUserVideoComment comment = videoCommentService.selectFsUserVideoCommentByCommentId(param.getCommentId());
        if (comment.getUserId()-Long.parseLong(getUserId())!=0){
            return R.error("不能修改别人的评论");
        }
        return videoCommentService.delComment(param);
    }

    @ApiOperation("记录开始时间")
    @PostMapping("/startWatchVideo")
    public R startWatchVideo(@RequestParam("videoId")Long videoId)
    {
        if (StringUtils.isNotEmpty(getUserId())){
           return videoViewService.startWatchVideo(videoId,Long.parseLong(getUserId()));
        }
        return R.ok("未登录");
    }

    @ApiOperation("记录结束时间")
    @PostMapping("/endWatchVideo")
    public R endWatchVideo(@RequestParam("videoId")Long videoId)
    {
        if (StringUtils.isNotEmpty(getUserId())){
            return videoViewService.endWatchVideo(videoId,Long.parseLong(getUserId()));
        }
        return R.ok("未登录");
    }

}
