package com.atlassian.webdriver.bitbucket.page;

import com.atlassian.pageobjects.binder.WaitUntil;
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.bitbucket.element.DiffSelectionDropdown;
import org.openqa.selenium.By;

public class BaseCommitDiffPage extends BaseRepositoryPage {

    @ElementBy(id = "diff-options-dropdown-trigger")
    protected PageElement dropdown;

    protected final String commit;

    public BaseCommitDiffPage(String projectKey, String slug, String commit) {
        super(projectKey, slug);
        this.commit = commit;
    }

    @Override
    public String getUrl() {
        return "/projects/" + projectKey + "/repos/" + slug + "/commits/" + commit;
    }

    public DiffSelectionDropdown openDropdownSelector() {
        return pageBinder.bind(DiffSelectionDropdown.class, dropdown).open();
    }

    @WaitUntil
    public void diffLoaded() {
        Poller.waitUntilTrue(isFullyLoaded());
    }

    public TimedCondition isFullyLoaded() {
        return content.withTimeout(TimeoutType.COMPONENT_LOAD).find(By.className("diff-view")).timed().hasClass("fully-loaded");
    }

    public String getCommit() {
        return commit;
    }
}
