package com.atlassian.stash.internal.notification.custom.pull;

import com.atlassian.plugin.PluginAccessor;
import com.atlassian.bitbucket.notification.custom.pull.CustomPullRequestNotificationRenderer;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Objects;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Accessor to retrieve a custom notification renderer by its ID.
 *
 * @see CustomPullRequestNotificationRenderer
 */
public class CustomPullRequestNotificationRendererAccessor {

    private final PluginAccessor pluginAccessor;

    public CustomPullRequestNotificationRendererAccessor(@Nonnull PluginAccessor pluginAccessor) {
        this.pluginAccessor = checkNotNull(pluginAccessor, "pluginAccessor");
    }

    @Nullable
    public CustomPullRequestNotificationRenderer getRenderer(String rendererId) {
        for (CustomPullRequestNotificationRenderer renderer : getRenderers()) {
            if (Objects.equals(renderer.getId(), rendererId)) {
                return renderer;
            }
        }
        return null;
    }

    private List<CustomPullRequestNotificationRenderer> getRenderers() {
        return pluginAccessor.getEnabledModulesByClass(CustomPullRequestNotificationRenderer.class);
    }

}
