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

import cz.moneta.test.dsl.ufo.banka.pages.cards.debit.OBR003_S001_ClientContractSignPage;
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.KUL016_S001_FirstDataAboutClientPage.CONTINUE_BUTTON;
import static cz.moneta.test.dsl.ufo.banka.pages.products.sme.KUL016_S001_FirstDataAboutClientPage.KUL016_S001_PAGE_LABEL_XPATH;
import static cz.moneta.test.dsl.ufo.operations.shared.SharedElements.LOAD_IMG_DIV_CLASS;

@Wait(value = KUL016_S001_PAGE_LABEL_XPATH, by = Lookup.XPATH, waitSecondsForElement = 60)
@Wait(CONTINUE_BUTTON)
public interface KUL016_S001_FirstDataAboutClientPage extends WebFlow<KUL016_S001_FirstDataAboutClientPage>, StoreAccessor {
    String KUL016_S001_PAGE_LABEL_XPATH = "//div[text()='KUL016_S001']";

    String CONTINUE_BUTTON = "KUL016_S001_B001";

    String BIRTH_NUMBER_INPUT = "KUL016_S001_E001";
    String BIRTH_DATE_INPUT = "KUL016_S001_E003";
    String FIRST_NAME_INPUT = "KUL016_S001_E004";
    String LAST_NAME_INPUT = "KUL016_S001_E005";

    @TypeInto(FIRST_NAME_INPUT)
    KUL016_S001_FirstDataAboutClientPage fillFirstName(String firstName);

    @TypeInto(LAST_NAME_INPUT)
    KUL016_S001_FirstDataAboutClientPage fillLastName(String lastName);

    @TypeInto(value = BIRTH_NUMBER_INPUT, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    KUL016_S001_FirstDataAboutClientPage fillBirthNumber(String birthNumber);

    @CustomAction
    default OBR003_S001_ClientContractSignPage dismissAlertIfVisibleAndContinue() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.click(() -> CONTINUE_BUTTON);
        if (endpoint.isAlertPresent(3)) {
            endpoint.dismissAlert(1);
            endpoint.waitForElementsToLoad(2, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
            endpoint.click(() -> CONTINUE_BUTTON);
        }
        return null;
    }
}
