package com.atlassian.bitbucket.rest.pull;

import com.atlassian.bitbucket.rest.comment.RestComment;
import com.atlassian.bitbucket.rest.comment.RestDiffCommentAnchor;
import com.atlassian.bitbucket.rest.content.RestDiff;
import com.atlassian.bitbucket.comment.CommentAction;
import com.atlassian.bitbucket.pull.PullRequestAction;
import com.atlassian.bitbucket.pull.PullRequestCommentActivity;
import com.atlassian.bitbucket.rest.annotation.JsonSurrogate;
import com.atlassian.bitbucket.rest.user.RestApplicationUser;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.util.Date;

import static com.atlassian.bitbucket.rest.util.RestUtils.applyOrNull;

@JsonSerialize
@JsonSurrogate(PullRequestCommentActivity.class)
public class RestPullRequestCommentActivity extends RestPullRequestActivity {

    public RestPullRequestCommentActivity(PullRequestCommentActivity activity) {
        this(activity.getId(), activity.getCreatedDate(), new RestApplicationUser(activity.getUser()), activity.getAction(),
                activity.getCommentAction(), new RestComment(activity.getComment()),
                applyOrNull(activity.getCommentAnchor(), RestPullRequestDiffCommentAnchor.REST_TRANSFORM),
                applyOrNull(activity.getDiff(), RestDiff.REST_TRANSFORM));
    }

    // Required for REST documentation
    private RestPullRequestCommentActivity(long id, Date createdDate, RestApplicationUser restStashUser,
                                           PullRequestAction action, CommentAction commentAction,
                                           RestComment restComment, RestDiffCommentAnchor restDiffCommentAnchor,
                                           RestDiff restDiff) {
        super(id, createdDate, restStashUser, action);

        put("commentAction", commentAction);
        put("comment", restComment);
        putIfNotNull("commentAnchor", restDiffCommentAnchor);
        putIfNotNull("diff", restDiff);
    }

    // REST Documentation Examples

    public static final RestPullRequestCommentActivity RESPONSE_EXAMPLE = new RestPullRequestCommentActivity(101L,
            new Date(1359065920), RestApplicationUser.RESPONSE_EXAMPLE, PullRequestAction.COMMENTED, CommentAction.ADDED,
            RestComment.RESPONSE_EXAMPLE, RestPullRequestDiffCommentAnchor.LINE_COMMENT_RESPONSE, null);
}
