package com.atlassian.webdriver.bitbucket.page;

import com.atlassian.pageobjects.elements.ElementBy;
import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.pageobjects.elements.query.Poller;
import com.atlassian.webdriver.bitbucket.element.AuiMessage;
import com.atlassian.webdriver.bitbucket.element.ProjectTable;
import org.openqa.selenium.By;

import java.util.List;

public class ProjectListPage extends BitbucketPage {

    @ElementBy(className = "create-project-link")
    private PageElement createProjectLink;

    @ElementBy(id = "projects-table", pageElementClass = ProjectTable.class)
    private ProjectTable projectTable;

    public ProjectListPage() {
    }

    @Override
    public String getUrl() {
        return "/projects";
    }

    public boolean canCreateProject() {
        return createProjectLink.isPresent();
    }

    public ProjectCreatePage goToCreateProjectPage() {
        createProjectLink.click();

        return pageBinder.bind(ProjectCreatePage.class);
    }
    
    public ProjectTable getProjectTable() {
        return projectTable;
    }

    public ProjectTable.ProjectRow findProjectByName(String projectName) {
        return projectTable.findProjectByName(projectName);
    }

    public List<AuiMessage> getMessages() {
        return body.findAll(By.className("aui-message"), AuiMessage.class);
    }

    @Override
    public void doWait() {
        super.doWait();
        Poller.waitUntilTrue(elementFinder.find(By.className("footer-body")).timed().isPresent());
    }
}
