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

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

@SuppressWarnings("unchecked")
public abstract class AbstractDatabasePage<P extends AbstractDatabasePage<P>> extends BitbucketPage {

    @ElementBy(id = "type")
    protected PageElement typeSelectBox;

    @ElementBy(id = "hostname")
    protected PageElement hostnameField;

    @ElementBy(id = "port")
    protected PageElement portField;

    @ElementBy(id = "database")
    protected PageElement databaseField;

    @ElementBy(id = "username")
    protected PageElement usernameField;

    @ElementBy(id = "password")
    protected PageElement passwordField;

    public P selectMySQL() {
        typeSelectBox.find(By.cssSelector("option[value='mysql']")).select();
        return (P) this;
    }

    public P selectPostgres() {
        typeSelectBox.find(By.cssSelector("option[value='postgres']")).select();
        return (P) this;
    }

    public P enterHost(String host) {
        hostnameField.clear().type(host);
        return (P) this;
    }

    public P enterPort(String port) {
        portField.clear().type(port);
        return (P) this;
    }

    public P enterDatabaseName(String databaseName) {
        databaseField.clear().type(databaseName);
        return (P) this;
    }

    public P enterUsername(String username) {
        usernameField.clear().type(username);
        return (P) this;
    }

    public P enterPassword(String password) {
        passwordField.clear().type(password);
        return (P) this;
    }

}
