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

import com.atlassian.webdriver.bitbucket.page.PullRequestListPage;
import com.google.common.collect.ImmutableMap;
import org.junit.Test;

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

/**
 * The pull request list getting started page should only be shown when there are no pull requests targeting the current
 * repository, not when just the current filter combination produces an empty list.
 */
public class PullRequestListGettingStartedTest extends BasePullRequestListFuncTest {

    @Test
    public void testListGettingStartedPage() {
        PullRequestListPage listPage = loginAs(AUTHOR_1, AUTHOR_1, PullRequestListPage.class, PROJECT_KEY, REPO_SLUG);

        assertThat(listPage.isGettingStartedMessageVisible(), is(true));
        assertThat(listPage.isCreateActionButtonVisible(), is(false));
    }

    @Test
    public void testListGettingStartedPageWithAuthorFilter() {
        createPullRequest(new PullRequestBuilder(BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_SRC,
                BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_TRGT).build());

        PullRequestListPage listPage = loginAs(AUTHOR_1, AUTHOR_1,
                PullRequestListPage.class, PROJECT_KEY, REPO_SLUG, ImmutableMap.builder()
                        .put("author", AUTHOR_2)
                        .build());

        assertThat(listPage.isGettingStartedMessageVisible(), is(false));
        assertThat(listPage.isCreateActionButtonVisible(), is(true));
    }

    @Test
    public void testListGettingStartedPageWithNonEmptyList() {
        createPullRequest(new PullRequestBuilder(BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_SRC,
                BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_TRGT).build());

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

        assertThat(listPage.isGettingStartedMessageVisible(), is(false));
        assertThat(listPage.isCreateActionButtonVisible(), is(true));
    }

    @Test
    public void testListGettingStartedPageWithReviewingFilter() {
        createPullRequest(new PullRequestBuilder(BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_SRC,
                BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_TRGT)
                .reviewer(REVIEWER_1)
                .build());

        PullRequestListPage listPage = loginAs(AUTHOR_1, AUTHOR_1,
                PullRequestListPage.class, PROJECT_KEY, REPO_SLUG, ImmutableMap.builder()
                        .put("reviewing", true)
                        .build());

        assertThat(listPage.isGettingStartedMessageVisible(), is(false));
        assertThat(listPage.isCreateActionButtonVisible(), is(true));
    }

    @Test
    public void testListGettingStartedPageWithStateFilter() {
        createPullRequest(new PullRequestBuilder(BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_SRC,
                BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_TRGT).build());

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

        assertThat(listPage.isGettingStartedMessageVisible(), is(false));
        assertThat(listPage.isCreateActionButtonVisible(), is(true));
    }

    @Test
    public void testListGettingStartedPageWithTargetBranchFilter() {
        createPullRequest(new PullRequestBuilder(BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_SRC,
                BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_TRGT).build());

        PullRequestListPage listPage = loginAs(AUTHOR_1, AUTHOR_1,
                PullRequestListPage.class, PROJECT_KEY, REPO_SLUG, ImmutableMap.builder()
                        .put("at", MASTER)
                        .build());

        assertThat(listPage.isGettingStartedMessageVisible(), is(false));
        assertThat(listPage.isCreateActionButtonVisible(), is(true));
    }
}
