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

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

import static cz.moneta.test.dsl.newib.settings.payments.directdebit.DirectDebitsPage.*;

@Wait(value = {DIRECT_DEBIT_TITLE_XPATH, NEW_DIRECT_DEBIT_BTN_XPATH}, waitSecondsForElement = 30)
public interface DirectDebitsPage extends NewIbPageFlow<DirectDebitsPage> {

    String DIRECT_DEBIT_TITLE_XPATH = "//h1[@class='t-title t-title--h3'][text()='Inkasa']";
    String NEW_DIRECT_DEBIT_BTN_XPATH = "//button[@data-testid='create-direct-debit-button']";
    String DIRECT_DEBIT_REMOVE_BTN_XPATH = "//tr[.//h2[normalize-space(text())='%s']]//button[contains(@aria-label,'Odstranit')]";
    String DIRECT_DEBIT_CHANGE_BTN_XPATH = "//tr[.//h2[normalize-space(text())='%s']]//button[contains(@aria-label,'Upravit')]";
    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 CONFIRMATION_BTN_XPATH = "//button[@data-testid='submit']";
    String BACK_TO_DIRECT_DEBITS_PAGE_BTN_XPATH = "//button[@data-testid='direct-debits']";
    String TITLE_VICTORY_PAGE_XPATH = "//h1[normalize-space(.)='Inkaso bylo úspěšně zrušeno']";
    String CLOSE_COOKIES_PANEL = "//button[@id='onetrust-accept-btn-handler']";
    String DIRECT_DEBIT_NUMBER_XPATH = "//tr[.//h2[normalize-space(text())='%s']]";
    String DIRECT_DEBIT_ROW_XPATH = "//tr[.//h2[normalize-space(text())='%s'] and .//div[contains(normalize-space(text()),'%s')] and .//div[contains(translate(normalize-space(.), ' ', ''),'%s')]]";
    String DIRECT_DEBIT_NAME_INPUT_XPATH = "//input[@id='description']";
    String DIRECT_DEBIT_LIMIT_INPUT_XPATH = "//input[@id='amountLimit']";
    String DIRECT_DEBIT_NAME_XPATH = "//tr[.//span[normalize-space(text())='Název inkasa'] and .//span[normalize-space(text())='%s']]";
    String DIRECT_DEBIT_LIMIT_XPATH = "//tr[.//span[normalize-space(text())='Limit inkasa'] and .//div[contains(translate(normalize-space(.), ' ', ''), '%s')]]";
    String CHANGE_TITLE_VICTORY_PAGE_XPATH = "//h1[normalize-space(.)='Inkaso bylo úspěšně upraveno']";

    @Click(value = DIRECT_DEBIT_REMOVE_BTN_XPATH, isStringDynamicXpath = true)
    DirectDebitsPage clickOnDeleteDirectDebit(String directDebitName);

    @Click(value = DIRECT_DEBIT_CHANGE_BTN_XPATH, isStringDynamicXpath = true)
    DirectDebitsPage clickOnChangeDirectDebit(String directDebitName);

    @Wait(value = SIGNATURE_RIGHT_XPATH, until = Until.VISIBLE)
    @Click(value = CONFIRMATION_BTN_XPATH, jsClick = true)
    DirectDebitsPage confirmDeleteDirectDebit();

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

    @Click(MOBILE_KEY_SEND_BTN_XPATH)
    DirectDebitsPage clickOnContinueMobileKey();

    @Click(BACK_TO_DIRECT_DEBITS_PAGE_BTN_XPATH)
    DirectDebitsPage clickOnBackToDirectDebitsPage();

    @CheckElementPresent(value = TITLE_VICTORY_PAGE_XPATH)
    DirectDebitsPage checkDeleteDirectDebitVictoryText();

    @CustomAction
    default DirectDebitsPage sleepRefreshAndCloseCookiesIfVisible(String directDebitName, String fromAccountNumber, String directDebitLimit) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        String sipoXpath = String.format(DIRECT_DEBIT_ROW_XPATH, directDebitName, fromAccountNumber, directDebitLimit);
        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;
    }

    @CustomAction
    default DirectDebitsPage checkDirectDebitIsDeletedAndRefreshIfNeeded(String sipoNumber) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        String sipoXpath = String.format(DIRECT_DEBIT_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, 30, 5);
        return null;
    }

    @TypeInto(value = DIRECT_DEBIT_NAME_INPUT_XPATH, clear = true)
    DirectDebitsPage setDirectDebitName(String directDebitName);

    @TypeInto(value = DIRECT_DEBIT_LIMIT_INPUT_XPATH, clear = true)
    DirectDebitsPage setAmountLimit(String directDebitLimit);

    @Click(value = CONFIRMATION_BTN_XPATH, jsClick = true)
    DirectDebitsPage confirmChangeDirectDebit();

    @CheckElementPresent(value = DIRECT_DEBIT_NAME_XPATH, isStringDynamicXpath = true)
    DirectDebitsPage checkDirectDebitName(String directDebitName);

    @CheckElementPresent(value = DIRECT_DEBIT_LIMIT_XPATH, isStringDynamicXpath = true)
    DirectDebitsPage checkDirectDebitLimit(String directDebitLimit);

    @CheckElementPresent(CHANGE_TITLE_VICTORY_PAGE_XPATH)
    DirectDebitsPage checkChangeDirectDebitVictoryText();
}