package com.atlassian.stash.internal.johnson;

import com.atlassian.bitbucket.i18n.I18nService;
import com.atlassian.event.api.EventListener;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.plugin.event.events.PluginFrameworkStartedEvent;
import com.atlassian.stash.internal.mode.MirrorApplicationMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Tracks whether the mirroring plugin failed to load when in mirroring mode and if so
 * Johnsons the application. This can't be performed in the {@link JohnsonPluginEventListener}
 * because required plugins can't currently be varied depending on the application mode.
 */
@Component
@MirrorApplicationMode
public class MirroringPluginFailureListener extends BaseJohnsoningPluginEventListener {
    private static final String PLUGIN_KEY_MIRRORING_MIRROR = "com.atlassian.bitbucket.server.bitbucket-mirroring-mirror";

    @Autowired
    public MirroringPluginFailureListener(EventPublisher eventPublisher, I18nService i18nService) {
        super(eventPublisher, i18nService);
    }

    @EventListener
    public void onPluginFrameworkStarted(PluginFrameworkStartedEvent event) {
        enabledOrRiot(PLUGIN_KEY_MIRRORING_MIRROR, event.getPluginAccessor().getPlugin(PLUGIN_KEY_MIRRORING_MIRROR));
    }
}
