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

import com.atlassian.pageobjects.elements.ElementBy;
import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.webdriver.bitbucket.element.FormFragment;
import org.apache.commons.lang3.StringUtils;

public class SshAdminFormFragment extends FormFragment<ServerSettingsPage> {

    @ElementBy(id = "sshEnabled")
    private PageElement enabledCheckbox;

    @ElementBy(id = "sshAccessKeysEnabled")
    private PageElement keyAccessEnabledCheckbox;

    @ElementBy(id = "sshPort")
    private PageElement portTextField;

    @ElementBy(id = "sshBaseUrl")
    private PageElement baseUrlTextField;

    public SshAdminFormFragment(ServerSettingsPage page) {
        super(page);
    }

    public boolean isEnabled() {
        return enabledCheckbox.isSelected();
    }

    public boolean isKeyAccessEnabled() {
        return keyAccessEnabledCheckbox.isSelected();
    }

    public SshAdminFormFragment toggleEnabled() {
        enabledCheckbox.toggle();
        return this;
    }

    public SshAdminFormFragment toggleKeyAccessEnabled() {
        keyAccessEnabledCheckbox.toggle();
        return this;
    }

    public String getPort() {
        return portTextField.getValue();
    }

    public SshAdminFormFragment setPort(String port) {
        portTextField.clear().type(port);
        return this;
    }

    public SshAdminFormFragment setPort(Number port) {
        setPort(port.toString());
        return this;
    }

    public String getBaseUrl() {
        return StringUtils.defaultString(baseUrlTextField.getValue());
    }

    public SshAdminFormFragment setBaseUrl(String baseUrl) {
        baseUrlTextField.clear().type(baseUrl);
        return this;
    }
}
