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

import com.atlassian.bitbucket.test.PullRequestTestHelper;
import com.atlassian.webdriver.bitbucket.element.PullRequestTable;
import com.atlassian.webdriver.bitbucket.page.PullRequestListPage;
import org.junit.Test;

import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

public class PullRequestListFilterTest extends BasePullRequestListFuncTest {

    @Test
    public void testListPullRequestsByAuthor() {
        PullRequestTestHelper expectedPullRequest = createPullRequest(new PullRequestBuilder(
                BRANCH_THAT_HAS_SAME_TEXT_FILE_MODIFIED_SRC, BRANCH_THAT_HAS_SAME_TEXT_FILE_MODIFIED_TRGT).build());

        createPullRequest(new PullRequestBuilder(BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_SRC,
                BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_TRGT)
                .author(AUTHOR_2)
                .build());

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

        assertThat("There should be two pull requests in the list", listPage.getPullRequestTable().getRows().size(),
                equalTo(2));

        listPage.filterByAuthor(AUTHOR_1, AUTHOR_1_DISPLAY_NAME);

        List<PullRequestTable.Entry> pullRequests = listPage.getPullRequestTable().getRows();
        assertThat("There should be one pull request in the list", listPage.getPullRequestTable().getRows().size(),
                equalTo(1));

        assertThat("The pull request should have author " + AUTHOR_1, pullRequests.get(0).getId(),
                equalTo(expectedPullRequest.getId()));
    }

    @Test
    public void testListPullRequestsByAuthorIsSelf() {
        PullRequestTestHelper expectedPullRequest = createPullRequest(new PullRequestBuilder(
                BRANCH_THAT_HAS_SAME_TEXT_FILE_MODIFIED_SRC, BRANCH_THAT_HAS_SAME_TEXT_FILE_MODIFIED_TRGT).build());

        createPullRequest(new PullRequestBuilder(BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_SRC,
                BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_TRGT)
                .author(AUTHOR_2)
                .build());

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

        assertThat("There should be two pull requests in the list", listPage.getPullRequestTable().getRows().size(),
                equalTo(2));

        listPage.filterByAuthor(AUTHOR_1, AUTHOR_1_DISPLAY_NAME);

        List<PullRequestTable.Entry> pullRequests = listPage.getPullRequestTable().getRows();
        assertThat("There should be one pull request in the list", listPage.getPullRequestTable().getRows().size(),
                equalTo(1));

        assertThat("The pull request should have author " + AUTHOR_1, pullRequests.get(0).getId(),
                equalTo(expectedPullRequest.getId()));
    }

    @Test
    public void testListPullRequestsByMultipleFilters() {
        PullRequestTestHelper expectedPullRequest = createPullRequest(new PullRequestBuilder(
                BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_SRC, BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_TRGT).build());

        createPullRequest(new PullRequestBuilder(BRANCH_THAT_HAS_SAME_TEXT_FILE_MODIFIED_SRC,
                BRANCH_THAT_HAS_SAME_TEXT_FILE_MODIFIED_TRGT)
                .author(AUTHOR_2)
                .build());

        createPullRequest(new PullRequestBuilder(BRANCH_WITH_AUTOMERGE_CONFLICT_SRC,
                BRANCH_WITH_AUTOMERGE_CONFLICT_TRGT)
                .author(AUTHOR_1)
                .build());

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

        assertThat("There should be three pull requests in the list", listPage.getPullRequestTable().getRows().size(),
                equalTo(3));

        listPage.filterByAuthor(AUTHOR_1, AUTHOR_1_DISPLAY_NAME);
        listPage.filterByTargetBranch(BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_TRGT);

        List<PullRequestTable.Entry> pullRequests = listPage.getPullRequestTable().getRows();
        assertThat("There should be one pull request in the list", listPage.getPullRequestTable().getRows().size(),
                equalTo(1));

        assertThat("The pull request should have author " + AUTHOR_1 + " and target branch " +
                BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_TRGT, pullRequests.get(0).getId(),
                equalTo(expectedPullRequest.getId()));
    }

    @Test
    public void testListPullRequestsByReviewerIsSelf() {
        PullRequestTestHelper expectedPullRequest = createPullRequest(new PullRequestBuilder(
                BRANCH_THAT_HAS_SAME_TEXT_FILE_MODIFIED_SRC, BRANCH_THAT_HAS_SAME_TEXT_FILE_MODIFIED_TRGT)
                .reviewer(REVIEWER_1)
                .build());

        createPullRequest(new PullRequestBuilder(BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_SRC,
                BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_TRGT)
                .reviewer(REVIEWER_2)
                .build());

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

        assertThat("There should be two pull requests in the list", listPage.getPullRequestTable().getRows().size(),
                equalTo(2));

        listPage.filterByReviewerToggle();

        List<PullRequestTable.Entry> pullRequests = listPage.getPullRequestTable().getRows();
        assertThat("There should be one pull request in the list", listPage.getPullRequestTable().getRows().size(),
                equalTo(1));

        assertThat("The pull request should have reviewer as current user " + REVIEWER_1,
                pullRequests.get(0).getId(), equalTo(expectedPullRequest.getId()));
    }

    @Test
    public void testListPullRequestsByTargetBranch() {
        PullRequestTestHelper expectedPullRequest = createPullRequest(new PullRequestBuilder(
                BRANCH_THAT_HAS_SAME_TEXT_FILE_MODIFIED_SRC, BRANCH_THAT_HAS_SAME_TEXT_FILE_MODIFIED_TRGT).build());

        createPullRequest(new PullRequestBuilder(BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_SRC,
                BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_TRGT).build());

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

        assertThat("There should be two pull requests in the list", listPage.getPullRequestTable().getRows().size(),
                equalTo(2));

        listPage.filterByTargetBranch(BRANCH_THAT_HAS_SAME_TEXT_FILE_MODIFIED_TRGT);

        List<PullRequestTable.Entry> pullRequests = listPage.getPullRequestTable().getRows();
        assertThat("There should be one pull request in the list", listPage.getPullRequestTable().getRows().size(),
                equalTo(1));

        assertThat("The pull request should have target branch " + BRANCH_THAT_HAS_SAME_TEXT_FILE_MODIFIED_TRGT,
                pullRequests.get(0).getId(), equalTo(expectedPullRequest.getId()));
    }
}
