package com.atlassian.webdriver.bitbucket.element;

import com.atlassian.pageobjects.elements.timeout.TimeoutType;
import com.atlassian.webdriver.bitbucket.page.BitbucketPage;
import org.openqa.selenium.By;

import static com.google.common.base.Preconditions.checkState;

public class DeleteConfirmationDialog<T extends BitbucketPage> extends Dialog {

    private final Class<T> page;
    private final Object[] args;

    @SuppressWarnings("UnusedDeclaration")
    public DeleteConfirmationDialog(By locator, TimeoutType timeoutType) { // required for compatibility with existing page objects
        super(locator, timeoutType);                                       // like ProjectDeleteConfirmationPage, where the dialog is injected
        this.page = null;                                                  // during the initialisation of the page
        this.args = new Object[0];
    }

    public DeleteConfirmationDialog(By locator, TimeoutType timeoutType, Class<T> page, Object... args) {
        super(locator, timeoutType);
        this.page = page;
        this.args = args;
    }

    public void clickConfirm() {
        clickMainAction();
    }

    public T clickConfirmAndBind() {
        checkState(page != null, "Can only use clickConfirmAndBind when page is supplied");
        clickConfirm();
        return pageBinder.bind(page, args);
    }

    public boolean isConfirmEnabled() {
        return mainActionButton.isEnabled();
    }

}
