package com.frameworkium.integration.github.pages.web.components;

import com.frameworkium.core.ui.annotations.Visible;
import com.frameworkium.core.ui.pages.PageFactory;
import com.frameworkium.core.ui.pages.Visibility;
import com.frameworkium.core.ui.tests.BaseTest;
import com.frameworkium.integration.github.pages.web.ExplorePage;
import com.frameworkium.integration.github.pages.web.SearchResultsPage;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Wait;
import ru.yandex.qatools.allure.annotations.Step;
import ru.yandex.qatools.htmlelements.annotations.Name;
import ru.yandex.qatools.htmlelements.element.Button;
import ru.yandex.qatools.htmlelements.element.HtmlElement;
import ru.yandex.qatools.htmlelements.element.Link;
import ru.yandex.qatools.htmlelements.element.TextInput;

import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOf;

@Name("Github Header")
@FindBy(css = "header")
public class HeaderComponent extends HtmlElement {

    @Visible
    @Name("Home Logo/Link")
    @FindBy(css = "a.header-logo-invertocat")
    private Link homeLink;

    @Name("Search Box")
    @FindBy(name = "q")
    private TextInput searchBox;

    @Name("Hamburger button")
    @FindBy(className = "btn-link")
    private Button hamburgerButton;

    @Name("Explore Link")
    @FindBy(css = "nav a[href='/explore']")
    private Link exploreLink;

    @Step("Go to the explore page")
    public ExplorePage clickExplore() {
        if (hamburgerButton.isDisplayed())
            hamburgerButton.click();
        exploreLink.click();
        return PageFactory.newInstance(ExplorePage.class);
    }

    @Step("Search for the text '{0}'")
    public SearchResultsPage search(String searchText) {
        searchBox.sendKeys(searchText + Keys.ENTER);
        return PageFactory.newInstance(SearchResultsPage.class);
    }

    @Step("Testing Visibility.forceVisible()")
    public void testForceVisible() {
        Wait<WebDriver> wait = BaseTest.newDefaultWait();

        WebElement link = homeLink.getWrappedElement();
        // hide the home link
        BaseTest.getDriver().executeScript("arguments[0].style.visibility='hidden';", link);
        wait.until(ExpectedConditions.not(visibilityOf(link)));
        // test force visible works
        new Visibility().forceVisible(link);
        wait.until(visibilityOf(link));
    }
}

@@ -1063,16 +1063,63 @@
 ityOf;%0A%0A
+import org.apache.tools.ant.taskdefs.WaitFor;%0A%0A
 @Name(%22G
@@ -1512,190 +1512,634 @@
 on;%0A
-%0A    @Name(%22Explore Link%22)%0A    @FindBy(css = %22nav a%5Bhref='/explore'%5D%22)%0A    private Link exploreLink;%0A%0A    @Step(%22Go to the explore page%22)%0A    public ExplorePage clickExplore(
+    %0A    @Name(%22Header Menu%22)%0A    @FindBy(css = %22nav.site-header-nav%22)%0A    private WebElement headerMenu;%0A%0A    @Name(%22Explore Link%22)%0A    @FindBy(css = %22nav a%5Bhref='/explore'%5D%22)%0A    private Link exploreLink;%0A%0A    private HeaderComponent showHeaderMenu() %7B%0A    %09Wait%3CWebDriver%3E wait = BaseTest.newDefaultWait();%0A    %09// Workaround due to flaky GitHub site%0A    %09%0A    %09// If browser is opened with width of 960 pixels or less%0A    %09// then the Header Menu is not displayed and a 'Hamburger' button is displayed instead. %0A    %09// This button needs to be clicked to display the Header Menu.%0A    %09if (!headerMenu.isDisplayed()
 ) %7B%0A    
     
@@ -2138,16 +2138,15 @@
     
-   
- 
+%09%09
 if (
+!
 hamb
@@ -2171,16 +2171,18 @@
 layed())
+ %7B
 %0A       
@@ -2186,33 +2186,487 @@
     
-    hamburgerButton.click
+%09%09// Sometimes the Hamburger button is not initially displayed%0A    %09%09%09// so click the Header element to display the button %0A    %09%09%09this.click();%0A    %09%09%7D%0A    %09%09hamburgerButton.click();%0A       %09%09%0A       %09%09// Ensure the Header Menu is displayed before attempting to click a link%0A       %09%09wait.until(ExpectedConditions.visibilityOf(headerMenu));%0A    %09%7D%0A        return this;%0A    %7D%0A    %0A    @Step(%22Go to the explore page%22)%0A    public ExplorePage clickExplore() %7B%0A        showHeaderMenu
 ();%0A

Fix flaky ComponentExampleTest test