package com.fs.app.controller;

import com.fs.app.annotation.Login;
import com.fs.common.core.domain.R;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.StringUtils;
import com.fs.course.domain.FsUserCourseComment;
import com.fs.course.domain.FsUserCourseCommentLike;
import com.fs.course.mapper.FsUserCourseMapper;
import com.fs.course.param.FsUserCourseCommentAddParam;
import com.fs.course.param.FsUserCourseCommentEditParam;
import com.fs.course.param.FsUserCourseCommentUParam;
import com.fs.course.service.IFsUserCourseCommentLikeService;
import com.fs.course.service.IFsUserCourseCommentService;
import com.fs.course.vo.FsUserCourseCommentListUVO;
import com.fs.course.vo.FsUserCourseCommentReplyListUVO;
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.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 课堂评论Controller
 *
 * @author fs
 * @date 2024-05-15
 */
@Api("课堂评论接口")
@RestController
@RequestMapping(value="/app/course/comment")
public class CourseCommentController extends AppBaseController
{
    @Autowired
    private IFsUserCourseCommentService fsUserCourseCommentService;

    @Autowired
    private IFsUserCourseCommentLikeService fsUserCourseCommentLikeService;

    @Autowired
    private FsUserCourseMapper fsUserCourseMapper;
    /**
     * 查询课堂评论列表
     */
    @ApiOperation("课堂评论列表")
    @GetMapping("/list")
    public R list(FsUserCourseCommentUParam param)
    {
        if (StringUtils.isNotEmpty(getUserId())){
            param.setUserId(Long.parseLong(getUserId()));
        }
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsUserCourseCommentListUVO> list = fsUserCourseCommentService.selectFsUserCourseCommentListUVO(param);
        list.forEach(comment -> {
            List<FsUserCourseCommentReplyListUVO> replies = fsUserCourseCommentService.selectFsUserCourseCommentReplyListUVO(comment.getCommentId());
            comment.setReplyList(replies);
        });
        PageInfo<FsUserCourseCommentListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }

    /**
     * 查询我的评论列表
     */
    @ApiOperation("我的评论列表")
    @GetMapping("/myList")
    public R myList(FsUserCourseCommentUParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsUserCourseCommentListUVO> list = fsUserCourseCommentService.selectFsUserCourseCommentMyListUVO(Long.parseLong(getUserId()));
        PageInfo<FsUserCourseCommentListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }
    /**
     * 新增课堂评论
     */
    @Login
    @ApiOperation("新增课堂评论")
    @Transactional
    @PostMapping("/addComment")
    public R add(@RequestBody FsUserCourseCommentAddParam param)
    {
        FsUserCourseComment fsUserCourseComment = new FsUserCourseComment();
        fsUserCourseComment.setUserId(Long.parseLong(getUserId()));
        fsUserCourseComment.setCourseId(param.getCourseId());
        fsUserCourseComment.setContent(param.getContent());
        fsUserCourseComment.setType(param.getType());
        if (param.getType()==1){
            fsUserCourseComment.setParentId(0L);
        }else {
            fsUserCourseComment.setParentId(param.getParentId());
        }
        fsUserCourseComment.setToUserId(param.getToUserId());
        fsUserCourseComment.setCreateTime(new Date());
        fsUserCourseCommentService.insertFsUserCourseComment(fsUserCourseComment);
        if (param.getType()==2){
            fsUserCourseCommentService.addComment(param.getParentId());
        }

        return R.ok();
    }

    /**
     * 修改课堂评论
     */
    @Login
    @ApiOperation("修改课堂评论")
    @PutMapping("/editComment")
    public R edit(@RequestBody FsUserCourseCommentEditParam param)
    {
        FsUserCourseComment comment = fsUserCourseCommentService.selectFsUserCourseCommentByCommentId(param.getCommentId());
        System.out.println(comment);
        if (comment.getUserId()-Long.parseLong(getUserId())!=0){
            return R.error("不能修改别人的评论");
        }
        FsUserCourseComment fsUserCourseComment = new FsUserCourseComment();
        fsUserCourseComment.setCommentId(param.getCommentId());
        fsUserCourseComment.setContent(param.getContent());
        fsUserCourseComment.setUpdateTime(new Date());
        if (fsUserCourseCommentService.updateFsUserCourseComment(fsUserCourseComment)>0) {
            return R.ok();
        }else {
            return R.error();
        }

    }

    /**
     * 删除课堂评论
     */
    @Login
    @ApiOperation("删除课堂评论")
	@DeleteMapping("/{commentId}")
    public R remove(@PathVariable Long commentId)
    {
        FsUserCourseComment comment = fsUserCourseCommentService.selectFsUserCourseCommentByCommentId(commentId);
        if (comment.getUserId()-Long.parseLong(getUserId())!=0){
            return R.error("不能修改别人的评论");
        }
        if(fsUserCourseCommentService.deleteFsUserCourseCommentByCommentId(commentId)>0){
            if (comment.getType()==2){
                fsUserCourseCommentService.minusComment(comment.getParentId());
            }
            return R.ok("删除成功");
        }else {
            return R.error("删除失败");
        }
    }


    @Login
    @ApiOperation("点赞/取消点赞")
    @Transactional
    @PostMapping("/doLike")
    public R doLike(@RequestBody FsUserCourseCommentLike like){
        FsUserCourseCommentLike fsUserCourseCommentLike = fsUserCourseCommentLikeService.selectFsUserCourseCommentLikeByUserIdAndCommentId(Long.parseLong(getUserId()), like.getCommentId());
        if (fsUserCourseCommentLike!=null&&fsUserCourseCommentLike.getId()!=null){
            fsUserCourseCommentLikeService.deleteFsUserCourseCommentLikeById(fsUserCourseCommentLike.getId());
            if (fsUserCourseCommentService.updateLikes(like.getCommentId(),-1)>0){
                return R.ok("取消点赞");
            }else {
                throw  new CustomException("失败");
            }

        }else {
            FsUserCourseCommentLike addLike = new FsUserCourseCommentLike();
            addLike.setCreateTime(new Date());
            addLike.setUserId(Long.parseLong(getUserId()));
            addLike.setCommentId(like.getCommentId());
            fsUserCourseCommentLikeService.insertFsUserCourseCommentLike(addLike);
            if (fsUserCourseCommentService.updateLikes(like.getCommentId(),1)>0){
                return R.ok("点赞");
            }else {
                throw  new CustomException("失败");
            }
        }

    }
}
