package it.com.atlassian.stash;

import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.webdriver.bitbucket.element.ProjectRepositoryTable;
import com.atlassian.webdriver.bitbucket.element.RepositoryTable;
import com.atlassian.webdriver.bitbucket.page.FileBrowserPage;
import com.atlassian.webdriver.bitbucket.page.ProjectOverviewPage;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.*;

public class ProjectOverviewTest extends BaseBrowserFuncTest {

    private static final String PROJECT_KEY = "PROJ_KEY";
    private static final String PROJECT_NAME = "Project Name";
    private static final String PROJECT_DESCRIPTION = "Project Description";

    private static final String USER_PROJ_ADMIN = "proj-admin";
    private static final String USER_PROJ_WRITE = "proj-write";
    private static final String USER_PROJ_READ = "proj-read";
    private static final String USER_REPO_READ = "repo-read";

    @BeforeClass
    public static void setupTestData() {
        getClassTestContext().project(PROJECT_KEY, PROJECT_NAME, PROJECT_DESCRIPTION)
                .user(USER_PROJ_ADMIN)
                .projectPermission(PROJECT_KEY, USER_PROJ_ADMIN, Permission.PROJECT_ADMIN)
                .user(USER_PROJ_WRITE)
                .projectPermission(PROJECT_KEY, USER_PROJ_WRITE, Permission.PROJECT_WRITE)
                .user(USER_PROJ_READ)
                .projectPermission(PROJECT_KEY, USER_PROJ_READ, Permission.PROJECT_READ);

        for (int i = 1; i <= 75; i++) {
            getClassTestContext().repository(PROJECT_KEY, "aaa" + i);
        }
        getClassTestContext().user(USER_REPO_READ)
                .repositoryPermission(PROJECT_KEY, "aaa75", USER_REPO_READ, Permission.REPO_READ);
    }

    @Test
    public void testProjectAdminCanCreateRepository() throws Exception {
        ProjectOverviewPage page = loginAs(USER_PROJ_ADMIN, USER_PROJ_ADMIN, ProjectOverviewPage.class, PROJECT_KEY);
        assertTrue(page.canCreateRepository());
    }

    @Test
    public void testProjectWriteCannotCreateRepository() throws Exception {
        ProjectOverviewPage page = loginAs(USER_PROJ_WRITE, USER_PROJ_WRITE, ProjectOverviewPage.class, PROJECT_KEY);
        assertFalse(page.canCreateRepository());
    }

    @Test
    public void testProjectReadCannotCreateRepository() throws Exception {
        ProjectOverviewPage page = loginAs(USER_PROJ_READ, USER_PROJ_READ, ProjectOverviewPage.class, PROJECT_KEY);
        assertFalse(page.canCreateRepository());
    }

    @Test
    public void testCanViewNextPage() {
        ProjectOverviewPage page = loginAsAdmin(ProjectOverviewPage.class, PROJECT_KEY);
        RepositoryTable repoTable = page.getRepositoryTable();

        RepositoryTable.RepositoryRow aaa75 = repoTable.scrollToRepository("aaa75");

        assertTrue("Should have found aaa75 in the table.", aaa75 != null);
        assertEquals("Slug matches", aaa75.getRepositorySlug(), "aaa75");
    }


    @Test
    public void testCanNavigateToRepository() {
        ProjectOverviewPage page = loginAs(USER_REPO_READ, USER_REPO_READ, ProjectOverviewPage.class, PROJECT_KEY);
        ProjectRepositoryTable repoTable = page.getRepositoryTable();

        ProjectRepositoryTable.ProjectRepositoryRow aaa75 = repoTable.findRepositoryByName("aaa75");

        assertTrue("Should have found aaa75 in the table.", aaa75 != null);
        assertEquals("Slug matches", aaa75.getRepositorySlug(), "aaa75");

        FileBrowserPage repoPage = aaa75.goToRepositoryPage();

        assertTrue("Should be at the repo page for aaa75", repoPage.isHere());
    }

}
