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

import com.atlassian.bitbucket.test.BuildStatusTestHelper;
import com.atlassian.webdriver.bitbucket.element.BuildStatusDialog;
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.core.Is.is;

public class PullRequestListBuildStatusTest extends BasePullRequestListFuncTest {

    private static final String BUILD_KEY = "BUILD_KEY";
    private static final String SOURCE_COMMIT = "ff851cdafd5c9bda5b19d151c5f638f13c2b2f6e";

    @Test
    public void testBuildStatusColumn() {
        createPullRequest(new PullRequestBuilder(BRANCH_THAT_HAS_FILE_PERMISSIONS_CHANGED_SRC, MASTER).build());

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

        assertThat("Build column should not be visible if there are no builds on the page",
                listPage.isBuildStatusColumnVisible(), is(false));
    }

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

        BuildStatusTestHelper.addBuildStatus(SOURCE_COMMIT, BUILD_KEY, BuildStatusTestHelper.State.SUCCESSFUL);

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

        List<PullRequestTable.Entry> rows = listPage.getPullRequestTable().getRows();
        assertThat("There should be two pull requests in the list", rows.size(), is(2));

        assertThat("The build status should only be visible where present", rows.get(0).hasBuildStatus());
        assertThat("The build status should only be visible where present", rows.get(1).hasBuildStatus(), is(false));
    }

    @Test
    public void testBuildStatusFailed() {
        testBuildStatus(BuildStatusTestHelper.State.FAILED);
    }

    @Test
    public void testBuildStatusInProgress() {
        testBuildStatus(BuildStatusTestHelper.State.INPROGRESS);
    }

    @Test
    public void testBuildStatusMultiple() {
        createPullRequest(new PullRequestBuilder(BRANCH_THAT_HAS_SAME_FILE_MODIFIED_AND_MOVED_SRC, MASTER).build());

        BuildStatusTestHelper.addBuildStatus(SOURCE_COMMIT, BUILD_KEY + "_3", BuildStatusTestHelper.State.FAILED);
        BuildStatusTestHelper.addBuildStatus(SOURCE_COMMIT, BUILD_KEY + "_2", BuildStatusTestHelper.State.INPROGRESS);
        BuildStatusTestHelper.addBuildStatus(SOURCE_COMMIT, BUILD_KEY + "_1", BuildStatusTestHelper.State.SUCCESSFUL);

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

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

        BuildStatusDialog buildStatusList = rows.get(0).getBuildStatusDialog();

        assertThat("The individual build status should be correct",
                buildStatusList.getBuildStatusLines().get(0).getStatus(), is(BuildStatusTestHelper.State.SUCCESSFUL));
        assertThat("The individual build status should be correct",
                buildStatusList.getBuildStatusLines().get(1).getStatus(), is(BuildStatusTestHelper.State.INPROGRESS));
        assertThat("The individual build status should be correct",
                buildStatusList.getBuildStatusLines().get(2).getStatus(), is(BuildStatusTestHelper.State.FAILED));
    }

    @Test
    public void testBuildStatusSuccessful() {
        testBuildStatus(BuildStatusTestHelper.State.SUCCESSFUL);
    }

    private void testBuildStatus(BuildStatusTestHelper.State buildState) {
        createPullRequest(new PullRequestBuilder(BRANCH_THAT_HAS_SAME_FILE_MODIFIED_AND_MOVED_SRC,
                BRANCH_THAT_HAS_SAME_FILE_MODIFIED_AND_MOVED_TRGT).build());

        BuildStatusTestHelper.addBuildStatus(SOURCE_COMMIT, BUILD_KEY, buildState);

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

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

        assertThat("The build status should be visible", rows.get(0).hasBuildStatus());

        BuildStatusDialog buildStatusList = rows.get(0).getBuildStatusDialog();
        BuildStatusDialog.BuildStatusLine buildStatus = buildStatusList.getBuildStatusLines().get(0);

        assertThat("Correct build status should be displayed",
                buildStatus.getStatus(), is(buildState));
    }
}