package com.atlassian.webdriver.bitbucket.page;

import com.atlassian.pageobjects.elements.ElementBy;
import com.atlassian.pageobjects.elements.PageElement;

import java.util.List;

public class RepositoryCreatePage extends BitbucketPage {

    @ElementBy(name = "name")
    private PageElement slugField;

    @ElementBy(name = "submit")
    private PageElement submitButton;
    @ElementBy(name = "cancel")
    private PageElement cancelButton;

    @ElementBy(className = "stash-repository-create-form")
    private PageElement form;

    private final String projectKey;

    public RepositoryCreatePage(String projectKey) {
        this.projectKey = projectKey;
    }

    @Override
    public String getUrl() {
        return "/projects/" + projectKey + "/repos?create";
    }

    public RepositoryCreatePage setName(String name) {
        slugField.clear();
        slugField.type(name);

        return this;
    }

    public EmptyRepositoryPage submitSuccess() {
        String slug = slugField.getValue();

        submitButton.click();

        return pageBinder.bind(EmptyRepositoryPage.class, projectKey, slug);
    }

    public RepositoryCreatePage submitFail() {
        submitButton.click();

        return pageBinder.bind(RepositoryCreatePage.class, projectKey);
    }

    public ProjectOverviewPage cancel() {
        cancelButton.click();
        return pageBinder.bind(ProjectOverviewPage.class, projectKey);
    }

    public List<String> getFieldsWithErrors() {
        return getFieldsWithErrors(form);
    }

}
