package com.atlassian.webdriver.bitbucket.element;

import com.atlassian.pageobjects.elements.ElementBy;
import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.pageobjects.elements.PageElementFinder;
import com.atlassian.pageobjects.elements.query.Poller;
import com.atlassian.pageobjects.elements.timeout.TimeoutType;
import org.openqa.selenium.By;

import javax.inject.Inject;
import java.util.List;

public class AUISidebar extends LegacyElementPageObject {

    @Inject
    private PageElementFinder elementFinder;

    @ElementBy(className = "aui-sidebar-toggle")
    private PageElement toggle;

    public AUISidebar(PageElement sidebarContainer) {
        super(sidebarContainer);
    }

    public AUISidebar(By locator, TimeoutType timeoutType) {
        super(locator, timeoutType);
    }

    public boolean isCollapsed() {
        return this.hasAttribute("aria-expanded", "false");
    }

    public PageElement getActionsMenu() {
        // the actions are a absolutely positioned dialog
        PageElement submenu = elementFinder.find(By.className("aui-sidebar-submenu"));
        Poller.waitUntilTrue(submenu.timed().isVisible());
        return submenu;
    }

    public List<LinkElement> getActionsLinks() {
        return elementFinder.findAll(By.cssSelector(".aui-sidebar-group-actions .aui-nav-item"), LinkElement.class);
    }

    @Override
    public AUISidebar toggle() {
        toggle.click();
        return this;
    }
}


