package com.atlassian.bitbucket.plugin.util;

import com.atlassian.event.api.EventPublisher;
import com.atlassian.event.config.ListenerHandlersConfiguration;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.Ordered;

/**
 * Utility class that inspects all beans in the Spring {@link org.springframework.beans.factory.BeanFactory BeanFactory}
 * and registers the beans that have {@link @com.atlassian.event.api.EventListener EventListener} or
 * {@code com.atlassian.plugin.event.PluginEventListener PluginEventListener} annotations with the
 * {@link EventPublisher}.
 * <p>
 * To use this class, simply create an instance in the Spring application context.
 *
 * @deprecated since 4.7 for removal in 5.0. Plugins no longer need to register this class in their plugin - the
 *             host application takes care of this automatically
 */
// Note: This component is used inside the plugin framework, so it cannot use Spring 4.2 and the new @EventListener
//       annotation; it still has to use ApplicationListener.
@Deprecated
public class EventListenerBeanRegistrar
        implements ApplicationListener<ContextRefreshedEvent>, DestructionAwareBeanPostProcessor, Ordered {

    @SuppressWarnings("UnusedParameters")
    public EventListenerBeanRegistrar(EventPublisher eventPublisher, ListenerHandlersConfiguration configuration) {
    }

    @Override
    public int getOrder() {
        return 1;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // noop
    }

    @Override
    public void postProcessBeforeDestruction(Object object, String beanName) {
        // noop
    }

    @Override
    public Object postProcessBeforeInitialization(Object object, String beanName) {
        return object;
    }

    @Override
    public Object postProcessAfterInitialization(Object object, String beanName) {
        return object;
    }
}
