package com.atlassian.webdriver.bitbucket.page;

import com.atlassian.pageobjects.binder.WaitUntil;
import com.atlassian.pageobjects.elements.ElementBy;
import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.pageobjects.elements.query.Poller;
import org.openqa.selenium.By;

import java.util.ArrayList;
import java.util.List;

import static com.atlassian.webdriver.bitbucket.util.ElementUtils.getElementTexts;

public class MergeHelpDialog {

    @ElementBy(id = "extended-merge-help-dialog")
    private PageElement dialog;

    @WaitUntil
    public void waitUntil() {
        // the dialog element itself is visibility: hidden, need to wait for its content to be visible
        Poller.waitUntilTrue(dialog.find(By.className("aui-dialog2-content")).timed().isVisible());
    }

    public List<String> getMergeHelpTasks() {
        List<PageElement> helpElements = dialog.findAll(By.className("merge-help"));
        List<String> helpTexts = new ArrayList<>(helpElements.size());

        for (PageElement helpElement : helpElements) {
            helpTexts.add(helpElement.getText());
        }
        return helpTexts;
    }

    public List<String> getInstructions() {
        return getElementTexts(dialog.findAll(By.cssSelector(".merge-instructions .code-block > pre > code")));
    }
}
