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

import com.atlassian.jira.plugins.dvcs.pageobjects.util.PageElementUtils;
import com.atlassian.pageobjects.Page;
import com.atlassian.pageobjects.elements.ElementBy;
import com.atlassian.pageobjects.elements.PageElement;
import org.openqa.selenium.By;

import java.util.List;

import static com.atlassian.pageobjects.elements.query.Poller.waitUntilTrue;
import static com.atlassian.pageobjects.elements.timeout.TimeoutType.COMPONENT_LOAD;
import static org.fest.assertions.api.Assertions.assertThat;

/**
 * @author Martin Skurla
 */
public class GreenHopperBoardPage implements Page
{
    @ElementBy(cssSelector="a[data-link-id='com.pyxis.greenhopper.jira:project-sidebar-plan-scrum'], #plan-toggle")
    PageElement boardPlanToggleViewButton;

    @ElementBy(tagName="body")
    PageElement bodyElement;


    @Override
    public String getUrl()
    {
        return "/secure/RapidBoard.jspa?rapidView=1&useStoredSettings=true&view=planning";
    }

    public void goToQABoardPlan()
    {
        waitUntilTrue(bodyElement.find(By.id("ghx-plan")).timed().isVisible());
    }

    public void assertCommitsAppearOnIssue(String issueKey, int expectedNumberOfAssociatedCommits)
    {
        PageElement backlogContainerDiv = bodyElement.find(By.className("ghx-backlog-container"));

        PageElement qa1Div  = PageElementUtils.findTagWithAttributeValue(backlogContainerDiv, "div", "data-issue-key", issueKey);
        PageElement qa1Link = PageElementUtils.findTagWithAttributeValue(qa1Div, "a", "title", issueKey);

        qa1Link.click();

        PageElement openIssueTabsMenu = bodyElement.find(By.className("ghx-detail-nav-menu"));
        waitUntilTrue(openIssueTabsMenu.withTimeout(COMPONENT_LOAD).timed().isVisible());
        PageElement commitsTabLink = PageElementUtils.findTagWithAttributeValue(openIssueTabsMenu, "a", "title", "Commits");

        commitsTabLink.click();

        PageElement issueCommitsIntegrationDiv = bodyElement.find(By.id("ghx-tab-com-atlassian-jira-plugins-jira-bitbucket-connector-plugin-dvcs-commits-greenhopper-tab"));
        List<PageElement> commits = issueCommitsIntegrationDiv.findAll(By.className("CommitContainer"));

        assertThat(commits).hasSize(expectedNumberOfAssociatedCommits);
    }
}

@@ -329,24 +329,96 @@
 il.List;%0D%0A%0D%0A
+import static com.atlassian.pageobjects.elements.query.Conditions.and;%0D%0A
 import stati
@@ -1167,16 +1167,261 @@
 tilTrue(
+and(boardPlanToggleViewButton.timed().isPresent(), boardPlanToggleViewButton.timed().isVisible()));%0D%0A        boardPlanToggleViewButton.click();%0D%0A        waitUntilTrue(and(bodyElement.find(By.id(%22ghx-plan%22)).timed().isPresent(),%0D%0A                
 bodyElem
@@ -1464,24 +1464,25 @@
 isVisible())
+)
 ;%0D%0A    %7D%0D%0A%0D%0A

fix BitbucketTests#greenHopperIntegration_ShouldAddDvcsCommitsTab