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

import com.atlassian.pageobjects.elements.ElementBy;
import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.webdriver.bitbucket.page.BitbucketLoginPage;
import org.openqa.selenium.By;

public final class SetupAdminUserPage extends SetupPage {

    @ElementBy(id = "username")
    private PageElement username;
    @ElementBy(id = "password")
    private PageElement password;
    @ElementBy(id = "confirmPassword")
    private PageElement confirmPassword;
    @ElementBy(id = "fullname")
    private PageElement fullname;
    @ElementBy(id = "email")
    private PageElement email;

    @ElementBy(id = "skipJira")
    private PageElement skipJiraButton;
    @ElementBy(id = "setupJira")
    private PageElement setupJiraButton;

    @Override
    public String getStepHeading() {
        return "Administrator account setup";
    }

    public SetupAdminUserPage setUsername(String name) {
        username.clear().type(name);
        return this;
    }

    public SetupAdminUserPage setPassword(String name) {
        password.clear().type(name);
        return this;
    }

    public SetupAdminUserPage setConfirmPassword(String name) {
        confirmPassword.clear().type(name);
        return this;
    }

    public SetupAdminUserPage setFullname(String name) {
        fullname.clear().type(name);
        return this;
    }

    public SetupAdminUserPage setEmail(String name) {
        email.clear().type(name);
        return this;
    }

    public BitbucketLoginPage clickSkipJiraSuccess() {
        skipJiraButton.click();
        return pageBinder.bind(BitbucketLoginPage.class);
    }

    public SetupAdminUserPage clickSkipJiraFail() {
        skipJiraButton.click();
        return this;
    }

    public SetupJiraIntegrationPage setupJira() {
        setupJiraButton.click();
        return pageBinder.bind(SetupJiraIntegrationPage.class);
    }

    public boolean hasUsernameWarning() {
        PageElement usernameWarning = elementFinder.find(By.cssSelector("#username + .error"));
        return usernameWarning.isPresent() && usernameWarning.isVisible();
    }

    public boolean hasPasswordWarning() {
        PageElement passwordWarning = elementFinder.find(By.cssSelector("#password + .error"));
        return passwordWarning.isPresent() && passwordWarning.isVisible();
    }

    public boolean hasPasswordConfirmationWarning() {
        PageElement confirmPasswordWarning = elementFinder.find(By.cssSelector("#confirmPassword + .error"));
        return confirmPasswordWarning.isPresent() && confirmPasswordWarning.isVisible();
    }

    public boolean hasFullnameWarning() {
        PageElement fullnameWarning = elementFinder.find(By.cssSelector("#fullname + .error"));
        return fullnameWarning.isPresent() && fullnameWarning.isVisible();
    }

    public boolean hasEmailWarning() {
        PageElement emailWarning = elementFinder.find(By.cssSelector("#email + .error"));
        return emailWarning.isPresent() && emailWarning.isVisible();
    }

}
