package com.atlassian.webdriver.bitbucket.page;

import com.atlassian.pageobjects.elements.ElementBy;
import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.pageobjects.elements.query.Poller;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;

public class ProjectEditSettingsPage extends BaseProjectPage {
    @ElementBy(name = "name")
    private PageElement nameField;
    @ElementBy(name = "key")
    private PageElement keyField;
    @ElementBy(name = "description")
    private PageElement descriptionField;

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

    @ElementBy(id= "projects-settings-delete-button")
    private PageElement deleteButton;

    public ProjectEditSettingsPage(String key) {
        super(key);
    }

    @Override
    public String getUrl() {
        return String.format("/projects/%s/settings", getProjectKey());
    }

    public ProjectEditSettingsPage setName(String name) {
        nameField.clear();
        nameField.type(name);

        return this;
    }

    public ProjectEditSettingsPage setKey(String key) {
        keyField.clear();
        keyField.type(key);

        return this;
    }

    public ProjectEditSettingsPage setDescription(String description) {
        descriptionField.clear();
        descriptionField.type(description);

        return this;
    }

    public String getNameValue() {
        return nameField.getValue();
    }

    public String getKeyValue() {
        return keyField.getValue();
    }

    public String getDescriptionValue() {
        return descriptionField.getValue();
    }

    public ProjectEditSettingsPage submitSuccess() {
        String key = keyField.getValue();

        waitForPageLoad(new Runnable() {
            @Override
            public void run() {
                submitButton.click();
            }
        });

        return pageBinder.bind(ProjectEditSettingsPage.class, key);
    }

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

        Poller.waitUntilTrue(elementFinder.find(By.cssSelector("#submit:enabled")).timed().isPresent());

        return pageBinder.bind(ProjectEditSettingsPage.class, getProjectKey());
    }

    public boolean isDeleteShowing() {
        return deleteButton.isPresent();
    }

    public ProjectDeleteConfirmationPage clickDelete() {
        deleteButton.click();

        return pageBinder.bind(ProjectDeleteConfirmationPage.class, getProjectKey());
    }

    public boolean wasAccessDenied() {
        try {
            return driver.findElements(By.cssSelector(".bitbucket-errors ._401")).size() > 0;
        } catch (NoSuchElementException e) {
            //Ignore
        }
        return false;
    }

    public String getFieldError(String fieldName) {
        try {
            WebElement element = driver.findElement(By.cssSelector(String.format("div.field-group > input#%s ~ div.error", fieldName)));
            return element.getText().trim();
        } catch (NoSuchElementException e) {
            return null;
        }
    }

    public String getKeyError() {
        return getFieldError("key");
    }
}
