package cz.moneta.test.dsl.newib.authentification;

import cz.moneta.test.dsl.newib.NewIbPageFlow;
import cz.moneta.test.harness.endpoints.ib.NewIbEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.newib.authentification.LoginPage.LOGIN_WITH_ID_AND_PASSWORD_XPATH;
import static cz.moneta.test.dsl.newib.authentification.LoginPage.PAGE_TITLE;

@Wait(value = {PAGE_TITLE, LOGIN_WITH_ID_AND_PASSWORD_XPATH}, waitSecondsForElement = 60)
public interface LoginPage extends NewIbPageFlow<LoginPage> {

    String PAGE_TITLE = "//a[@class='c-logo__link']";
    String ID_INPUT = "//input[@id='ibId']";
    String PASSWORD_INPUT = "//input[@id='password']";
    String LOGIN_BUTTON = "//button[@data-testid='submit']";
    String CLOSE_COOKIES_PANEL = "//button[@id='onetrust-accept-btn-handler']";
    String LOGIN_WITH_ID_AND_PASSWORD_XPATH = "//ul[@class='c-tabs__list']/li[2]/button";

    @Wait(CLOSE_COOKIES_PANEL)
    @Click(CLOSE_COOKIES_PANEL)
    LoginPage closeCookiesPanel();

    @CustomAction
    default LoginPage closeCookiesPanelIfVisible() {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        if (newIbEndpoint.isElementVisible(5, CLOSE_COOKIES_PANEL, Lookup.XPATH)) {
            newIbEndpoint.click(() -> CLOSE_COOKIES_PANEL);
        }
        return null;
    }

    @TypeInto(ID_INPUT)
    LoginPage typeUsername(String password);

    @TypeInto(PASSWORD_INPUT)
    LoginPage typePassword(String password);

    /* jsClick implemented because showing up banner with last users login
     Will be change to scroll and click to future or just click. */
    @Click(value = LOGIN_BUTTON, jsClick = true)
    LoginPageTransitions clickToLogin();

    @Click(LOGIN_WITH_ID_AND_PASSWORD_XPATH)
    LoginPage clickLoginWithIdAndPassword();

    @Click(LOGIN_BUTTON)
    LoginPage clickContinue();
}