package cn.reghao.tnb.search.app.comment.service;

import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.jutil.jdk.string.SnowFlake;
import cn.reghao.tnb.auth.api.iface.AccountQuery;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.search.app.comment.db.mongo.UserCommentMongo;
import cn.reghao.tnb.search.app.comment.model.po.UserComment;
import cn.reghao.tnb.user.api.dto.UserCard;
import cn.reghao.tnb.user.api.iface.UserService;
import cn.reghao.tnb.search.app.comment.model.dto.CommentSbtDto;
import cn.reghao.tnb.search.app.comment.model.vo.ReplyUser;
import cn.reghao.tnb.search.app.comment.model.vo.UserReply;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2022-06-16 16:34:29
 */
@Service
public class CommentService {
    @DubboReference(check = false, retries = 0, timeout = 60_000)
    private UserService userService;
    @DubboReference(check = false, retries = 0, timeout = 60_000)
    private AccountQuery accountQuery;

    private int pageSize = 10;
    private final SnowFlake idGenerator;
    private final UserCommentMongo userCommentMongo;

    public CommentService(UserCommentMongo userCommentMongo) {
        this.idGenerator = new SnowFlake(1L, 1L);
        this.userCommentMongo = userCommentMongo;
    }

    public void saveComment(CommentSbtDto commentSbtDto) {
        long commentId = idGenerator.nextId();
        UserComment userComment = new UserComment(commentId, commentSbtDto);
        userCommentMongo.save(userComment);
        /*VideoPost videoPost = videoPostMapper.findByVideoId(postId);
        if (videoPost != null) {
            long publishBy = videoPost.getPublishBy();
            int msgType = MsgType.commentMsg.getValue();
            String title = "新评论";
            String content = "发表了新评论";
            UserMessageDto userMessageDto = new UserMessageDto(msgType, title, content, publishBy);
            //userService.sendUserMessage(userMessageDto);
        }*/
    }

    @Deprecated
    public void saveBiliComment(CommentSbtDto commentSbtDto) {
        /*long commentId = commentSbtDto.getNewComment().getId();
        UserComment userComment = userCommentMongo.findByCommentId(commentId);
        if (userComment != null) {
            return;
        }

        CommentData commentData = commentSbtDto.getNewComment();
        long parentId = commentSbtDto.getParent().getId();
        userComment = new UserComment(parentId, commentId, commentData);
        userCommentMongo.save(userComment);*/
    }

    public PageList<UserReply> getUserComment(String videoId, int pageNumber) {
        long total = userCommentMongo.countByPostId(videoId);
        int pageSize = 10;
        List<UserReply> list = userCommentMongo.findByPostId(videoId, pageNumber).stream()
                .map(this::getCommentVo)
                .collect(Collectors.toList());
        return PageList.pageList(pageNumber, pageSize, (int) total, list);
    }

    private ReplyUser getReplyUser(long userId) {
        String userIdStr = accountQuery.getUserIdStr(userId);
        ReplyUser commentUser = new ReplyUser();
        commentUser.setUserId(userIdStr);
        UserCard userCard = userService.getUserAvatar(userId);
        if (userCard != null) {
            commentUser.setName(userCard.getScreenName());
            commentUser.setAvatar(userCard.getAvatarUrl());
        } else {
            commentUser.setName("哈哈");
            commentUser.setAvatar("avatar.jpg");
        }

        long loginUser = UserContext.getUserId();
        if (loginUser == userId) {
            commentUser.setAuthor(true);
        }

        return commentUser;
    }

    private UserReply getCommentVo(UserComment userComment) {
        Long userId = userComment.getPublishBy();
        ReplyUser commentUser = getReplyUser(userId);
        UserReply userReply = new UserReply(userComment);
        userReply.setUser(commentUser);

        long commentId = userComment.getCommentId();
        long total = userCommentMongo.countChildComment(commentId);
        if (total > 0) {
            List<UserReply> childComments = userCommentMongo.findByParentId(commentId, 1, 3).stream()
                    .map(userComment1 -> {
                        Long userId1 = userComment1.getPublishBy();
                        ReplyUser commentUser1 = getReplyUser(userId1);
                        UserReply userReply1 = new UserReply(userComment1);
                        userReply1.setUser(commentUser1);
                        return userReply1;
                    })
                    .collect(Collectors.toList());
            userReply.setChildren(childComments);
            userReply.setTotal(total);
        }
        return userReply;
    }

    public PageList<UserReply> getChildComment(long commentId, int pageNumber) {
        Long total = userCommentMongo.countChildComment(commentId);
        List<UserReply> list = userCommentMongo.findByParentId(commentId, pageNumber, pageSize).stream()
                .map(this::getCommentVo)
                .collect(Collectors.toList());
        return PageList.pageList(pageNumber, pageSize, total.intValue(), list);
    }
}
