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

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

import static cz.moneta.test.dsl.newib.mainpage.ApplicationPage.PAGE_LABEL_XPATH;
import static cz.moneta.test.dsl.newib.mainpage.ApplicationPage.LOADER_DIV_XPATH;

@Wait(value = PAGE_LABEL_XPATH, waitSecondsForElement = 60, by = Lookup.XPATH)
@Wait(value = LOADER_DIV_XPATH, by = Lookup.XPATH, until = Until.GONE)
public interface ApplicationPage extends NewIbPageFlow<ApplicationPage>, StoreAccessor {
    String PAGE_LABEL_XPATH = "//h1[text()=\"Žádost o podnikatelský úvěr\"]";
    String LOADER_DIV_XPATH = "//div[@class='styles_ib-cardWithLoader__3_k-p']";
    String AGREE_AND_CONTINUE_TO_SIGN_BUTTON_XPATH = "//button[text()='Souhlasím, pokračovat k podpisu' and @class='c-btn']";
    String MOBILE_KEY_INPUT = "mobileKey";
    String SIGN_BUTTON_XPATH = "//button[@data-testid='secondFactorSend']";
    String SUCCESSFULLY_SEND_APPLICATION_H2_XPATH = "//h2[text()='Vaše žádost o úvěr byla odeslána']";
    String RETURN_TO_OVERVIEW_BUTTON_XPATH = "//button[@data-testid='backToDashboard' and text()='Zpět na přehled']";

    @Click(value = AGREE_AND_CONTINUE_TO_SIGN_BUTTON_XPATH, by = Lookup.XPATH)
    ApplicationPage clickAgreeAndContinueToSign();

    @CustomAction
    default ApplicationPage scrollToBottom() {
        NewIbEndpoint ep = getEndpoint(NewIbEndpoint.class);
        ep.sendKeysOneAtATime(Key.DOWN);
        ep.sendKeysOneAtATime(Key.END);
        return null;
    }

    @TypeInto(value = MOBILE_KEY_INPUT, by = Lookup.ID)
    ApplicationPage fillMobKey(String mobKey);

    @Click(value = SIGN_BUTTON_XPATH, jsClick = true, andWait = @Wait(value = SIGN_BUTTON_XPATH, until = Until.GONE))
    ApplicationPage clickSign();

    @CheckElementPresent(SUCCESSFULLY_SEND_APPLICATION_H2_XPATH)
    ApplicationPage checkSuccessApplicationSent();

    @Click(RETURN_TO_OVERVIEW_BUTTON_XPATH)
    DashboardPage clickReturnToOverview();
}
