package com.atlassian.stash.internal.johnson;

import com.atlassian.bitbucket.i18n.I18nService;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.johnson.event.AddEvent;
import com.atlassian.johnson.event.Event;
import com.atlassian.johnson.event.EventLevel;
import com.atlassian.johnson.event.EventType;
import com.atlassian.plugin.Plugin;
import com.atlassian.plugin.PluginState;

public class BaseJohnsoningPluginEventListener {
    protected final EventPublisher eventPublisher;
    protected final I18nService i18nService;

    public BaseJohnsoningPluginEventListener(EventPublisher eventPublisher, I18nService i18nService) {
        this.eventPublisher = eventPublisher;
        this.i18nService = i18nService;
    }

    protected void enabledOrRiot(String key, Plugin plugin) {
        if (plugin == null) {
            Event event = new Event(EventType.get("plugin-failed"),
                    i18nService.getMessage("bitbucket.web.plugin.failed.install", key),
                    EventLevel.get(EventLevel.ERROR));
            eventPublisher.publish(new AddEvent(this, event));
        } else if (plugin.getPluginState() != PluginState.ENABLED) {
            Event event = new Event(EventType.get("plugin-failed"),
                    i18nService.getMessage("bitbucket.web.plugin.failed.enable", key),
                    EventLevel.get(EventLevel.ERROR));
            eventPublisher.publish(new AddEvent(this, event));
        }
    }
}
