package cz.moneta.test.dsl.ufo.banka.pages.products.sme;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.cards.debit.OBR003_S001_ClientContractSignPage;
import cz.moneta.test.dsl.ufo.operations.shared.SharedElements;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.ufo.banka.pages.products.sme.CHK001_S001_WaitingForModulationPage.*;

@Wait(value = CHK001_S001_PAGE_LABEL_XPATH, waitSecondsForElement = 180, by = Lookup.XPATH)
public interface CHK001_S001_WaitingForModulationPage extends WebFlow<CHK001_S001_WaitingForModulationPage>, StoreAccessor {
    String CHK001_S001_PAGE_LABEL_XPATH = "//div[text()='CHK001_S001']";

    @CustomAction()
    default CHK001_S001_WaitingForModulationTransitionsPage waitForModulation(int timeout) {
        processWait(timeout, 3);
        return null;
    }

    @CustomAction()
    default CHK001_S001_WaitingForModulationTransitionsPage waitForModulation(int timeout, int allowedReturnsFromOBR003) {
        processWait(timeout, allowedReturnsFromOBR003);
        return null;
    }

    private void processWait(int timeout, int allowedReturnsFromOBR003) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        for (int i = 0; i < allowedReturnsFromOBR003; i++) {
            endpoint.waitForElementsToLoad(timeout, Lookup.CLASSNAME, Until.GONE, SharedElements.LOAD_IMG_DIV_CLASS);
            if (endpoint.isElementVisible(2, OBR003_S001_ClientContractSignPage.CENTER_BUTTON, Lookup.ID)) {
                endpoint.click(() -> OBR003_S001_ClientContractSignPage.CENTER_BUTTON);
            } else {
                break;
            }
        }
    }
}
