package com.atlassian.webdriver.bitbucket.page;

import com.atlassian.pageobjects.binder.Init;
import com.atlassian.pageobjects.elements.ElementBy;
import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.webdriver.bitbucket.element.UserAccountSshKeyRow;
import com.atlassian.webdriver.bitbucket.util.ElementUtils;
import com.atlassian.webdriver.bitbucket.util.Locators;

import java.util.List;

public class UserAccountSshSettingsPage extends UserAccountSettingsPage {
    @ElementBy(id = "ssh-keys-table")
    private PageElement sshKeysTable;

    @ElementBy(id = "add-key-button")
    private PageElement addKeyButton;

    @Init
    public void init() {
        ElementUtils.displayAllDeleteButtons(javascriptExecutor);
    }

    @Override
    public String getUrl() {
        return "/plugins/servlet/ssh/account/keys";
    }

    public List<UserAccountSshKeyRow> getSshKeys() {
        return sshKeysTable.findAll(Locators.tableRow(), UserAccountSshKeyRow.class);
    }

    public UserAccountAddSshKeyPage clickAddKey() {
        waitForPageLoad(new Runnable() {
            @Override
            public void run() {
                addKeyButton.click();
            }
        });
        return pageBinder.bind(UserAccountAddSshKeyPage.class);
    }
}
