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

import cz.moneta.test.dsl.newib.NewIbPageFlow;
import cz.moneta.test.dsl.newib.components.HorizontalMainMenu;
import cz.moneta.test.dsl.newib.components.VerticalMainMenu;
import cz.moneta.test.dsl.newib.mainpage.DashboardPage;
import cz.moneta.test.dsl.newib.settings.payments.sipo.SipoOrdersPage;
import cz.moneta.test.harness.endpoints.ib.NewIbEndpoint;
import cz.moneta.test.harness.support.web.*;

public interface SipoOrderVictoryPage extends NewIbPageFlow<SipoOrderVictoryPage>, HorizontalMainMenu, VerticalMainMenu {
    String SIPO_ORDER_VICTORY_CHANGE_TITLE_PAGE_XPATH = "//h2[@class='t-title t-title--h3'][text()='SIPO bylo úspěšně upraveno']";
    String OVERVIEW_BUTTON_XPATH = "//button[text()='Přehled'][@data-testid='submit']";
    String SIPO_NUMBER_TEXT_XPATH = "//p[@data-testid='TextComponent' and contains(., 'Jeho spojovací číslo je')]//strong[1][contains(text(), '%s')]";
    String SIPO_LIMIT_TEXT_XPATH = "//p[@data-testid='TextComponent' and contains(., 'maximální výše')]//strong[2][contains(text(), '%s')]";
    String PREPARED_TITLE_XPATH = "//h1[@class='t-title t-title--h3' and normalize-space(text())='Založení SIPO bylo připraveno k podpisu']";
    String BACK_TO_SIPO_ORDERS_BUTTON_XPATH = "//button[@data-testid='Button' and normalize-space(text())='Zpět na SIPO']";
    String DASHBOARD_BUTTON_XPATH = "//li[@data-testid='dashboard-link']//button[.//span[text()='Přehled']]";

    @CheckElementPresent(SIPO_ORDER_VICTORY_CHANGE_TITLE_PAGE_XPATH)
    SipoOrderVictoryPage checkSipoOrderChangeVictoryText();

    @Click(OVERVIEW_BUTTON_XPATH)
    SipoOrdersPage goToSipoOverview();

    @CheckElementsPresent({
            @CheckElementPresent(value = SIPO_NUMBER_TEXT_XPATH, isStringDynamicXpath = true),
            @CheckElementPresent(value = SIPO_LIMIT_TEXT_XPATH, isStringDynamicXpath = true)})
    SipoOrderVictoryPage checkSipoNumberAndLimit(String sipoNumber, String sipoLimit);

    @CustomAction
    default SipoOrdersPage handleSipoVictoryOrPrepared(String sipoNumber, String sipoLimit) {
        NewIbEndpoint endpoint = getEndpoint(NewIbEndpoint.class);

        if (endpoint.isElementVisible(2, PREPARED_TITLE_XPATH, Lookup.XPATH)) {
            endpoint.click(() -> OVERVIEW_BUTTON_XPATH);
        } else {
            checkSipoNumberAndLimit(sipoNumber, sipoLimit);
            endpoint.click(() -> OVERVIEW_BUTTON_XPATH);
        }
        return null;
    }

    @Click(BACK_TO_SIPO_ORDERS_BUTTON_XPATH)
    SipoOrdersPage clickOnBackToSipoOrders();

    @Click(DASHBOARD_BUTTON_XPATH)
    DashboardPage goToDashboardFromSipoVictory();
}
