package com.apobates.forum.thrones.event;

import com.github.davidmarquis.redisq.Message;
import com.github.davidmarquis.redisq.consumer.MessageListener;
import com.github.davidmarquis.redisq.consumer.retry.RetryableMessageException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.apobates.forum.core.entity.Topic;
import com.apobates.forum.core.impl.event.TopicMoveEvent;
import com.apobates.forum.letterbox.entity.ForumLetter;
import com.apobates.forum.letterbox.entity.ForumLetterReceiver;
import com.apobates.forum.letterbox.service.ForumLetterService;
import com.apobates.forum.utils.DateTimeUtils;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * 话题移动通知侦听器
 *
 * @author xiaofanku
 * @since 20191117
 */
@Component("topicMoveActionNotice")
public class TopicMoveNoticeListener implements MessageListener<TopicMoveEvent> {
    @Autowired
    private ForumLetterService forumLetterService;
    private final static Logger logger = LoggerFactory.getLogger(PostsMoodNoticeListener.class);
    
    @Override
    public void onMessage(Message<TopicMoveEvent> event) throws RetryableMessageException {
        logger.info("[Topic][MoveEvent][1]话题移动通知处理开始");
        Topic topic = event.getPayload().getTopic();
        String content = String.format("%s在%s将话题: #%s# ,从%s版块移至%s版块, 如果存在异议您可以联系在线管理员",
                event.getPayload().getManagerNickname(),
                DateTimeUtils.getRFC3339(event.getPayload().getDateTime()),
                topic.getTitle(),
                event.getPayload().getProtoBoard().getTitle(),
                event.getPayload().getTargetBoard().getTitle());
        ForumLetter flr = ForumLetter.noticeLetter("您发布的话题已经被管理员移至新版块", content);
        forumLetterService.create(
                flr,
                List.of(new ForumLetterReceiver(topic.getMemberId(), topic.getMemberNickname())));
        logger.info("[Topic][MoveEvent][1]话题移动通知处理结束");
    }
}