package com.atlassian.stash.internal.comment.like.notification;

import com.atlassian.event.api.AsynchronousPreferred;
import com.atlassian.bitbucket.comment.Comment;
import com.atlassian.bitbucket.event.annotation.TransactionAware;
import com.atlassian.bitbucket.notification.custom.pull.CustomPullRequestNotificationEvent;
import com.atlassian.bitbucket.notification.custom.pull.CustomNotificationData;
import com.atlassian.bitbucket.pull.PullRequest;
import com.atlassian.bitbucket.user.ApplicationUser;
import com.google.common.collect.ImmutableMap;

import javax.annotation.Nonnull;

import java.util.Date;

/**
 * Custom notification event to signal a user has liked a comment on a pull request.
 *
 * @see CustomPullRequestNotificationEvent
 */
@AsynchronousPreferred
@TransactionAware
public class CommentLikePullRequestNotificationEvent extends CustomPullRequestNotificationEvent {

    static final String COMMENT_KEY = "comment";
    static final String DATE_KEY = "date";
    static final String LIKER_KEY = "liker";

    public CommentLikePullRequestNotificationEvent(@Nonnull Object source, @Nonnull Iterable<ApplicationUser> recipients,
                                                   @Nonnull PullRequest pullRequest, @Nonnull Comment comment,
                                                   @Nonnull ApplicationUser liker, @Nonnull Date date) {
        super(source, CommentLikePullRequestNotificationRenderer.ID, pullRequest, recipients,
                asNotificationData(comment, liker, date));
    }

    private static CustomNotificationData asNotificationData(Comment comment, ApplicationUser liker, Date date) {
        return new CustomNotificationData(ImmutableMap.<String, Object>of(
                COMMENT_KEY, comment.getId(),
                DATE_KEY, date.getTime(),
                LIKER_KEY, liker.getId()));
    }

}
