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.letterbox.entity.ForumLetter;
import com.apobates.forum.letterbox.entity.ForumLetterReceiver;
import com.apobates.forum.letterbox.service.ForumLetterService;
import com.apobates.forum.member.entity.MemberStatusEnum;
import com.apobates.forum.member.impl.event.MemberPenalizeEvent;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * 会员惩罚通知
 *
 * @author xiaofanku
 * @since 20190804
 */
@Component("penalizeNotice")
public class MemberPenalizeNoticeListener implements MessageListener<MemberPenalizeEvent> {
    @Autowired
    private ForumLetterService forumLetterService;
    private final static Logger logger = LoggerFactory.getLogger(MemberPenalizeNoticeListener.class);
    /*
    *
6{
MemberChangeListener[JMS]
MemberRole[Status|Role]ChangeListener.java +3
MemberPenalizeNoticeListener[x]
MemberVipExchangeNoticeListener[x]
ModeratorRecall[Born]NoticeListener +2
}
14(JMS && ActiveMQ)
    * */
    @Override
    public void onMessage(Message<MemberPenalizeEvent> event) throws RetryableMessageException {
        logger.info("[Member][PenalizeEvent][2]惩罚记录通知开始");
        MemberPenalizeEvent mpe = event.getPayload();
        forumLetterService.create(
                getPenalizeNotic(mpe.getReason(), mpe.getArrive(), mpe.getDuration()),
                List.of(new ForumLetterReceiver(mpe.getMember(), mpe.getNames())));
        logger.info("[Member][PenalizeEvent][2]惩罚记录通知结束");
    }
    
    private ForumLetter getPenalizeNotic(String reason, MemberStatusEnum arrive, String duration) {
        return ForumLetter.noticeLetter(
                "会员惩罚通知",
                String.format("理由: <blockquote>%s</blockquote> 惩罚状态: %s, 惩罚时长: %s, 如果存在异议您可以联系在线管理员", reason, arrive.getTitle(), duration));
    }
}