package com.apobates.forum.thrones.event;

import com.apobates.forum.core.entity.Board;
import com.apobates.forum.core.entity.BoardGroup;
import com.apobates.forum.core.entity.BoardModeratorRoleHistory;
import com.apobates.forum.core.entity.proxy.BoardGroupReplica;
import com.apobates.forum.core.entity.proxy.BoardReplica;
import com.apobates.forum.core.impl.event.ModeratorRecallEvent;
import com.apobates.forum.core.service.BoardGroupService;
import com.apobates.forum.core.service.BoardService;
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.Member;
import com.apobates.forum.member.entity.MemberRoleEnum;
import com.apobates.forum.member.service.MemberService;
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 org.springframework.stereotype.Component;
import java.util.List;
import java.util.Optional;

@Component("recallNotice")
public class ModeratorRecallNoticeListener implements MessageListener<ModeratorRecallEvent> {
    @Autowired
    private BoardService boardService;
    @Autowired
    private BoardGroupService boardGroupService;
    @Autowired
    private MemberService memberSerivce;
    @Autowired
    private ForumLetterService forumLetterService;
    private final static Logger logger = LoggerFactory.getLogger(ModeratorRecallNoticeListener.class);

    @Override
    public void onMessage(Message<ModeratorRecallEvent> message) throws RetryableMessageException {
        logger.info("[Moderator][RecallEvent][2]版主卸任通知开始发送");
        final BoardModeratorRoleHistory mrh = message.getPayload().getRemoveRoleHistory();
        final MemberRoleEnum currentRole = message.getPayload().getUpdateRole();
        Optional<Member> member = memberSerivce.get(mrh.getMemberId());
        member.ifPresent(m -> {
            forumLetterService.create(
                    getModeratorRecallNotice(boardGroupService.get(mrh.getVolumesId()), boardService.get(mrh.getBoardId()), currentRole),
                    List.of(new ForumLetterReceiver(m.getId(), m.getNickname())));
        });
        logger.info("[Moderator][RecallEvent][2]版主卸任通知发送结束");
    }
    //生成卸任通知
    private ForumLetter getModeratorRecallNotice(Optional<BoardGroup> boardGroup, Optional<Board> board, MemberRoleEnum currentRole) {
        String section = "";
        if(MemberRoleEnum.MASTER == currentRole && boardGroup.isPresent()){
            BoardGroupReplica bgr = BoardGroupReplica.copyBoardGroup.apply(boardGroup.get());
            section=String.format("<a class=\"embed-link\" href=\"/board/volumes/%s.xhtml\">%s</a>的大", bgr.getConnect(), bgr.getTitle());
        }

        if(MemberRoleEnum.BM == currentRole && boardGroup.isPresent() && board.isPresent()){
            BoardGroupReplica bgr = BoardGroupReplica.copyBoardGroup.apply(boardGroup.get());
            BoardReplica br = BoardReplica.copyBoard.apply(board.get());
            section=String.format("<a class=\"embed-link\" href=\"/board/volumes/%s.xhtml\">%s</a> &#187; <a class=\"embed-link\" href=\"/board/%s.xhtml\">%s</a>的",
                    bgr.getConnect(), bgr.getTitle(),
                    br.getConnect(), br.getTitle());
        }
        String content = String.format("您现在已经辞去%s版主! 感谢您对社区孜孜不倦的工作付出, 您当前的会员角色: %s", section, currentRole.getTitle());
        return ForumLetter.noticeLetter("版主卸任通知", content);
    }
}
