package com.atlassian.webdriver.bitbucket.element;

import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.pageobjects.elements.WebDriverLocatable;
import com.atlassian.pageobjects.elements.timeout.TimeoutType;
import com.google.common.base.Function;
import org.openqa.selenium.By;

import java.util.List;

import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Lists.transform;

public class AuiForm extends LegacyElementPageObject {

    public static final By SELECTOR = By.cssSelector("form.aui");

    public AuiForm(PageElement pageElement) {
        super(pageElement);
    }

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

    public AuiForm(By locator, TimeoutType timeoutType) {
        super(locator, timeoutType);
    }

    public AuiForm(By locator, WebDriverLocatable parent) {
        super(locator, parent);
    }

    public AuiForm(By locator, WebDriverLocatable parent, TimeoutType timeoutType) {
        super(locator, parent, timeoutType);
    }

    public AuiForm(WebDriverLocatable locatable, TimeoutType timeoutType) {
        super(locatable, timeoutType);
    }

    public String getFieldValue(String fieldName) {
        return find(By.cssSelector("input[name='" + fieldName + "']")).getValue();
    }

    public AuiForm setFieldValue(String fieldName, String fieldValue) {
        find(By.cssSelector("input[name='" + fieldName + "']")).clear().type(fieldValue);
        return this;
    }

    public AuiForm checkFieldValue(String fieldName) {
        find(By.cssSelector("input[name='" + fieldName + "']")).click();
        return this;
    }

    public boolean hasErrors() {
        return findAll(By.className("error")).size() == 0;
    }

    public boolean hasFieldErrors(String fieldName) {
        return getFieldErrors(fieldName).size() == 0;
    }

    public List<String> getErrors() {
        return getErrors(By.className("aui-message-error"));
    }

    public List<String> getFieldErrors(String fieldName) {
        return getErrors(By.cssSelector("input[name='" + fieldName + "'] ~ .error"));
    }

    private List<String> getErrors(By locator) {
        return newArrayList(transform(
                findAll(locator),
                new Function<PageElement, String>() {
                    public String apply(PageElement input) {
                        return input.getText().trim();
                    }
                }
        ));
    }

    public <T> T submit(Class<T> klass, Object... args) {
        find(By.cssSelector("input.aui-button[type='submit']")).click();
        return pageBinder.bind(klass, args);
    }

    public <T> T cancel(Class<T> klass, Object... args) {
        find(By.cssSelector("a.cancel")).click();
        return pageBinder.bind(klass, args);
    }

    public boolean hasFieldError(String fieldName) {
        //data-aui-validate-state
        String attr = find(By.cssSelector("input[name='" + fieldName + "']")).getAttribute("data-aui-validate-state");
        return attr != null && attr.equals("invalid");
    }
}
