package com.atlassian.webdriver.bitbucket.element.activity;

import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.pageobjects.elements.query.Poller;
import com.atlassian.pageobjects.elements.timeout.TimeoutType;
import com.atlassian.webdriver.bitbucket.element.PullRequestMergeConfirmDialog;
import com.atlassian.webdriver.bitbucket.page.BitbucketPage;
import com.atlassian.webdriver.bitbucket.page.PullRequestPage;
import com.google.common.base.Supplier;
import org.openqa.selenium.By;

import javax.annotation.Nonnull;

public class MergeButton<T extends PullRequestPage> extends ToolbarButton<PullRequestMergeConfirmDialog<T>> {

    public MergeButton(@Nonnull PageElement container, @Nonnull BitbucketPage currentPage,
                       @Nonnull Supplier<PullRequestMergeConfirmDialog<T>> returnElement) {
        super(container, currentPage, returnElement);
    }

    public MergeButton(By locatable, TimeoutType timeoutType, BitbucketPage currentPage,
                       Supplier<PullRequestMergeConfirmDialog<T>> returnElement) {
        super(locatable, timeoutType, currentPage, returnElement);
    }

    @Override
    public boolean isEnabled() {
        Poller.waitUntilFalse(find(By.className("bb-spinner"))
                //Merge checks can take a while so `SLOW_PAGE_LOAD` is used to avoid flakyness
                .withTimeout(TimeoutType.SLOW_PAGE_LOAD)
                .timed()
                .isPresent());
        return super.isEnabled();
    }
}
