package com.atlassian.webdriver.bitbucket.page.repository.sync;

import com.atlassian.pageobjects.elements.ElementBy;
import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.pageobjects.elements.query.Poller;
import com.atlassian.pageobjects.elements.query.TimedCondition;
import com.atlassian.pageobjects.elements.timeout.TimeoutType;
import com.atlassian.webdriver.utils.by.ByDataAttribute;
import org.openqa.selenium.By;

/**
 * Describes the dialog shown when the "Synchronize" button is clicked on a
 * {@link RefSyncSettingsPage.DivergedBranch diverged branch}.
 * <p>
 * Currently this Page Object is geared entirely toward verifying the contents of the dialog. It is not possible
 * to trigger any of the possible actions, only to close the dialog again.
 */
public class RefSyncDialog {

    @ElementBy(className = "ref-sync-dialog", timeoutType = TimeoutType.DIALOG_LOAD)
    private PageElement dialog;

    public void cancel() {
        dialog.find(By.className("button-panel-cancel-link")).click();
        Poller.waitUntilFalse(isOpen());
    }

    public boolean hasDiscard() {
        return dialog.find(ByDataAttribute.byData("option", "discard")).isPresent();
    }

    public boolean hasMerge() {
        return dialog.find(ByDataAttribute.byData("option", "merge")).isPresent();
    }

    public TimedCondition isOpen() {
        return dialog.timed().isVisible();
    }
}
