package com.slodon.b2b2c.controller.video.admin;

import com.slodon.b2b2c.aop.OperationLogger;
import com.slodon.b2b2c.core.constant.VideoConst;
import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PageVO;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.model.video.SvideoCommentModel;
import com.slodon.b2b2c.model.video.SvideoReplyModel;
import com.slodon.b2b2c.model.video.SvideoVideoModel;
import com.slodon.b2b2c.model.video.VideoSettingModel;
import com.slodon.b2b2c.video.example.SvideoCommentExample;
import com.slodon.b2b2c.video.example.SvideoReplyExample;
import com.slodon.b2b2c.video.example.SvideoVideoExample;
import com.slodon.b2b2c.video.pojo.SvideoComment;
import com.slodon.b2b2c.video.pojo.SvideoReply;
import com.slodon.b2b2c.video.pojo.SvideoVideo;
import com.slodon.b2b2c.vo.video.CommentVO;
import com.slodon.b2b2c.vo.video.ReplyVO;
import com.slodon.b2b2c.vo.video.VideoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(tags = "admin-评论管理")
@RestController
@RequestMapping("v3/video/admin/video/comment")
public class AdminCommentController extends BaseController {

    @Resource
    private SvideoVideoModel svideoVideoModel;
    @Resource
    private SvideoCommentModel svideoCommentModel;
    @Resource
    private SvideoReplyModel svideoReplyModel;
    @Resource
    private VideoSettingModel videoSettingModel;

    @ApiOperation("视频列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "videoName", value = "视频名称", paramType = "query"),
            @ApiImplicitParam(name = "memberName", value = "会员名称", paramType = "query"),
            @ApiImplicitParam(name = "memberNickname", value = "会员昵称", paramType = "query"),
            @ApiImplicitParam(name = "videoType", value = "视频类型:1-视频;2-图文", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("list")
    public JsonResult<PageVO<VideoVO>> list(HttpServletRequest request, String videoName, String memberName, String memberNickname, Integer videoType) {
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        SvideoVideoExample example = new SvideoVideoExample();
        example.setVideoNameLike(videoName);
        example.setMemberNameLike(memberName);
        example.setMemberNicknameLike(memberNickname);
        example.setVideoType(videoType);
        example.setState(VideoConst.STATE_2);
        example.setIsDelete(VideoConst.IS_DELETE_0);
        List<SvideoVideo> list = svideoVideoModel.getSvideoVideoList(example, pager);
        List<VideoVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(svideoVideo -> {
                //设置默认头像
                if (StringUtil.isEmpty(svideoVideo.getMemberAvatar())) {
                    svideoVideo.setMemberAvatar(videoSettingModel.getVideoSettingBySettingName("author_default_profile").getValue());
                }
                //无昵称,用会员账号
                if (StringUtil.isEmpty(svideoVideo.getMemberNickname())){
                    svideoVideo.setMemberNickname(svideoVideo.getMemberName());
                }
                vos.add(new VideoVO(svideoVideo));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("查看评论列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "videoId", value = "视频id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "authorName", value = "评论人名称", paramType = "query"),
            @ApiImplicitParam(name = "content", value = "评论内容", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "评论开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "评论结束时间", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("commentList")
    public JsonResult<PageVO<CommentVO>> commentList(HttpServletRequest request, Integer videoId, String authorName,
                                                     String content, Date startTime, Date endTime) {
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        SvideoCommentExample example = new SvideoCommentExample();
        example.setVideoId(videoId);
        example.setFromAuthorNameLike(authorName);
        example.setContentLike(content);
        example.setCreateTimeAfter(startTime);
        example.setCreateTimeBefore(endTime);
        List<SvideoComment> list = svideoCommentModel.getSvideoCommentList(example, pager);
        List<CommentVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(comment -> {
                vos.add(new CommentVO(comment));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("删除视频评论")
    @OperationLogger(option = "删除视频评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "videoId", value = "视频id", required = true)
    })
    @PostMapping("del")
    public JsonResult del(HttpServletRequest request, Integer videoId) {
        AssertUtil.notNullOrZero(videoId, "视频id参数错误");

        SvideoCommentExample example = new SvideoCommentExample();
        example.setVideoId(videoId);
        svideoCommentModel.deleteSvideoCommentByExample(example);

        //评论删除后，短视频的评论数量清零
        SvideoVideo svideoVideo = new SvideoVideo();
        svideoVideo.setVideoId(videoId);
        svideoVideo.setCommentNum(0);
        svideoVideo.setUpdateTime(new Date());
        svideoVideoModel.updateSvideoVideo(svideoVideo);
        return SldResponse.success("删除成功");
    }

    @ApiOperation("查看回复列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId", value = "评论id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "fromAuthorName", value = "回复人名称", paramType = "query"),
            @ApiImplicitParam(name = "toAuthorName", value = "被回复人名称", paramType = "query"),
            @ApiImplicitParam(name = "content", value = "回复内容", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "回复开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "回复结束时间", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("replyList")
    public JsonResult<PageVO<ReplyVO>> replyList(HttpServletRequest request, Integer commentId, String fromAuthorName,
                                                 String toAuthorName, String content, Date startTime, Date endTime) {

        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        SvideoReplyExample example = new SvideoReplyExample();
        example.setCommentId(commentId);
        example.setFromAuthorNameLike(fromAuthorName);
        example.setToAuthorNameLike(toAuthorName);
        example.setCreateTimeAfter(startTime);
        example.setCreateTimeBefore(endTime);
        example.setContentLike(content);
        List<SvideoReply> list = svideoReplyModel.getSvideoReplyList(example, pager);
        List<ReplyVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(reply -> {
                vos.add(new ReplyVO(reply));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("删除评论")
    @OperationLogger(option = "删除评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId", value = "评论id", required = true)
    })
    @PostMapping("delComment")
    public JsonResult delComment(HttpServletRequest request, Integer commentId) {
        svideoCommentModel.deleteSvideoComment(commentId);
        return SldResponse.success("删除成功");
    }

    @ApiOperation("删除回复")
    @OperationLogger(option = "删除回复")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "replyId", value = "回复id", required = true)
    })
    @PostMapping("delReply")
    public JsonResult delReply(HttpServletRequest request, Integer replyId) {
        svideoReplyModel.deleteSvideoReply(replyId);
        return SldResponse.success("删除成功");
    }
}
