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

import cn.axzo.framework.rocketmq.Event;
import cn.axzo.framework.rocketmq.EventConsumer;
import cn.axzo.framework.rocketmq.EventHandler;
import cn.axzo.msg.center.api.mq.CardPresetButtonPressedMessage;
import cn.axzo.msg.center.service.enums.MqMessageType;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

/**
 * @author yanglin
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class CardSyncTodoHandler implements EventHandler, InitializingBean {

    private final EventConsumer eventConsumer;
    private final TodoSyncCardService todoSyncCardService;

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

    @Override
    public void afterPropertiesSet() {
        eventConsumer.registerHandler(MqMessageType.CARD_PRESET_BUTTON_PRESSED.getEventCode(), this);
    }

}