package it.restart.com.atlassian.jira.plugins.dvcs.github;

import com.atlassian.jira.plugins.dvcs.pageobjects.common.OAuth;
import com.atlassian.pageobjects.Page;
import com.atlassian.pageobjects.PageBinder;
import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.pageobjects.elements.PageElementFinder;
import com.atlassian.pageobjects.elements.query.Poller;
import com.atlassian.webdriver.utils.WebDriverUtil;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;

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

public class GithubOAuthApplicationPage implements Page
{
    @Inject
    private PageElementFinder pageElementFinder;

    @Inject
    private PageBinder pageBinder;

    private final String hostUrl;

    public GithubOAuthApplicationPage()
    {
        this("https://github.com");
    }

    public GithubOAuthApplicationPage(String hostUrl)
    {
        this.hostUrl = hostUrl;
    }

    @Override
    public String getUrl()
    {
        return hostUrl + "/settings/applications";
    }

    public void removeConsumer(final OAuth oAuth)
    {
        this.removeConsumer(StringUtils.removeStart(oAuth.applicationId, hostUrl));
    }

    public void removeConsumer(final String appUri)
    {
        removeConsumer(By.xpath("//a[@href='" + appUri + "']"));
    }

    public void removeConsumerForAppName(final String appName)
    {
        removeConsumer(By.linkText(appName));
    }

    public void removeConsumer(By bySelector)
    {
        // try to find the developer tab and click on it
        // if it is found we are in github so we need to use developers tab instead of applications tab
        // if not we are in github enterprise we do not have developers tab
        // TODO change this test
        // it is better to use /settings/developers for github tests
        // and /settings/applications for github enterprise
        GithubOAuthApplicationPage page = this;
        PageElement developerTabLink = pageElementFinder.find(By.cssSelector(".tabnav-tabs a[href='/settings/developers']"));
        if (developerTabLink.isPresent() && developerTabLink.isEnabled())
        {
           developerTabLink.click();
           page = pageBinder.bind(GithubOAuthApplicationPage.class);
           Poller.waitUntilTrue(page.pageElementFinder.find(By.className("table-list-bordered")).timed().isPresent());
        }
        removeConsumer(page, bySelector);
    }

    public List<PageElement> findOAthApplications(final String partialLinkText)
    {
        return pageElementFinder.findAll(By.partialLinkText(partialLinkText));
    }

    private static void removeConsumer(final GithubOAuthApplicationPage page, final By bySelector)
    {
        final PageElement link = page.pageElementFinder.find(bySelector);
        if (link.isPresent() && link.isVisible())
        {
            link.click();
            page.pageBinder.bind(GithubOAuthPage.class).removeConsumer();
        }
        else
        {
            // get next link
            final PageElement nextPageLink = page.pageElementFinder.find(By.className("next_page"));
            if (nextPageLink.isPresent() && nextPageLink.isVisible() && nextPageLink.getTagName().equalsIgnoreCase("a")
                    && nextPageLink.isEnabled())
            {
                nextPageLink.click();
                Poller.waitUntilTrue(page.pageElementFinder.find(By.className("table-list-bordered")).timed().isPresent());
                final GithubOAuthApplicationPage nextPage = page.pageBinder.bind(GithubOAuthApplicationPage.class);
                removeConsumer(nextPage, bySelector);
            }
            else
            {
                throw new RuntimeException("Can not find consumer application link");
            }
        }
    }
}

@@ -426,16 +426,89 @@
 erUtil;%0A
+import it.restart.com.atlassian.jira.plugins.dvcs.test.GithubTestHelper;%0A
 import o
@@ -627,16 +627,108 @@
 nject;%0A%0A
+import static it.restart.com.atlassian.jira.plugins.dvcs.test.GithubTestHelper.GITHUB_URL;%0A%0A
 public c
@@ -985,28 +985,18 @@
 his(
-%22https://github.com%22
+GITHUB_URL
 );%0A 
@@ -1139,32 +1139,267 @@
  getUrl()%0A    %7B%0A
+        // if github then we need to visit /settings/developers%0A        if (hostUrl.equalsIgnoreCase(GITHUB_URL))%0A        %7B%0A            return hostUrl + %22/settings/developers%22;%0A        %7D%0A        else // github enterprise%0A        %7B%0A    
         return h
@@ -1429,24 +1429,35 @@
 lications%22;%0A
+        %7D%0A%0A
     %7D%0A%0A    p
@@ -1914,919 +1914,27 @@
     
-// try to find the developer tab and click on it%0A        // if it is found we are in github so we need to use developers tab instead of applications tab%0A        // if not we are in github enterprise we do not have developers tab%0A        // TODO change this test%0A        // it is better to use /settings/developers for github tests%0A        // and /settings/applications for github enterprise%0A        GithubOAuthApplicationPage page = this;%0A        PageElement developerTabLink = pageElementFinder.find(By.cssSelector(%22.tabnav-tabs a%5Bhref='/settings/developers'%5D%22));%0A        if (developerTabLink.isPresent() && developerTabLink.isEnabled())%0A        %7B%0A           developerTabLink.click();%0A           page = pageBinder.bind(GithubOAuthApplicationPage.class);%0A           Poller.waitUntilTrue(page.pageElementFinder.find(By.className(%22table-list-bordered%22)).timed().isPresent());%0A        %7D%0A        removeConsumer(page
+removeConsumer(this
 , by

fix GithupTests#afterClass Flakiness