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

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

import static cz.moneta.test.dsl.newib.payments.DirectDebitVictoryPage.*;

@Wait(value = {CONFIRM_BUTTON_XPATH}, waitSecondsForElement = 30)
public interface DirectDebitVictoryPage extends NewIbPageFlow<DirectDebitVictoryPage> {
    String CONFIRM_BUTTON_XPATH = "//button[@data-testid='submit']";
    String DATE_OF_FIRST_PAYMENT_XPATH = "//p[@data-testid='TextComponent']/strong[normalize-space()='%s']";
    String FROM_ACCOUNT_XPATH = "//p[@data-testid='TextComponent' and contains(., '%s')]";
    String DASHBOARD_BUTTON_XPATH = "//li[@data-testid='dashboard-link']//button[.//span[text()='Přehled']]";
    String PREPARED_TITLE_XPATH = "//h1[contains(., 'Založení inkasa je připraveno k podpisu')]";

    @CheckElementsPresent({
            @CheckElementPresent(value = DATE_OF_FIRST_PAYMENT_XPATH, isStringDynamicXpath = true),
            @CheckElementPresent(value = FROM_ACCOUNT_XPATH, isStringDynamicXpath = true)})
    SipoOrderVictoryPage checkDateAndFromAccount(String dateOfFirstPayment, String fromAccountNumber);

    @CustomAction
    default DashboardPage handleDirectDebitVictoryOrPrepared(String dateOfFirstPayment, String fromAccountNumber) {
        NewIbEndpoint endpoint = getEndpoint(NewIbEndpoint.class);
        if (endpoint.isElementVisible(2, PREPARED_TITLE_XPATH, Lookup.XPATH)) {
            endpoint.click(() -> DASHBOARD_BUTTON_XPATH);
        } else {
            checkDateAndFromAccount(dateOfFirstPayment, fromAccountNumber);
            endpoint.click(() -> DASHBOARD_BUTTON_XPATH);
        }
        return null;
    }
}