package com.atlassian.stash.internal.pull.comment;

import com.atlassian.bitbucket.comment.pull.AddPullRequestFileCommentRequest;
import com.atlassian.bitbucket.event.TestEventPublisher;
import com.atlassian.bitbucket.event.pull.PullRequestCommentActivityEvent;
import com.atlassian.bitbucket.event.pull.PullRequestCommentAddedEvent;
import com.atlassian.bitbucket.mockito.MockitoUtils;
import com.atlassian.bitbucket.pull.PullRequestAction;
import com.atlassian.bitbucket.scm.ScmService;
import com.atlassian.bitbucket.scm.pull.PullRequestEffectiveDiff;
import com.atlassian.bitbucket.user.SimpleAuthenticationContext;
import com.atlassian.stash.internal.comment.CommentDao;
import com.atlassian.stash.internal.comment.InternalComment;
import com.atlassian.stash.internal.pull.InternalPullRequest;
import com.atlassian.stash.internal.pull.InternalPullRequestActivity;
import com.atlassian.stash.internal.pull.PullRequestActivityDao;
import com.atlassian.stash.internal.pull.PullRequestCommentAnchorDao;
import com.atlassian.stash.internal.user.InternalNormalUser;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

import javax.validation.Validator;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class DefaultPullRequestCommentHelperTest {

    @Mock
    private CommentDao commentDao;

    @Mock
    private PullRequestActivityDao activityDao;

    @Mock
    private PullRequestCommentAnchorDao anchorDao;

    @Mock
    private CommentPostProcessor postProcessor;

    @Mock(answer= Answers.RETURNS_DEEP_STUBS)
    private ScmService scmService;

    @Mock
    private Validator validator;

    @Spy
    private TestEventPublisher eventPublisher = new TestEventPublisher();
    @Spy
    private SimpleAuthenticationContext authenticationContext = new SimpleAuthenticationContext();

    @InjectMocks
    private DefaultPullRequestCommentHelper commentHelper;

    @Before
    public void setup() {
        when(commentDao.create(any(InternalComment.class))).thenAnswer(MockitoUtils.returnFirst());
        when(activityDao.create(any(InternalPullRequestActivity.class))).thenAnswer(MockitoUtils.returnFirst());
        authenticationContext.setCurrentUser(new InternalNormalUser.Builder().name("commenter").build());
    }

    @Test
    public void testCreateComment() {
        InternalPullRequest pullRequest = new InternalPullRequest.Builder().build();
        commentHelper.create(pullRequest, "Comment text");

        List<Object> events = eventPublisher.getPublishedEvents();
        assertThat(events, hasSize(2));

        PullRequestCommentAddedEvent commentAddedEvent = (PullRequestCommentAddedEvent) events.get(0);
        assertThat(commentAddedEvent.getComment().getText(), is("Comment text"));

        PullRequestCommentActivityEvent commentActivityEvent = (PullRequestCommentActivityEvent) events.get(1);
        assertThat(commentActivityEvent.getActivity().getAction(), is(PullRequestAction.COMMENTED));
        assertThat(commentActivityEvent.getActivity().getComment().getText(), is("Comment text"));
    }

    @Test
    public void testCommentWithLeadingSpacesDoesNotGetTrimmed() {
        InternalPullRequest pullRequest = new InternalPullRequest.Builder().build();
        PullRequestEffectiveDiff effectiveDiff = mock(PullRequestEffectiveDiff.class);
        when(effectiveDiff.getUntilId()).thenReturn("untilId");
        when(effectiveDiff.getSinceId()).thenReturn("sinceId");
        when(scmService.getPullRequestCommandFactory(pullRequest).effectiveDiff().call()).thenReturn(effectiveDiff);
        String commentText = "    Comment text";
        AddPullRequestFileCommentRequest commentRequest = new AddPullRequestFileCommentRequest.Builder(pullRequest)
                .path("path")
                .text(commentText)
                .build();

        commentHelper.createFileComment(commentRequest);

        List<Object> events = eventPublisher.getPublishedEvents();
        assertThat(events, hasSize(2));

        PullRequestCommentAddedEvent commentAddedEvent = (PullRequestCommentAddedEvent) events.get(0);
        assertThat(commentAddedEvent.getComment().getText(), is(commentText));

        PullRequestCommentActivityEvent commentActivityEvent = (PullRequestCommentActivityEvent) events.get(1);
        assertThat(commentActivityEvent.getActivity().getAction(), is(PullRequestAction.COMMENTED));
        assertThat(commentActivityEvent.getActivity().getComment().getText(), is(commentText));
    }
}
