package com.atlassian.webdriver.bitbucket.page.setup;

import com.atlassian.pageobjects.elements.ElementBy;
import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.webdriver.bitbucket.page.mirror.ApprovalRequiredPage;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;

/**
 * The setup page for a mirror instance
 */
public class MirrorSetupPage extends SetupPage {

    @ElementBy(id = "baseUrl")
    private PageElement mirrorBaseUrl;
    @ElementBy(id = "applicationTitle")
    private PageElement mirrorName;
    @ElementBy(id = "upstreamUrl")
    private PageElement primaryServerUrl;
    @ElementBy(id = "submit")
    private PageElement submitButton;

    @Override
    public String getStepHeading() {
        return "Configure mirror settings";
    }

    public String getMirrorBaseUrl() {
        return mirrorBaseUrl.getValue();
    }

    public String getMirrorName() {
        return mirrorName.getValue();
    }

    public String getPrimaryServerUrl() {
        return primaryServerUrl.getValue();
    }

    public boolean hasMirrorBaseUrlWarning() {
        return hasError(mirrorBaseUrl);
    }

    public boolean hasMirrorNameWarning() {
        return hasError(mirrorName);
    }

    public boolean hasPrimaryServerUrlWarning() {
        return hasError(primaryServerUrl);
    }

    public void setMirrorBaseUrl(String value) {
        mirrorBaseUrl.clear().type(value);
    }

    public void setMirrorName(String value) {
        mirrorName.clear().type(value);
    }

    public void setPrimaryServerUrl(String value) {
        primaryServerUrl.clear().type(value);
    }

    public void clickSubmit() {
        submitButton.click();
    }

    private boolean hasError(PageElement inputElement) {
        String error = inputElement.getAttribute("data-aui-notification-error");
        return StringUtils.isNotBlank(error);
    }
}
