package com.tanpu.community.manager;

import com.tanpu.common.exception.BizException;
import com.tanpu.community.api.beans.TopicBriefInfoDTO;
import com.tanpu.community.api.beans.TopicDTO;
import com.tanpu.community.api.beans.TopicDataAnalysDTO;
import com.tanpu.community.api.constants.RedisKeyConstant;
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.dao.entity.community.TopicEntity;
import com.tanpu.community.service.*;
import com.tanpu.community.util.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class TopicManager {

    @Autowired
    private TopicService topicService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private ThemeService themeService;

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private CommentService commentService;

    //新增话题
    public void insertTopic(String topicTitle, String userId) {
        if (topicService.queryByTitile(topicTitle) == null){
            topicService.addTopic(topicTitle, userId);
        }
        return;

    }

    //话题详情列表
    public List<TopicDTO> getAllTopicDetail() {
        return ConvertUtil.topicEntitiesToDTOs(topicService.queryAll());
    }


    //话题简介列表
    public List<TopicBriefInfoDTO> getAllTopicBriefInfo() {
        List<TopicEntity> allTopic = topicService.queryAll();
        List<TopicBriefInfoDTO> topicDTOS = ConvertUtil.topicEntitiesToBriefDTOs(allTopic);
        //讨论数=发布主题贴数+回复总数
        for (TopicBriefInfoDTO topicDTO : topicDTOS) {
            Long discussAmount = redisService.getLong(RedisKeyConstant.TOPIC_DISCUSS_AMOUNT_ + topicDTO.getTopicId());
            topicDTO.setDiscussionAmount(discussAmount);
        }
        return topicDTOS;
    }


    public void setTopTopic(String topicId, boolean setTop) {
        TopicEntity topicEntity = topicService.queryById(topicId);
        if (topicEntity == null) {
            throw new BizException("找不到话题,id:" + topicId);
        }
        if (setTop) {
            topicService.updateTopicToTop(topicId);
        } else {
            topicService.updateTopicNotTop(topicId);
        }
    }

    public void setTopicConceal(String topicId, boolean setConceal) {
        TopicEntity topicEntity = topicService.queryById(topicId);
        if (topicEntity == null) {
            throw new BizException("找不到话题,id:" + topicId);
        }
        if (setConceal) {
            topicService.updateTopicToConceal(topicId);
        } else {
            topicService.updateTopicNotConceal(topicId);
        }
    }

    public TopicDTO getDetail(String topicId) {
        TopicEntity topicEntity = topicService.queryById(topicId);
        if (topicEntity == null) {
            throw new BizException("找不到话题,id:" + topicId);
        }
        TopicDTO topicDTO = ConvertUtil.topicEntityToDTO(topicEntity);
        topicDTO.setViewAmount(redisService.getLong(RedisKeyConstant.TOPIC_TOTAL_VIEW_AMOUNT_ + topicId));
        topicDTO.setLikeAmount(redisService.getLong(RedisKeyConstant.TOPIC_LIKE_AMOUNT_ + topicId));
        topicDTO.setUserAmount(redisService.getLong(RedisKeyConstant.TOPIC_USER_AMOUNT_ + topicId));
        return topicDTO;
    }

    public void modifyViewAmount(String topicId, Long modifyMount) {
        TopicEntity topicEntity = topicService.queryById(topicId);
        if (topicEntity == null) {
            throw new BizException("找不到话题,id:" + topicId);
        }
        topicService.modifyViewAmount(topicId, modifyMount);
        if (modifyMount > 0) {
            redisService.incr(RedisKeyConstant.TOPIC_TOTAL_VIEW_AMOUNT_ + topicId, modifyMount);
        } else {
            redisService.decr(RedisKeyConstant.TOPIC_TOTAL_VIEW_AMOUNT_ + topicId, -modifyMount);
        }

    }


    public void refreshRedisCache() {
        List<TopicEntity> topicEntities = topicService.queryAll();
        for (TopicEntity topic : topicEntities) {
            String topicId = topic.getId();
            Long viewAmountModify = topic.getViewAmountModify();
            List<ThemeEntity> themeEntities = themeService.selectByTopic(topicId);
            List<String> themeIds = themeEntities.stream().map(ThemeEntity::getId).collect(Collectors.toList());
            Long likeAmountByThemeIds = collectionService.getLikeAmountByThemeIds(themeIds);
            Long bookAmountByThemeIds = collectionService.getBookAmountByThemeIds(themeIds);
            Long commentAmountByThemeIds = commentService.getCommentAmountByThemeIds(themeIds);
            Set<String> postUsers = themeService.getPostUserAmount(themeIds);
            Set<String> commentUsers = commentService.getCommentUserAmount(themeIds);
            HashSet<String> totalUsers = new HashSet<>(postUsers);
            totalUsers.addAll(commentUsers);
            redisService.set(RedisKeyConstant.TOPIC_LIKE_AMOUNT_ + topicId, likeAmountByThemeIds);
            redisService.set(RedisKeyConstant.TOPIC_BOOK_AMOUNT_ + topicId, bookAmountByThemeIds);
            redisService.set(RedisKeyConstant.TOPIC_COMMENT_AMOUNT_ + topicId, commentAmountByThemeIds);


            redisService.set(RedisKeyConstant.TOPIC_TOTAL_VIEW_AMOUNT_ + topicId, commentAmountByThemeIds + themeIds.size());
            redisService.set(RedisKeyConstant.TOPIC_THEME_AMOUNT_ + topicId, commentAmountByThemeIds + themeIds.size());
            redisService.set(RedisKeyConstant.TOPIC_DISCUSS_AMOUNT_ + topicId,  commentAmountByThemeIds + themeIds.size());
            redisService.set(RedisKeyConstant.TOPIC_POST_USER_AMOUNT_ + topicId, (long) postUsers.size());
            redisService.set(RedisKeyConstant.TOPIC_COMMENT_USER_AMOUNT_ + topicId, (long) commentUsers.size());
            redisService.set(RedisKeyConstant.TOPIC_TOTAL_USER_AMOUNT_ + topicId, (long) totalUsers.size());


        }

    }

    //后台管理:返回数据分析
    public TopicDataAnalysDTO queryDataAnalysis(String topicId) {
        TopicDataAnalysDTO topicDataAnalysDTO = new TopicDataAnalysDTO();
        TopicEntity topicEntity = topicService.queryById(topicId);
        if (topicEntity == null) {
            throw new BizException("话题未找到,id:" + topicId);
        }
        this.refreshRedisCache();
        topicDataAnalysDTO.setId(topicId);
        topicDataAnalysDTO.setTopicTitle(topicEntity.getTopicTitle());
        topicDataAnalysDTO.setCommentAmount(redisService.getInteger(RedisKeyConstant.TOPIC_COMMENT_AMOUNT_ + topicId));
        topicDataAnalysDTO.setPosterAmount(redisService.getInteger(RedisKeyConstant.TOPIC_POST_USER_AMOUNT_ + topicId));
        topicDataAnalysDTO.setReplIierAmount(redisService.getInteger(RedisKeyConstant.TOPIC_COMMENT_USER_AMOUNT_ + topicId));
        topicDataAnalysDTO.setThemeAmount(redisService.getInteger(RedisKeyConstant.TOPIC_THEME_AMOUNT_ + topicId));
        topicDataAnalysDTO.setUserTotalAmount(redisService.getInteger(RedisKeyConstant.TOPIC_TOTAL_USER_AMOUNT_ + topicId));
        topicDataAnalysDTO.setViewTotalAmount(redisService.getInteger(RedisKeyConstant.TOPIC_TOTAL_VIEW_AMOUNT_ + topicId));
        topicDataAnalysDTO.setViewPageAmount(redisService.getInteger(RedisKeyConstant.TOPIC_PAGE_VIEW_AMOUNT_ + topicId));
        return topicDataAnalysDTO;
    }
}