package com.atlassian.bitbucket.notification.custom.pull;

import com.atlassian.bitbucket.event.ApplicationEvent;
import com.atlassian.bitbucket.pull.PullRequest;
import com.atlassian.bitbucket.user.ApplicationUser;

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

/**
 * Event raised by plugins to trigger a custom notification on a pull request.
 * <p>
 * Note that the {@link #getData() data} provided by the event should be as lightweight as possible
 * because it is persisted as JSON data in the database for any recipient configured to receive batched
 * notifications. For example, use IDs (such as the IDs of the repository and pull request for a pull request)
 * rather than the full object (such as a pull request instance).
 */
public abstract class CustomPullRequestNotificationEvent extends ApplicationEvent {

    private final CustomNotificationData data;
    private final String rendererId;
    private final PullRequest pullRequest;
    private final Iterable<ApplicationUser> recipients;

    /**
     * @param source      the source of the event
     * @param rendererId  the ID of the renderer for this notification; this must correspond to
     *                    {@link CustomPullRequestNotificationRenderer#getId()}.
     * @param pullRequest the pull request for this notification
     * @param recipients  the recipients for this notification
     * @param data        the metadata that will be used to render this notification
     */
    protected CustomPullRequestNotificationEvent(@Nonnull Object source, @Nonnull String rendererId,
                                                 @Nonnull PullRequest pullRequest,
                                                 @Nonnull Iterable<ApplicationUser> recipients,
                                                 @Nonnull CustomNotificationData data) {
        super(source);
        this.data = requireNonNull(data, "data");
        this.rendererId = requireNonNull(rendererId, "rendererId");
        this.pullRequest = requireNonNull(pullRequest, "pullRequest");
        this.recipients = requireNonNull(recipients, "recipients");
    }

    @Nonnull
    public CustomNotificationData getData() {
        return data;
    }

    @Nonnull
    public String getRendererId() {
        return rendererId;
    }

    @Nonnull
    public PullRequest getPullRequest() {
        return pullRequest;
    }

    @Nonnull
    public Iterable<ApplicationUser> getRecipients() {
        return recipients;
    }
}
