package cz.moneta.test.dsl.auto.smartauto;

import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.SmartAutoEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.auto.smartauto.DocumentsConfirmationPage.*;

@Wait(value = {DOWNLOAD_ANCHOR, DOCUMENT_CANVAS}, waitSecondsForElement = 60)
public interface DocumentsConfirmationPage extends SmartAutoFlow<DocumentsConfirmationPage>, StoreAccessor {

    String DOWNLOAD_ANCHOR = "//a[@testid='download-button']";
    String CONTINUE_BUTTON = "//button[@testid='bbutton'][not (@disabled)]//span[contains(text(), 'Souhlasím')]";
    String DISABLED_CONTINUE_BUTTON = "//button[@disabled]";
    String DOCUMENT_CANVAS = "//canvas[contains(@class, 'pdf__Page__canvas')]";

    @CustomAction
    default DocumentsConfirmationPage scrollToDocumentBottom() {
        SmartAutoEndpoint endpoint = getEndpoint(SmartAutoEndpoint.class);
        //Making sure that this element is really ready for click, for some reasons sometimes click was received by it's sibling
        endpoint.sleepSeconds(1);
        endpoint.click(() -> DOCUMENT_CANVAS);
        endpoint.sendKeysOneAtATime(Key.END);
        endpoint.waitForElementsToLoad(30, Until.GONE, DISABLED_CONTINUE_BUTTON);
        endpoint.waitForElementsToLoad(30, CONTINUE_BUTTON);
        return null;
    }

    @Click(value = CONTINUE_BUTTON, jsClick = true)
    @Page(NewCalculationPage.class)
    NewCalculationPage clickContinue();
}
