package com.webchat.service.queue;

import com.webchat.common.constants.MessageConstants;
import com.webchat.common.enums.RedisKeyEnum;
import com.webchat.common.util.JsonUtil;
import com.webchat.domain.vo.dto.BaseMessageDTO;
import com.webchat.domain.vo.dto.CommentReplyMessageDTO;
import com.webchat.domain.vo.dto.LikeCollectMessageDTO;
import com.webchat.domain.vo.dto.SystemMessageDTO;
import com.webchat.service.message.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @Author 程序员七七
 * @webSite https://www.coderutil.com
 * @Date 2022/11/13 03:57
 * @description
 * 消息中心消息队列
 * 设置@Lazy为false，因为server这边默认bean懒加载，很有可能一开始就执行不了初始化方法
 */
@Component
@Lazy(value = false)
@Slf4j
public class UserMessageQueue extends AbstractRedisQueue<BaseMessageDTO> {

    @Autowired
    private MessageService messageService;

    @PostConstruct
    public void initBean() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                UserMessageQueue.this.schedule();
            }
        }).start();
    }

    @Override
    protected int getPoolSize() {
        return 3;
    }

    /**
     * 从消息队列redis读取数据的超时时间
     * @return
     */
    @Override
    protected long getTimeout() {
        return 10000;
    }

    /**
     * 一个任务元素重试的次数
     * @return 默认不重试
     */
    @Override
    protected int getRetryTimes() {
        return 1;
    }

    /**
     * 一个任务最多被重返队列的次数
     * @return
     */
    @Override
    protected int getBackQueueTimes() {
        return 0;
    }

    @Override
    protected BaseMessageDTO convert(String s) {
        BaseMessageDTO baseMessage = JsonUtil.fromJson(s, BaseMessageDTO.class);
        if (MessageConstants.CategoryEnum.SYSTEM.getCategory().equals(baseMessage.getCategory())) {
            return JsonUtil.fromJson(s, SystemMessageDTO.class);
        } else if (MessageConstants.CategoryEnum.LIKE_COLLECT.getCategory().equals(baseMessage.getCategory())) {
            return JsonUtil.fromJson(s, LikeCollectMessageDTO.class);
        } else if (MessageConstants.CategoryEnum.COMMENT_REPLY.getCategory().equals(baseMessage.getCategory())) {
            return JsonUtil.fromJson(s, CommentReplyMessageDTO.class);
        }
        return null;
    }

    @Override
    protected String getQueueName() {
        return RedisKeyEnum.QUEUE_WEB_USER_MESSAGE.getKey();
    }

    @Override
    protected void receive(BaseMessageDTO data) {
        if (data == null) {
            return;
        }
        if (data.getToUserId().equals(data.getFromUserId())) {
            return;
        }
        log.info("【用户消息队列】开始处理消息, taskId:{}, message:{}", data.getTaskId(), data);
        messageService.send(data);
        log.info("【用户消息队列】消息处理成功. taskId:{}", data.getTaskId());
    }

    @Override
    protected void error(BaseMessageDTO data, Exception ex) {
        log.error("【用户消息队列】消费出错 taskId:{}, ex:{}", data.getTaskId(), ex);
    }
}
