package com.nbclass.activity.controller.casesfirst;

import com.github.pagehelper.PageInfo;
import com.nbclass.activity.model.*;
import com.nbclass.activity.service.CommentService;
import com.nbclass.activity.service.ContentService;
import com.nbclass.exception.ParameterException;
import com.nbclass.util.JWTUtils;
import com.nbclass.vo.base.Result;
import org.springframework.beans.factory.annotation.Autowired;
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.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 评论api
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private ContentService contentService;

    /**
     * 案例详情和评论列表
     * @param contentId
     * @return
     */
    @GetMapping("/getComment")
    public Result getComment(HttpServletRequest request,Long contentId){
        String token = request.getHeader("token");
        String userId = JWTUtils.getUserId(token);
        Content content = commentService.getComment(contentId,Long.parseLong(userId));
        return Result.success(content);
    }

    /**
     * 评论回复列表
     * @param request
     * @param commentId 评论id
     * @return
     */
    @GetMapping("/getCommentReplyList")
    public Result getCommentReplyList(HttpServletRequest request,Long commentId){
        String token = request.getHeader("token");
        String userId = JWTUtils.getUserId(token);
        Comment comment = commentService.getCommentReplyList(commentId,Long.parseLong(userId));
        return Result.success(comment);
    }

    /**
     * 添加用户发布信息
     * @param comment
     * @return
     */
    @PostMapping("/saveComment")
    public Result saveComment(Comment comment,HttpServletRequest request){
        String token = request.getHeader("token");
        String userId = JWTUtils.getUserId(token);
        comment.setUserId(Long.parseLong(userId));
        commentService.saveComment(comment);
        //更新案例总评论数
        contentService.updateComments(comment.getContentId());
        return Result.success();
    }

    /**
     * 添加评论点赞
     * @param commentId 评论Id
     * @return
     */
    @PostMapping("/updatePraiseNo")
    public Result updatePraiseNo(Long commentId, HttpServletRequest request){
        String token = request.getHeader("token");
        String userId = JWTUtils.getUserId(token);
        //根据评论id获取被点赞人Id
        Comment comment = commentService.getCommentUserId(commentId);
        System.out.println(comment);
        Praise praise = new Praise();
        praise.setCommentId(commentId);
        praise.setPraiseId(Long.parseLong(userId));
        praise.setUserId(comment.getUserId());
        commentService.savePraiseNo(praise);
        //评论总数加1
        commentService.updatePraiseno(commentId);
        return Result.success();
    }

    /**
     * 取消评论点赞
     * @return
     */
    @PostMapping("/deletePraise")
    public Result deletePraise(Long commentId,HttpServletRequest request){
        String token = request.getHeader("token");
        String userId = JWTUtils.getUserId(token);
        commentService.deletePraise(commentId,Long.parseLong(userId));
        //总点赞数减一
        commentService.updatePraiseNoONE(commentId);
        return Result.success();
    }

    /**
     * 添加案例点赞
     * @param contentId
     * @return
     */
    @PostMapping("/updateContentPraiseNo")
    public Result updateContentPraiseNo(Long contentId){
        commentService.updateContentPraiseNo(contentId);
        return Result.success();
    }


    /**
     * 用户回复
     * @param comment
     * @param request
     * @return
     */
    @PostMapping("/saveUserReply")
    public Result saveUserReply(Comment comment,HttpServletRequest request){
        String token = request.getHeader("token");
        String userId = JWTUtils.getUserId(token);
        comment.setReplyId(Long.parseLong(userId));
        commentService.saveUserReply(comment);
        //评论总回复数加一
        commentService.updateReplyNumberONE(comment.getCommentId());
        //更新案例总评论数
        contentService.updateComments(comment.getContentId());
        return Result.success();
    }

    /**
     * 用户举报
     * @param report
     * @return
     */
    @PostMapping("/saveReport")
    public Result saveReport(Report report,HttpServletRequest request){
        String token = request.getHeader("token");
        String userId = JWTUtils.getUserId(token);
        report.setReportId(Long.parseLong(userId));
        commentService.saveReport(report);
        return Result.success();
    }

    /**
     * 举报列表获取
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/getReport")
    public Result getReport(Integer page,Integer pageSize,HttpServletRequest request){
        String token = request.getHeader("token");
        String userId = JWTUtils.getUserId(token);
        PageInfo<Report> list = commentService.getReport(page,pageSize,userId);
        return Result.success(list);
    }



    /**
     * 添加标签、索引反馈日志
     * @return
     */
    @PostMapping("/saveLabelLogger")
    public Result saveLabelLogger(LabelLogger labelLogger,HttpServletRequest request){
        String token = request.getHeader("token");
        String userId = JWTUtils.getUserId(token);
        labelLogger.setUserId(Long.parseLong(userId));
        commentService.saveLabelLogger(labelLogger);
        //案例索引标签反馈次数+1
        contentService.updateLabelFeedback(labelLogger.getContentId());
        return Result.success();
    }

    /**
     * 标签、索引反馈日志列表
     * @param request
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/getLabelLogger")
    public Result getLabelLogger(HttpServletRequest request,Integer page,Integer pageSize){
        String token = request.getHeader("token");
        String userId = JWTUtils.getUserId(token);
        PageInfo<LabelLogger> list = commentService.getLabelLogger(page,pageSize,Long.parseLong(userId));
        return Result.success(list);
    }

    /**
     * 新建文件夹
     * @param favoritesFolder
     * @param request
     * @return
     */
    @PostMapping("/saveFavorites")
    public Result saveFavorites(FavoritesFolder favoritesFolder, HttpServletRequest request){
        String token = request.getHeader("token");
        String userId = JWTUtils.getUserId(token);
        //判断文件夹是否重名
        int result = commentService.getFavoritesName(favoritesFolder.getName());
        if(result > 0){
            throw new ParameterException("文件夹已存在!");
        }
        favoritesFolder.setUserId(Long.parseLong(userId));
        commentService.saveFavorites(favoritesFolder);
        return Result.success();
    }

    /**
     * 案例详情的文件夹列表
     * @param request
     * @return
     */
    @GetMapping("/getUserFolder")
    public Result getUserFolder(HttpServletRequest request,Integer page,Integer pageSize,Long commentId){
        String token = request.getHeader("token");
        String userId = JWTUtils.getUserId(token);
        PageInfo<FavoritesFolder> list = commentService.getUserFolder(Long.parseLong(userId),page,pageSize,commentId);
        return Result.success(list);
    }

    /**
     * 收藏案例到文件夹中
     * @param contentId
     * @return
     */
    @PostMapping("/saveFolder")
    public Result saveFolder(Long folderId,Long contentId){
        //判断该文件夹是否已被收藏
        int result = commentService.getFolderExistence(folderId,contentId);
        if(result > 0 ){
            throw new ParameterException("该文件夹已被收藏!");
        }
        //收藏案例到文件夹中
        commentService.saveFolder(folderId,contentId);
        //添加收藏总数
        commentService.saveFolderTotal(folderId);
        //更新案例的收藏总数+1
        commentService.updateCollectionNumber(contentId);
        return Result.success();
    }

    /**
     * 取消文件夹中的案例收藏
     * @param folderId
     * @param contentId
     * @return
     */
    @PostMapping("/deleteFolderResources")
    public Result deleteFolderResources(Long folderId,Long contentId){
        commentService.deleteFolderResources(folderId,contentId);
        //更新案例的收藏总数-1
        commentService.updateCollectionNumberReduce(contentId);
        return Result.success();
    }

    /**
     * 获取用户文件夹列表
     * @param request
     * @return
     */
    @GetMapping("/getUserFolderList")
    public Result getUserFolderList(HttpServletRequest request,Integer page,Integer pageSize){
        String token = request.getHeader("token");
        String userId = JWTUtils.getUserId(token);
        PageInfo<FavoritesFolder> list = commentService.getUserFolderList(Long.parseLong(userId),page,pageSize);
        return Result.success(list);
    }

    /**
     * 我的收藏
     * @param request
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/getMyCollection")
    public Result getMyCollection(HttpServletRequest request,Integer page,Integer pageSize,Long folderId){
        String token = request.getHeader("token");
        String userId = JWTUtils.getUserId(token);
        PageInfo<Content> list = commentService.getMyCollection(Long.parseLong(userId),page,pageSize,folderId);
        return Result.success(list);
    }

    /**
     * 个人中心回复列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/getReplyList")
    public Result getReplyList(HttpServletRequest request,Integer page,Integer pageSize){
        String token = request.getHeader("token");
        String userId = JWTUtils.getUserId(token);
        PageInfo<Comment> list = commentService.getReplyList(Long.parseLong(userId),page,pageSize);
        return Result.success(list);
    }

    /**
     * 点赞列表
     * @param request
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/getFabulousList")
    public Result getFabulousList(HttpServletRequest request,Integer page,Integer pageSize){
        String token = request.getHeader("token");
        String userId = JWTUtils.getUserId(token);
        PageInfo<Comment> list = commentService.getFabulousList(Long.parseLong(userId),page,pageSize);
        return Result.success(list);
    }

    /**
     * 案例打分
     * @param scoring
     * @return
     */
    @PostMapping("/saveScoring")
    public Result saveScoring(HttpServletRequest request,Scoring scoring){
        String token = request.getHeader("token");
        String userId = JWTUtils.getUserId(token);
        scoring.setUserId(Long.parseLong(userId));
        commentService.saveScoring(scoring);
        return Result.success();
    }


}
