package it.com.atlassian.stash.pull.list;

import com.atlassian.bitbucket.pull.PullRequestParticipantStatus;
import com.atlassian.bitbucket.test.PullRequestTestHelper;
import com.atlassian.webdriver.bitbucket.element.Participant;
import com.atlassian.webdriver.bitbucket.element.PullRequestTable;
import com.atlassian.webdriver.bitbucket.page.PullRequestListPage;
import com.google.common.collect.ImmutableMap;
import org.junit.Test;

import java.util.List;

import static com.atlassian.bitbucket.pull.PullRequestState.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;

public class PullRequestListTest extends BasePullRequestListFuncTest {

    @Test
    public void testCreatePullRequestActionButton() {
        createPullRequest(new PullRequestBuilder(BRANCH_THAT_HAS_SAME_FILE_MODIFIED_AND_MOVED_SRC,
                BRANCH_THAT_HAS_SAME_FILE_MODIFIED_AND_MOVED_TRGT).build());

        PullRequestListPage listPage = loginAs(AUTHOR_1, AUTHOR_1, PullRequestListPage.class, PROJECT_KEY, REPO_SLUG);

        assertThat(listPage.isCreateActionButtonVisible(), is(true));

        // Internally this will bind to PullRequestCreatePage, which should fail if the button does not work.
        listPage.clickCreateActionButton();
    }

    @Test
    public void testListNoResultsMessage() {
        createPullRequest(new PullRequestBuilder(BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_SRC, MASTER)
                .reviewers(REVIEWER_1, REVIEWER_2, REVIEWER_3)
                .build(), DECLINED);

        PullRequestListPage listPage = loginAs(AUTHOR_1, AUTHOR_1,
                PullRequestListPage.class, PROJECT_KEY, REPO_SLUG, ImmutableMap.builder()
                        .put("state", MERGED.name())
                        .build());

        assertThat(listPage.isNoResultsMessageVisible(), is(true));
    }

    @Test
    public void testReviewersAreOrderedByApprovalThenDisplayName() {
        PullRequestTestHelper pullRequest = createPullRequest(new PullRequestBuilder(
                BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_SRC, MASTER)
                .reviewers(REVIEWER_1, REVIEWER_2, REVIEWER_3)
                .build());

        pullRequest.setStatusFor(REVIEWER_2, PullRequestParticipantStatus.APPROVED);

        PullRequestListPage listPage = loginAs(AUTHOR_1, AUTHOR_1, PullRequestListPage.class, PROJECT_KEY, REPO_SLUG);

        List<PullRequestTable.Entry> rows = listPage.getPullRequestTable().getRows();
        assertThat(rows, hasSize(1));

        List<Participant> reviewers = rows.get(0).getReviewers();
        assertThat(reviewers, hasSize(3));

        // TODO: add NEEDS_WORK
        // Approved first, then by name
        assertThat(reviewers.get(0).getUsername(), is(REVIEWER_2));
        assertThat(reviewers.get(0).isApproved(), is(true));
        assertThat(reviewers.get(1).getUsername(), is(REVIEWER_1));
        assertThat(reviewers.get(1).isApproved(), is(false));
        assertThat(reviewers.get(2).getUsername(), is(REVIEWER_3));
        assertThat(reviewers.get(2).isApproved(), is(false));
    }
}
