package com.atlassian.webdriver.bitbucket.page;

import com.atlassian.pageobjects.elements.ElementBy;
import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.webdriver.bitbucket.element.BitbucketHeader;
import com.atlassian.webdriver.bitbucket.page.admin.AdminHomePage;

public class BitbucketHomePage extends ProjectListPage implements com.atlassian.pageobjects.page.HomePage<BitbucketHeader> {

    private static final String LOGIN = "/login(;jsessionid=[A-Z0-9]+)?";

    @ElementBy(linkText = "Projects")
    private PageElement linkProjects;

    @ElementBy(linkText = "Administration")
    private PageElement linkAdmin;

    @Override
    public boolean isHere() {
        return super.isHere() || isLoginPage();
    }

    private boolean isLoginPage() {
        final String url = driver.getCurrentUrl();
        final int pos = url.lastIndexOf('/');
        return pos > 0 && url.substring(pos).matches(LOGIN);
    }

    public ProjectListPage clickProjects() {
        linkProjects.click();
        return pageBinder.bind(ProjectListPage.class);
    }

    public AdminHomePage clickAdmin() {
        linkAdmin.click();
        return pageBinder.bind(AdminHomePage.class);
    }

}
