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

import com.atlassian.pageobjects.Page;
import com.atlassian.pageobjects.component.Header;
import com.atlassian.pageobjects.component.WebSudoBanner;
import com.atlassian.pageobjects.elements.ElementBy;
import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.pageobjects.page.HomePage;
import com.atlassian.webdriver.bitbucket.page.AbstractPage;
import org.openqa.selenium.By;

import static com.atlassian.webdriver.bitbucket.page.mirror.BitbucketMirrorTestedProduct.unsupported;

public class MirrorNotificationPage extends AbstractPage implements HomePage<MirrorNotificationPage.NullHeader> {
    private static final String MIRRORING_REDIRECT_PATH = "/mirror";
    private static final NullHeader NULL_HEADER = new NullHeader();

    private String initialPath;

    @ElementBy(id = "mirror-status-message")
    private PageElement statusMessage;

    @SuppressWarnings("UnusedDeclaration")
    public MirrorNotificationPage() {
        this(MIRRORING_REDIRECT_PATH);
    }

    public MirrorNotificationPage(String initialPath) {
        this.initialPath = initialPath;
    }

    @Override
    public NullHeader getHeader() {
        return NULL_HEADER;
    }

    @Override
    public String getUrl() {
        return initialPath;
    }

    @Override
    public boolean isHere() {
        return getUrlWithoutVariables(driver.getCurrentUrl()).endsWith(getUrlWithoutVariables(MIRRORING_REDIRECT_PATH));
    }

    public boolean hasStatusMessage() {
        return statusMessage.isPresent();
    }

    public static class NullHeader implements Header {

        @Override
        public boolean isLoggedIn() {
            throw unsupported();
        }

        @Override
        public <M extends Page> M logout(Class<M> nextPage) {
            throw unsupported();
        }

        @Override
        public WebSudoBanner getWebSudoBanner() {
            throw unsupported();
        }
    }
}
