package com.atlassian.webdriver.bitbucket.page;

import com.atlassian.pageobjects.elements.ElementBy;
import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.pageobjects.elements.query.TimedQuery;
import com.atlassian.webdriver.bitbucket.element.AUIDropdown2;
import com.atlassian.webdriver.bitbucket.element.SearchableSelector;
import com.atlassian.webdriver.bitbucket.util.ElementUtils;
import org.openqa.selenium.By;

public abstract class BranchLayoutPage extends BaseRepositoryPage {

    @ElementBy(id = "repository-layout-revision-selector")
    private PageElement branchSelectorTrigger;

    @ElementBy(id = "branch-actions")
    private PageElement branchActionsTrigger;

    public BranchLayoutPage(String projectKey, String slug) {
        super(projectKey, slug);
    }

    public SearchableSelector getBranchSelector() {
        return pageBinder.bind(SearchableSelector.class, branchSelectorTrigger, By.id("inline-dialog-repository-layout-revision-selector-dialog"));
    }

    public AUIDropdown2 getBranchActions() {
        // ZeroClipboard does not play nicely with webdriver and causes the browser to hang.
        // Ensure it is disabled.
        ElementUtils.disableZeroClipboard(javascriptExecutor);
        return pageBinder.bind(AUIDropdown2.class, branchActionsTrigger);
    }

    public BranchCreationPage goCreateBranchFrom() {
        getBranchActions().open().clickItem("Create branch from here");
        return pageBinder.bind(BranchCreationPage.class);
    }

    public TimedQuery<String> getError() {
        return elementFinder.find(By.className("branch-layout-error")).timed().getText();
    }
}
