package cz.moneta.test.dsl.brokerportal;

import cz.moneta.test.dsl.brokerportal.forgottenpassword.ForgottenPasswordPage;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.hypos.BrokerPortalEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.brokerportal.BrokerPortalLoginPage.LOGIN_INPUT;

@Wait(LOGIN_INPUT)
public interface BrokerPortalLoginPage extends WebFlow<BrokerPortalLoginPage>, StoreAccessor {
    String LOGIN_INPUT = "//input[@name='login']";
    String PASSWORD_INPUT = "//input[@name='password']";
    String LOGIN_BUTTON = "//button[@type='submit']";
    String FORGOTTEN_PASSWORD_BUTTON = "//a[@href='#/forgottenPassword']/button";
    String ACCEPT_COOKIES_BUTTON = "//button[@id='onetrust-accept-btn-handler']";

    /**
     * Clicking on accept cookies button. This condition is here because tab with accept cookies
     * is showing up only on local computer, not on server.
     * @return
     */
    @CustomAction
    default BrokerPortalLoginPage acceptCookies() {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);

        if (endpoint.isElementPresent(ACCEPT_COOKIES_BUTTON, Lookup.XPATH)) {
            endpoint.click(() -> ACCEPT_COOKIES_BUTTON);
        }
        return null;
    }

    @TypeInto(LOGIN_INPUT)
    BrokerPortalLoginPage fillLogin(String login);

    @TypeInto(PASSWORD_INPUT)
    BrokerPortalLoginPage fillPassword(String password);

    @Click(LOGIN_BUTTON)
    BrokerPortalHomePage clickLoginButton();

    @Click(LOGIN_BUTTON)
    BrokerPortalLoginPage clickLoginStayOnLoginPageButton();

    @Click(FORGOTTEN_PASSWORD_BUTTON)
    ForgottenPasswordPage clickForgottenPasswordButton();
}