package com.atlassian.webdriver.bitbucket.page.admin;

import com.atlassian.pageobjects.elements.ElementBy;
import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.pageobjects.elements.WebDriverCheckboxElement;
import com.atlassian.webdriver.bitbucket.page.BitbucketPage;
import com.atlassian.webdriver.bitbucket.util.ElementUtils;
import org.openqa.selenium.By;

import static org.junit.Assert.fail;

public class UserAddPage extends BitbucketPage {

    @ElementBy(id = "username")
    private PageElement usernameTextbox;
    @ElementBy(id = "fullname")
    private PageElement fullnameTextbox;
    @ElementBy(id = "email")
    private PageElement emailTextbox;
    @ElementBy(id = "notify")
    private WebDriverCheckboxElement sendEmailCheckbox;
    @ElementBy(id = "password")
    private PageElement passwordTextbox;
    @ElementBy(id = "confirmPassword")
    private PageElement confirmPasswordTextbox;
    @ElementBy(id = "createAnother")
    private WebDriverCheckboxElement createAnotherCheckbox;
    @ElementBy(cssSelector = "input[type=submit]")
    private PageElement createButton;


    public String getUrl() {
        return "/admin/users?create";
    }

    public UserAddPage setUsername(String name) {
        return updateField(usernameTextbox, name);
    }

    public UserAddPage setFullname(String fullname) {
        return updateField(fullnameTextbox, fullname);
    }

    public UserAddPage setEmail(String email) {
        return updateField(emailTextbox, email);
    }

    public UserAddPage setPassword(String password) {
        return updatePasswordField(passwordTextbox, password);
    }

    public UserAddPage setConfirmPassword(String confirmPassword) {
        return updatePasswordField(confirmPasswordTextbox, confirmPassword);
    }

    public UserAddPage selectSendEmail(boolean select) {
        if (hasSendEmailCheckbox()) {
            return updateCheckbox(sendEmailCheckbox, select);
        } else if (select) {
            fail("No mail host configuration");
        }
        return this;
    }

    public UserAddPage selectCreateAnother(boolean select) {
        return updateCheckbox(createAnotherCheckbox, select);
    }

    public UserAddPage clearAllFields() {
        usernameTextbox.clear();
        fullnameTextbox.clear();
        emailTextbox.clear();
        selectSendEmail(false);
        passwordTextbox.clear();
        confirmPasswordTextbox.clear();
        return this;
    }

    public UserEditPage clickCreateSuccess() {
        createButton.click();
        return pageBinder.bind(UserEditPage.class);
    }

    public UserAddPage clickCreateFail() {
        createButton.click();
        return this;
    }

    /* -- utils --- */

    public Iterable<String> getFieldsWithErrors() {
        return ElementUtils.getFieldsWithErrors(body.find(By.cssSelector("form.aui")));
    }

    private UserAddPage updateField(PageElement element, String text) {
        element.clear().type(text);
        return this;
    }

    private UserAddPage updateCheckbox(WebDriverCheckboxElement checkbox, boolean select) {
        if (select) {
            checkbox.check();
        } else {
            checkbox.uncheck();
        }
        return this;
    }

    private UserAddPage updatePasswordField(PageElement element, String text) {
        element.type(text);
        return this;
    }

    public boolean hasSendEmailCheckbox() {
        return sendEmailCheckbox.isPresent();
    }
}
