package ru.otus.mobile.page;

import com.codeborne.selenide.SelenideElement;
import com.google.inject.Singleton;
import ru.otus.mobile.component.AlertDialogComponent;
import ru.otus.mobile.config.TestAccount;

@Singleton
public final class LoginPage extends AbsPageObject {
    private final SelenideElement usernameInput = byId("username_text_input");
    private final SelenideElement passwordInput = byId("password_text_input");
    private final SelenideElement loginButton = byId("log_in_button");
    private final SelenideElement appMainContainer = byId("app_main_fragment_container");
    private final AlertDialogComponent alertDialog = new AlertDialogComponent(appMainContainer);

    public void login(TestAccount account) {
        if (!isOpened()) {
            return;
        }
        usernameInput.click();
        usernameInput.clear();
        usernameInput.sendKeys(account.username());
        passwordInput.click();
        passwordInput.clear();
        passwordInput.sendKeys(account.password());
        loginButton.click();
        alertDialog.acceptIfVisible();
    }

    public boolean isOpened() {
        return usernameInput.exists() && passwordInput.exists();
    }
}
