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

import com.atlassian.pageobjects.elements.ElementBy;
import com.atlassian.pageobjects.elements.query.Poller;
import com.atlassian.webdriver.bitbucket.element.FileTree;
import com.atlassian.webdriver.bitbucket.element.UnifiedDiffFileContent;
import com.atlassian.webdriver.utils.by.ByDataAttribute;
import org.openqa.selenium.By;

public class DiffComparePage extends ComparePage {

    static final String TAB_NAME = "diff";
    static final char TAB_KEYBOARD_SHORTCUT = '1';

    @ElementBy(cssSelector = ".file-content", pageElementClass = UnifiedDiffFileContent.class)
    protected UnifiedDiffFileContent fileContent;

    @ElementBy(className = FileTree.FILE_TREE_CONTAINER_CSS_CLASS, pageElementClass = FileTree.class)
    private FileTree tree;

    public DiffComparePage(String projectKey, String slug) {
        super(projectKey, slug, TAB_NAME);
    }

    @Override
    public void doWait() {
        Poller.waitUntilFalse(elementFinder.find(By.cssSelector(".file-tree-wrapper .spinner")).timed().isPresent());
        super.doWait();
    }

    @Override
    public CommitsComparePage switchTab() {
        return switchTab(CommitsComparePage.class, CommitsComparePage.TAB_NAME);
    }

    @Override
    public CommitsComparePage switchTabUsingKeyboard() {
        return switchTab(CommitsComparePage.class, CommitsComparePage.TAB_KEYBOARD_SHORTCUT);
    }

    public boolean hasCommentsSwitch() {
        return elementFinder.find(By.className("diff-options-dropdown")).find(ByDataAttribute.byData("key", "hideComments")).isPresent();
    }

    public UnifiedDiffFileContent getDiff() {
        return fileContent;
    }

    public FileTree getTree() {
        return tree;
    }

}
