package com.tanpu.community.manager;

import com.tanpu.common.util.JsonUtil;
import com.tanpu.community.api.beans.qo.CommentQo;
import com.tanpu.community.api.beans.req.theme.CreateCommentReq;
import com.tanpu.community.api.enums.CollectionTypeEnum;
import com.tanpu.community.api.enums.CommentTypeEnum;
import com.tanpu.community.dao.entity.community.CommentEntity;
import com.tanpu.community.dao.entity.community.HomePageEntity;
import com.tanpu.community.service.CollectionService;
import com.tanpu.community.service.CommentService;
import com.tanpu.community.service.HomePageService;
import com.tanpu.community.util.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class CommentManager {

    @Autowired
    private CommentService commentService;

    @Autowired
    private HomePageService homePageService;

    @Autowired
    private CollectionService collectionService;

    // 评论(对主题)
    public void comment(CreateCommentReq req, String userId) {
        CommentEntity commentEntity = CommentEntity.builder()
                .themeId(req.getThemeId())
                .parentId(req.getParentId())
                .replyId(req.getReplyId())
                .authorId(userId)
                .content(JsonUtil.toJson(req.getComment()))
                .commentType(CommentTypeEnum.THEME.getCode())
                .build();

        commentService.insertComment(commentEntity);
    }

    public List<CommentQo> queryComments(String themeId,String userId) {
        List<CommentEntity> commentEntities = commentService.selectByThemeIdAndParentId(themeId, null);
        List<CommentQo> commentQos = ConvertUtil.commentEntity2Qos(commentEntities);


        Set<String> likeCommentList = collectionService.getListByUser(userId, CollectionTypeEnum.LIKE_COMMENT);

        for (CommentQo commentQo : commentQos) {
            //用户信息
            String authorId = commentQo.getAuthorId();
            HomePageEntity homePageEntity = homePageService.selectByUserId(authorId);
            commentQo.setUserImg(homePageEntity.getHeadImg());
            commentQo.setNickName(homePageEntity.getNickName());
            //是否点赞及点赞数
            String commentId = commentQo.getCommentId();
            if (likeCommentList.contains(commentId)){
                commentQo.setLiked(true);
            }else {
                commentQo.setLiked(false);
            }
            Integer countByTypeAndId = collectionService.getCountByTypeAndId(commentId,CollectionTypeEnum.LIKE_COMMENT);
            commentQo.setLikeCount(countByTypeAndId);

            //二级评论,本期不上线
//            List<CommentEntity> CommentLv2Entities = commentService.selectByThemeIdAndParentId(themeId, commentId);
//            List<CommentLv2Qo> commentLv2Qos = ConvertUtil.commentLv2Entity2Qos(CommentLv2Entities);
//            commentQo.setCommentLv2Qos(commentLv2Qos);
        }

        return commentQos;
    }
}