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

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.payments.SipoOrderPage;
import cz.moneta.test.dsl.newib.settings.payments.standingorders.StandingOrdersPage;
import cz.moneta.test.harness.endpoints.ib.NewIbEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.newib.settings.payments.sipo.SipoOrdersPage.NEW_SIPO_ORDER_BTN_XPATH;
import static cz.moneta.test.dsl.newib.settings.payments.sipo.SipoOrdersPage.SIPO_ORDERS_TITLE_XPATH;

@Wait(value = {NEW_SIPO_ORDER_BTN_XPATH, SIPO_ORDERS_TITLE_XPATH}, waitSecondsForElement = 30)
public interface SipoOrdersPage extends NewIbPageFlow<StandingOrdersPage>, HorizontalMainMenu, VerticalMainMenu {
    String SIPO_ORDERS_TITLE_XPATH = "//h1[@class='t-title t-title--h3'][text()='SIPO']";
    String NEW_SIPO_ORDER_BTN_XPATH = "//button[@data-testid='create-sipo-button']";
    String SIPO_NUMBER_XPATH = "//td[contains(@class, 'c-contentTableRow__titleColumn')]//h2[contains(text(), '%s')]";
    String REMOVE_SIPO_ORDER_BTN_XPATH = "//tr[.//h2[normalize-space(text())='%s']]//button[starts-with(@data-testid,'remove-')]";
    String SIPO_REMOVE_ORDER_TITPLE_XPATH = "//h2[@class='t-title t-title--h3'][text()='Zrušení SIPA']";
    String CONFIRM_BUTTON_XPATH = "//section[contains(@class, 'c-superModal')]//div[contains(@class, 'styles_rightBtns')]//button[@data-testid='submit' and text()='Potvrdit']";
    String MOBILE_KEY_INPUT_XPATH = "//label[@for='mobileKey']//following-sibling::div//input[@id='mobileKey']";
    String MOBILE_KEY_SEND_BTN_XPATH = "//button[@data-testid='secondFactorSend']";
    String SIPO_REMOVE_ORDER_VICTORX_XPATH = "//div[contains(@class, 'c-flex') and contains(text(), 'SIPO bylo úspěšně zrušeno')]";
    String OVERVIEW_BUTTON_XPATH = "//button[@data-testid='direct-debits']";
    String CHANGE_ON_SIPO_ORDERS_TEXT_XPATH = "//div[@class='c-alert__text']//span[@data-testid='TextComponent']/span[contains(text(), '%s')]";
    String CLOSE_COOKIES_PANEL = "//button[@id='onetrust-accept-btn-handler']";
    String EDIT_SIPO_ORDER_BTN_XPATH = "//tr[.//h2[normalize-space(text())='%s']]//button[contains(@aria-label,'Upravit SIPO')]";
    String SIPO_ROW_XPATH = "//tr[.//h2[normalize-space(text())='%s'] and .//div[contains(normalize-space(text()),'%s')] and .//div[contains(translate(normalize-space(.), ' ', ''),'%s')]]";

    @CustomAction
    default SipoOrdersPage sleepRefreshAndCloseCookiesIfVisible(String sipoNumber, String fromAccount, String sipoLimit) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        String sipoXpath = String.format(SIPO_ROW_XPATH, sipoNumber, fromAccount, sipoLimit);
        Runnable refreshFunction = () -> {
            newIbEndpoint.refeshPage();
            if (newIbEndpoint.isElementVisible(1, CLOSE_COOKIES_PANEL, Lookup.XPATH)) {
                newIbEndpoint.click(() -> CLOSE_COOKIES_PANEL);
            }
        };
        newIbEndpoint.waitForElementAndRefresh(sipoXpath, Lookup.XPATH, refreshFunction, 90, 8);
        return this;
    }

    @CheckElementPresent(SIPO_ORDERS_TITLE_XPATH)
    SipoOrdersPage checkSipoOrdersTitle();

    @CheckElementPresent(NEW_SIPO_ORDER_BTN_XPATH)
    SipoOrdersPage checkNewSipoOrderBtn();

    @Click(NEW_SIPO_ORDER_BTN_XPATH)
    SipoOrderPage clickOnNewSipo();

    @Click(value = REMOVE_SIPO_ORDER_BTN_XPATH, isStringDynamicXpath = true)
    SipoOrdersPage clickOnRemoveSipoOrder(String sipoNumber);

    @CheckElementPresent(SIPO_REMOVE_ORDER_TITPLE_XPATH)
    SipoOrdersPage checkSipoOrderRemoveTitle();

    @Click(CONFIRM_BUTTON_XPATH)
    SipoOrdersPage clickOnConfirm();

    @TypeInto(MOBILE_KEY_INPUT_XPATH)
    SipoOrdersPage setMobileKey(String mobileKey);

    @Click(MOBILE_KEY_SEND_BTN_XPATH)
    SipoOrdersPage clickOnContinueMobileKey();

    @CheckElementPresent(SIPO_REMOVE_ORDER_VICTORX_XPATH)
    SipoOrdersPage checkSipoOrderRemoveVictoryText();

    @Click(OVERVIEW_BUTTON_XPATH)
    SipoOrdersPage goBackToSipoOverview();

    @CheckElementPresent(value = CHANGE_ON_SIPO_ORDERS_TEXT_XPATH, isStringDynamicXpath = true)
    SipoOrdersPage checkChangeOnSipoOrdersText(String sipoNumber);

    @CustomAction
    default SipoOrdersPage checkSipoIsDeletedAndRefreshIfNeeded(String sipoNumber) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        String sipoXpath = String.format(SIPO_NUMBER_XPATH, sipoNumber);
        Runnable refreshFunction = () -> {
            newIbEndpoint.refeshPage();
            if (newIbEndpoint.isElementVisible(1, CLOSE_COOKIES_PANEL, Lookup.XPATH)) {
                newIbEndpoint.click(() -> CLOSE_COOKIES_PANEL);
            }
        };
        newIbEndpoint.waitForElementDisappearOrRefresh(sipoXpath, Lookup.XPATH, refreshFunction, 60, 5);
        return null;
    }

    @Click(value = EDIT_SIPO_ORDER_BTN_XPATH, isStringDynamicXpath = true)
    SipoOrderPage clickOnEdit(String sipoNumber);
}
