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.PresetButtonPressedMessage;
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.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_preset_button_sync_card_${spring.profiles.active}"
)
public class TodoPresetButtonSyncCardListener extends IsolationMQListener {

    private final TodoSyncCardService todoSyncCardService;

    TodoPresetButtonSyncCardListener(TodoSyncCardService todoSyncCardService) {
        super(ConsumerIsolation.TODO_PRESET_BUTTON_CLICKED_SYNC_CARD,
                MqMessageType.TODO_PRESET_BUTTON_PRESSED);
        this.todoSyncCardService = todoSyncCardService;
    }

    @Override
    public void onEventImpl(Event event, EventConsumer.Context context) {
        PresetButtonPressedMessage message = event.normalizedData(PresetButtonPressedMessage.class);
        log.info("received PresetButtonPressedMessage: {}", message);
        todoSyncCardService.syncTodoPresetButtonPressed(message);
    }

}