package com.atlassian.webdriver.bitbucket.page;

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

public abstract class BaseProjectPage extends BaseSidebarPage {

    @ElementBy(cssSelector = "#content > .aui-sidebar", pageElementClass = ProjectSidebarNav.class)
    private ProjectSidebarNav sidebarNav;

    private final String key;
    private String name;

    public BaseProjectPage(String key) {
        this.key = key;
    }

    public String getProjectKey() {
        return key;
    }

    public String getProjectName() {
        if (name == null) {
            name = elementFinder.find(By.cssSelector("header a.entity-name")).getText();
        }
        return name;
    }

    public ProjectSidebarNav getSidebarNav() {
        return sidebarNav;
    }

    public ProjectEditSettingsPage goToEditSettingsPage() {
        sidebarNav.clickSettingsItem();

        return pageBinder.bind(ProjectEditSettingsPage.class, key);
    }

    public ProjectOverviewPage goToOverviewPage() {
        sidebarNav.clickRepositoriesItem();

        return pageBinder.bind(ProjectOverviewPage.class, key);
    }

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