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.TodoUpdateMessage;
import cn.axzo.msg.center.mq.ConsumerIsolation;
import cn.axzo.msg.center.service.enums.BizCategoryEnum;
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
class TodoSyncCardBizHandler implements EventHandler, InitializingBean {

    private final EventConsumer eventConsumer;
    private final TodoSyncCardService todoSyncCardService;

    @Override
    public void onEvent(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 (ConsumerIsolation.getIsolation() == ConsumerIsolation.TODO_SYNC_CARD_BIZ
                && message.getUpdatedTodo().getBizCategory() == BizCategoryEnum.OTHER) {
            todoSyncCardService.onMessage(event, message);
        }
    }

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

}