package cn.axzo.msg.center.message.service.todo.card;

import cn.axzo.framework.rocketmq.Event;
import cn.axzo.framework.rocketmq.EventConsumer;
import cn.axzo.msg.center.api.mq.TodoUpdateMessage;
import cn.axzo.msg.center.mq.ConsumerIsolation;
import cn.axzo.msg.center.mq.IsolationMQListener;
import cn.axzo.msg.center.mq.RocketMQConfig;
import cn.axzo.msg.center.service.enums.BizCategoryEnum;
import cn.axzo.msg.center.service.enums.MqMessageType;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.springframework.stereotype.Component;

/**
 * @author yanglin
 */
@Slf4j
@Component
@RocketMQMessageListener(
        consumeThreadMax = 2,
        maxReconsumeTimes = 3,
        consumeMode = ConsumeMode.ORDERLY,
        nameServer = "${rocketmq.name-server}",
        topic = RocketMQConfig.MSG_CENTER_TOPIC,
        consumerGroup = "GID_topic_todo_sync_card_biz_${spring.profiles.active}"
)
class TodoSyncCardBizListener extends IsolationMQListener {

    private final TodoSyncCardService todoSyncCardService;

    TodoSyncCardBizListener(TodoSyncCardService todoSyncCardService) {
        super(ConsumerIsolation.TODO_SYNC_CARD_BIZ, MqMessageType.TODO_STATE_UPDATE);
        this.todoSyncCardService = todoSyncCardService;
    }

    @Override
    public void onEventImpl(Event event, EventConsumer.Context context) {
        TodoUpdateMessage message;
        try {
            message = event.normalizedData(TodoUpdateMessage.class);
        } catch (Exception e) {
            log.warn("parse message error. event={}", event, e);
            return;
        }
        if (message.getUpdatedTodo().getBizCategory() == BizCategoryEnum.OTHER)
            todoSyncCardService.onMessage(event, message);
    }

}