package com.atlassian.webdriver.bitbucket.element;

import com.atlassian.pageobjects.PageBinder;
import com.atlassian.pageobjects.elements.ElementBy;
import com.atlassian.pageobjects.elements.PageElement;
import org.openqa.selenium.By;

import javax.annotation.Nonnull;
import javax.inject.Inject;

public class SourceTargetSelector {

    @Inject
    private PageBinder pageBinder;

    @ElementBy(id = "sourceRepo")
    private PageElement sourceRepoSelector;

    @ElementBy(id = "sourceRepo-field")
    private PageElement sourceRepoField;

    @ElementBy(id = "sourceBranch")
    private PageElement sourceBranchSelector;

    @ElementBy(id = "sourceBranch-field")
    private PageElement sourceBranchField;

    @ElementBy(id = "targetRepo")
    private PageElement targetRepoSelector;

    @ElementBy(id = "targetRepo-field")
    private PageElement targetRepoField;

    @ElementBy(id = "targetBranch")
    private PageElement targetBranchSelector;

    @ElementBy(id = "targetBranch-field")
    private PageElement targetBranchField;

    public SearchableSelector getSourceRepositorySelector() {
        return pageBinder.bind(SearchableSelector.class, sourceRepoSelector, By.id("sourceRepoDialog"), sourceRepoField);
    }

    public SearchableSelector getSourceBranchSelector() {
        return pageBinder.bind(SearchableSelector.class, sourceBranchSelector, By.id("sourceBranchDialog"), sourceBranchField);
    }

    public SearchableSelector getTargetRepositorySelector() {
        return pageBinder.bind(SearchableSelector.class, targetRepoSelector, By.id("targetRepoDialog"), targetRepoField);
    }

    public SearchableSelector getTargetBranchSelector() {
        return pageBinder.bind(SearchableSelector.class, targetBranchSelector, By.id("targetBranchDialog"), targetBranchField);
    }

    public CommitBadge getSourceChangsetBadge() {
        return pageBinder.bind(CommitBadge.class, By.cssSelector(".source-selector .commit-badge-detailed"));
    }

    public CommitBadge getTargetChangsetBadge() {
        return pageBinder.bind(CommitBadge.class, By.cssSelector(".target-selector .commit-badge-detailed"));
    }

    public static class CommitBadge extends LegacyElementPageObject {

        public CommitBadge(@Nonnull PageElement pageElement) {
            super(pageElement);
        }

        public CommitBadge(By locator) {
            super(locator);
        }

        public String getAuthor() {
            return find(By.className("commit-author")).getText();
        }

        public String getId() {
            return find(By.className("commitid")).getText();
        }

        public String getDate() {
            return find(By.tagName("time")).getText();
        }
    }

}
