package com.atlassian.webdriver.bitbucket.page;

import com.atlassian.pageobjects.elements.ElementBy;
import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.webdriver.bitbucket.element.PullRequestDetails;
import com.atlassian.webdriver.bitbucket.element.RefLozenge;
import com.atlassian.webdriver.bitbucket.element.SearchableSelector;
import com.atlassian.webdriver.bitbucket.element.SourceTargetSelector;

import java.util.List;

public class PullRequestCreatePage extends FormPage<PullRequestOverviewPage, FileBrowserPage> {

    private final String projectKey;
    private final String slug;

    @ElementBy(className = "pull-request-details")
    private PullRequestDetails details;

    @ElementBy(id = "submit-form")
    private PageElement submitButton;

    @ElementBy(id = "show-create-pr-button")
    private PageElement continueButton;

    @ElementBy(cssSelector = ".branch-change.show-hide-button")
    private PageElement changeButton;

    @ElementBy(cssSelector = ".aui-lozenge.source-branch")
    private RefLozenge sourceBranch;

    @ElementBy(cssSelector = ".aui-lozenge.target-branch")
    private RefLozenge targetBranch;

    public PullRequestCreatePage(String projectKey, String slug) {
        this.projectKey = projectKey;
        this.slug = slug;
    }

    @Override
    public String getUrl() {
        return String.format("/projects/%s/repos/%s/pull-requests?create", projectKey, slug);
    }

    public PullRequestDetails getDetails() {
        return details;
    }

    public boolean canCreate() {
        return submitButton.isEnabled();
    }

    public SearchableSelector getSourceRepositorySelector() {
        return pageBinder.bind(SourceTargetSelector.class).getSourceRepositorySelector();
    }

    public SearchableSelector getSourceBranchSelector() {
        return pageBinder.bind(SourceTargetSelector.class).getSourceBranchSelector();
    }

    public SearchableSelector getTargetRepositorySelector() {
        return pageBinder.bind(SourceTargetSelector.class).getTargetRepositorySelector();
    }

    public SearchableSelector getTargetBranchSelector() {
        return pageBinder.bind(SourceTargetSelector.class).getTargetBranchSelector();
    }

    public RefLozenge getCollapsedSourceBranch() {
        return sourceBranch;
    }

    public RefLozenge getCollapsedTargetBranch() {
        return targetBranch;
    }

    public PullRequestCreatePage clickContinue() {
        continueButton.click();
        return this;
    }

    public PullRequestCreatePage clickChange() {
        changeButton.click();
        return this;
    }

    public boolean isShowingPRForm() {
        return !continueButton.isVisible();
    }

    @Override
    public PullRequestCreatePage clickSubmitExpectingErrors(Object... args) {
        return (PullRequestCreatePage) super.clickSubmitExpectingErrors(projectKey, slug);
    }

    public PullRequestOverviewPage clickSubmitToFork(String targetProject, String targetRepo) {
        return super.clickSubmit(targetProject, targetRepo, 0);
    }

    public PullRequestOverviewPage clickSubmitToPersonalFork(String username, String targetRepo) {
        return form.submit(UserPullRequestOverviewPage.class, username, targetRepo, 0);
    }

    @Override
    public PullRequestOverviewPage clickSubmit(Object... args) {
        return super.clickSubmit(projectKey, slug, 0);
    }

    public List<String> getTitleFieldErrors() {
        return form.getFieldErrors("title");
    }
}
