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

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.payments.StandingOrderChangePage;
import cz.moneta.test.dsl.newib.payments.StandingOrderPage;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ib.NewIbEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.newib.settings.payments.standingorders.StandingOrdersPage.*;

@Wait(value = {STANDING_ORDERS_TITLE_XPATH, NEW_STANDING_ORDER_BTN_XPATH}, waitSecondsForElement = 30)
public interface StandingOrdersPage extends NewIbPageFlow<StandingOrdersPage>, HorizontalMainMenu, VerticalMainMenu, StoreAccessor {

    String STANDING_ORDERS_TITLE_XPATH = "//h1[@class='t-title t-title--h3'][text()='Trvalé příkazy']";
    String NEW_STANDING_ORDER_BTN_XPATH = "//button[@data-testid='create-standing-order-button']";
    String DELETE_STANDING_ORDER_BTN_XPATH = "//tr[.//h2[text()='%s']]//button[contains(@aria-label, 'Odstranit trvalý příkaz')]";
    String DELETE_CONFIRMATION_BTN_XPATH = "//button[@data-testid='submit']";
    String CONFIRM_DELETE_BUTTON_XPATH = "(//button[@data-testid='submit' and not(@disabled) and not(contains(@class,'hidden'))])[last()]";
    String DELETE_CONFIRMATION_VICTORY_TXT_XPATH = "//h1[contains(., 'Trvalý příkaz byl úspěšně smazán')]";
    String CLOSE_BTN_XPATH = "//button[text()='Zpět na Trvalé příkazy']";
    String STANDING_ORDER_EDITED_CREATED_TXT_XPATH = "//h1[@class='t-title t-title--h3'][contains(text(), 'Trvalé příkazy')]";
    String NO_STANDING_ORDERS_INFO_TXT_XPATH = "//div[text()='Momentálně nemáte žádné trvalé příkazy.']";
    String CLOSE_MODAL_BTN_XPATH = "//button[@data-testid='settings_modal-close']";
    String CLOSE_COOKIES_PANEL = "//button[@id='onetrust-accept-btn-handler']";
    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 SIGNATURE_RIGHT_XPATH = "//span[@data-testid='TextComponent'][text()='Právo podpisu']";
    String NEW_STANDING_ORDER_XPATH = "//tr[.//h2[normalize-space(text())='%s']]";
    String EDIT_STANDING_ORDER_XPATH = "//button[contains(@aria-label, concat('Upravit trvalý příkaz ', '%s'))]";
    String NEW_STANDING_ORDER_CHANGED_XPATH = "//tr[.//h2[text()='%s'] and .//div[contains(@class,'t-amount') and contains(text(),'%s')]]";
    String AMOUNT_ROW_XPATH = "//tr[@data-testid='item-info-amount']//div[contains(@class,'t-amount') and contains(normalize-space(text()),'%s')]";
    String FREQUENCY_ROW_XPATH = "//tr[@data-testid='item-info-frequency']//span[@data-testid='item-info-frequency' and normalize-space(text())='%s']";
    String END_DATE_ROW_XPATH = "//tr[@data-testid='item-info-endDate']//span[@data-testid='item-info-endDate' and contains(normalize-space(.), '%s')]";

    @Click(NEW_STANDING_ORDER_BTN_XPATH)
    StandingOrderPage clickOnNewStandingOrder();

    @CustomAction
    default StandingOrdersPage sleepRefreshAndCloseCookiesIfVisible(String standingOrderName) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        String dynamicStandingOrderXpath = String.format(NEW_STANDING_ORDER_XPATH, standingOrderName);
        Runnable refreshFunction = () -> {
            newIbEndpoint.refeshPage();
            if (newIbEndpoint.isElementVisible(1, CLOSE_COOKIES_PANEL, Lookup.XPATH)) {
                newIbEndpoint.click(() -> CLOSE_COOKIES_PANEL);
            }
        };
        newIbEndpoint.waitForElementAndRefresh(dynamicStandingOrderXpath, Lookup.XPATH, refreshFunction, 30, 5);
        return null;
    }

    @Click(value = DELETE_STANDING_ORDER_BTN_XPATH, isStringDynamicXpath = true)
    StandingOrdersPage deleteStandingOrder(String standingOrderName);

    @Wait(value = SIGNATURE_RIGHT_XPATH, until = Until.VISIBLE)
    @Click(DELETE_CONFIRMATION_BTN_XPATH)
    StandingOrdersPage confirmDeleteStandingOrder();

    @CustomAction
    default StandingOrdersPage confirmDeleteChangedStandingOrder() {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.sleepSeconds(1);
        newIbEndpoint.click(() -> CONFIRM_DELETE_BUTTON_XPATH);
        return null;
    }

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

    @Click(MOBILE_KEY_SEND_BTN_XPATH)
    StandingOrdersPage clickOnContinueMobileKey();

    @CheckElementPresent(DELETE_CONFIRMATION_VICTORY_TXT_XPATH)
    StandingOrdersPage checkDeleteConfirmationVictoryTxt();

    @Click(CLOSE_BTN_XPATH)
    StandingOrdersPage clickOnClose();

    @CheckElementPresent(STANDING_ORDER_EDITED_CREATED_TXT_XPATH)
    StandingOrdersPage checkStandingOrderEditedCreatedInfo();

    @Click(CLOSE_MODAL_BTN_XPATH)
    DashboardPage closeModal();

    @Click(value = NEW_STANDING_ORDER_XPATH, isStringDynamicXpath = true)
    StandingOrdersPage clickOnStandingOrder(String standingOrderName);

    @CheckElementPresent(value = AMOUNT_ROW_XPATH, isStringDynamicXpath = true)
    StandingOrdersPage checkAmount(String amount);

    @CheckElementPresent(value = FREQUENCY_ROW_XPATH, isStringDynamicXpath = true)
    StandingOrdersPage checkFrequency(String paymentFrequency);

    @Click(value = EDIT_STANDING_ORDER_XPATH, isStringDynamicXpath = true)
    StandingOrderChangePage clickOnEdit(String standingOrderName);

    @CustomAction
    default StandingOrdersPage waitUntilStandingOrderDisappears(String standingOrderName, String amount) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        String dynamicStandingOrderXpath = String.format(NEW_STANDING_ORDER_CHANGED_XPATH, standingOrderName, amount);
        Runnable refreshFunction = () -> {
            newIbEndpoint.refeshPage();
            if (newIbEndpoint.isElementVisible(1, CLOSE_COOKIES_PANEL, Lookup.XPATH)) {
                newIbEndpoint.click(() -> CLOSE_COOKIES_PANEL);
            }
        };
        newIbEndpoint.waitForElementDisappearOrRefresh(dynamicStandingOrderXpath, Lookup.XPATH, refreshFunction, 30, 5);
        return null;
    }
}
