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

import cz.moneta.test.dsl.newib.NewIbPageFlow;
import cz.moneta.test.dsl.newib.accounts.HistoryTab;
import cz.moneta.test.harness.endpoints.ib.NewIbEndpoint;
import cz.moneta.test.harness.support.web.Wait;
import cz.moneta.test.harness.support.web.CustomAction;
import cz.moneta.test.harness.support.web.Lookup;
import org.junit.jupiter.api.Assertions;

import static cz.moneta.test.dsl.newib.payments.BulkPaymentVictoryPage.TRANSACTIONS_OVERVIEW_BTN_XPATH;
import static cz.moneta.test.dsl.newib.payments.BulkPaymentVictoryPage.NEXT_PAYMENT_BTN_XPATH;

@Wait({TRANSACTIONS_OVERVIEW_BTN_XPATH, NEXT_PAYMENT_BTN_XPATH})
public interface BulkPaymentVictoryPage extends NewIbPageFlow<HistoryTab> {
    String BULK_PAYMENT_VICTORY_TITLE_TEXT_XPATH = "//h1[@class='t-title t-title--h3 u-mb--large'][text()='Přijali jsme Vaše platby ke zpracování']";
    String TRANSACTIONS_OVERVIEW_BTN_XPATH = "//button[@data-testid='bulk-payment-continue']";
    String NEXT_PAYMENT_BTN_XPATH = "//button[@data-testid='bulk-payment-next-payment']";
    String INFO_TEXT_NUMBER_OF_PAYMENTS_XPATH = "//div[@class='u-mb--xxxLarge']//strong[text()='%s']";
    String INFO_TEXT_TOTAL_AMOUNT_XPATH = "//div[@class='u-mb--xxxLarge']//strong[starts-with(normalize-space(), '%s')]";
    String INFO_TEXT_TO_SIGNATURE_XPATH = "//h1[contains(normalize-space(), 'Založení hromadné platby je připraveno k podpisu')]";

    @CustomAction
    default HistoryTab checkBulkPaymentVictoryPage(String numberOfBulkPaymentVictoryText, String totalAmountBulkPayment) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);

        if (newIbEndpoint.isElementPresent(INFO_TEXT_TO_SIGNATURE_XPATH, Lookup.XPATH)) {
            newIbEndpoint.click(() -> TRANSACTIONS_OVERVIEW_BTN_XPATH);
        } else {
            Assertions.assertTrue(newIbEndpoint.isElementPresent(String.format(INFO_TEXT_NUMBER_OF_PAYMENTS_XPATH, numberOfBulkPaymentVictoryText), Lookup.XPATH), "Number of bulk payments not displayed as expected");
            Assertions.assertTrue(newIbEndpoint.isElementPresent(String.format(INFO_TEXT_TOTAL_AMOUNT_XPATH, totalAmountBulkPayment), Lookup.XPATH), "Total amount of bulk payments not displayed as expected");
            Assertions.assertTrue(newIbEndpoint.isElementPresent(BULK_PAYMENT_VICTORY_TITLE_TEXT_XPATH, Lookup.XPATH), "Bulk payment victory title not displayed");
            newIbEndpoint.click(() -> TRANSACTIONS_OVERVIEW_BTN_XPATH);
        }

        return null;
    }
}
