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

import cz.moneta.test.dsl.newib.NewIbPageFlow;
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.payments.NewForeignPaymentPage.*;

@Wait(value = {CURRENCY_BTN_XPATH, AMOUNT_INPUT_XPATH}, waitSecondsForElement = 30)
public interface NewForeignPaymentPage extends NewIbPageFlow<NewForeignPaymentPage>, NewPaymentMenuPage, StoreAccessor {
    String IBAN_INPUT_XPATH = "//input[@id='recipientAccountNumber']";
    String CURRENCY_BTN_XPATH = "//button[@name='currencyCode']";
    String CURRENCY_OPTION_XPATH = "//div[@class='c-dropdown__itemContent'][text()='%s']";
    String CURRENCY_OPTION_SELECTED_XPATH = "//button[@data-value='%s']";
    String AMOUNT_INPUT_XPATH = "//input[@id='amount']";
    String RECIPIENT_BANK_BIC_INPUT_XPATH = "//input[@id='recipientBankBic']";
    String RECIPIENT_BANK_NAME_INPUT_XPATH = "//input[@id='recipientBankName']";
    String RECIPIENT_BANK_ADDRESS_STREET_INPUT_XPATH = "//input[@id='recipientBankAddressStreet']";
    String RECIPIENT_BANK_COUNTRY_INPUT_XPATH = "//input[@name='recipientBankCountry']";
    String RECIPIENT_NAME_INPUT_XPATH = "//input[@id='recipientName'][not(@disabled)]";
    String RECIPIENT_ADDRESS_STREET_INPUT_XPATH = "//input[@id='recipientAddressStreet']";
    String RECIPIENT_ADDRESS_CITY_INPUT_XPATH = "//input[@id='recipientAddressCity']";
    String RECIPIENT_ADDRESS_COUNTRY_BUTTON_XPATH = "//button[@name='recipientAddressCountry']";
    String RECIPIENT_MESSAGE_INPUT_XPATH = "//input[@name='recipientNote']";
    String SEND_PAYMENT_CONFIRMATION_EMAIL_CHECKBOX_XPATH = "//label[@for='confirmationEmailCheckbox']";
    String CONFIRMATION_EMAIL_INPUT_XPATH = "//input[@id='confirmationEmail']";
    String CONTINUE_BUTTON_XPATH = "//button[@data-testid='submit']";
    String BIC_VALUE_XPATH = "//input[@id='recipientBankBic' and @value='%s']";
    String BANK_NAME_VALUE_XPATH = "//input[@id='recipientBankName' and @value='%s']";
    String BANK_STREET_VALUE_XPATH = "//input[@id='recipientBankAddressStreet' and @value='%s']";
    String BANK_CITY_VALUE_XPATH = "//input[@id='recipientBankAddressCity' and @value='%s']";
    String BANK_COUNTRY_VALUE_XPATH = "//button[@id='recipientBankCountry_button']/span[1]/span/span";
    String VALIDATION_CURRENCY_IS_NOT_ACCEPTED_XPATH = "//*[@id='currencyCode_errorMessage'][text()='%s']";
    String RECIPIENT_BANK_COUNTRY_BUTTON_DISABLED_XPATH = "//*[@id='recipientBankCountry_button'][@disabled]";
    String ALERT_CZ_IBAN_XPATH = "//strong[normalize-space(.)='%s']";
    String REDIRECT_TO_DOMESTIC_PAYMENT_BUTTON_XPATH = "//button[@data-testid='redirectToCzechPaymentFormBtn']";
    String RECEPIENT_BANKA_BIC_XPATH = "//input[@data-testid='Component' and @id='recipientBankBic' and normalize-space(@value) != '']";
    String ERROR_IBAN_XPATH = "//div[@id='recipientAccountNumber_errorMessage'][text()='%s']";
    String BIC_DISABLED_XPATH = "//*[@id='recipientBankBic'][@disabled]";
    String RECIPIENT_BANK_NAME_DISABLED_XPATH = "//*[@id='recipientBankName'][@disabled]";
    String RECIPIENT_BANK_ADDRESS_STREET_DISABLED_XPATH = "//*[@id='recipientBankAddressStreet'][@disabled]";
    String RECIPIENT_BANK_ADDRESS_CITY_DISABLED_XPATH = "//*[@id='recipientBankAddressCity'][@disabled]";
    String TYPE_OF_PAYMENT_XPATH = "//button[@id='paymentPriority_button']";
    String CHOOSE_TYPE_OF_PAYMENT_XPATH = "//button[@id='paymentPriority_button']//span[contains(@class, 'f-control2__valueTitle')]";
    String OPTION_TYPE_OF_PAYMENT_XPATH = "//li[@role='option' and not(@disabled) and .//div[text()='%s']]";
    String FEE_FOREIGN_PRIORITY_PAYMENT_XPATH = "//p[.//small[contains(text(), 'Odeslání prioritní platby')] and .//strong[text()='%s'] and contains(., 'CZK')]";
    String RECIPIENT_ADDRESS_COUNTRY_OPTION_XPATH = "//div[@class='c-dropdown__itemContent'][text()='%s']";
    String CHARGE_BEARER_VALUE_XPATH = "//*[@id='chargeBearer_button' and contains(., '%s')]";
    String CHARGE_BEARER_BUTTON_DISABLED_XPATH = "//*[@id='chargeBearer_button'][@disabled]";
    String ESTIMATED_AMOUNT_XPATH = "//div[@data-testid='TextComponent']/small[contains(., 'Orientační částka transakce je') and contains(., '%s') and contains(., 'CZK')]";
    String SUBMIT_XPATH = "//span[@data-testid='TextComponent']//small[contains(., 'Odešlete o') and contains(., '%s') and contains (., 'více oproti středovému kurzu ČNB')]";
    String CONVERSION_FEE_XPATH = "//div[@data-testid='TextComponent']/small[contains(., 'Poplatek za konverzi je') and contains(., '%s')]";
    String PAYMENT_FEE_XPATH = "//p[@data-testid='TextComponent']//small[contains(., 'Poplatek za platbu') and contains(., '%s')]";
    String SEPA_ALERT_XPATH = "//*[@data-testid='sepaAlert' and contains(., 'Platba bude odeslána v rámci SEPA')]";
    String CHANGED_IBAN_INPUT_XPATH = "//input[@id='recipientIban']";
    String CHARGE_BEARER_BUTTON_ENABLED_XPATH = "//*[@id='chargeBearer_button'][not(@disabled)]";
    String BIC_ENABLED_XPATH = "//*[@id='recipientBankBic'][not(@disabled)]";
    String RECIPIENT_BANK_NAME_ENABLED_XPATH = "//*[@id='recipientBankName'][not(@disabled)]";
    String RECIPIENT_BANK_ADDRESS_STREET_ENABLED_XPATH = "//*[@id='recipientBankAddressStreet'][not(@disabled)]";
    String RECIPIENT_BANK_ADDRESS_CITY_ENABLED_XPATH = "//*[@id='recipientBankAddressCity'][not(@disabled)]";
    String RECIPIENT_BANK_COUNTRY_BUTTON_ENABLED_XPATH = "//*[@id='recipientBankCountry_button'][not(@disabled)]";
    String RECIPIENT_IBAN_ERROR_MESSAGE_XPATH = "//*[@id='recipientAccountNumber_errorMessage'][text()='Neplatné číslo účtu ve formátu IBAN.']";
    String CHARGE_BEARER_BUTTON_XPATH = "//button[@id='chargeBearer_button']";
    String FEES_OPTION_XPATH = "//div[@class='c-dropdown__itemContent'][text()='%s']";
    String CHARGE_BEARER_ERROR_MESSAGE_XPATH = "//*[@id='chargeBearer_errorMessage'][text()='Pro platbu do této země lze použít pouze typ výloh SHA.']";
    String TOOLTIP_FEES_XPATH = "//label[@id='chargeBearer_label']//*[@role='tooltip']";
    String ALERT_OVERDRAFT_XPATH = "//div[@class='c-alert__text']//span[text()='Odesílaná částka je vyšší než zůstatek na účtu. Aby platba odešla, navyšte ho.']";
    String INVALID_IBAN_MODAL_XPATH = "//button[@data-testid='invalid-iban-modal__continue']";
    String MODAL_CZK_TO_CZK_XPATH = "//div[contains(@class, 'c-modal__content')][.//h1[contains(., 'Zadali jste platbu v korunách na účet v ČR')] and .//div[contains(@class, 'c-modal__body') and contains(., 'Takovou platbu je nejlepší poslat jako tuzemskou') and contains(., 'Pokud chcete odeslat platbu jako zahraniční, zvolte jinou měnu nebo IBAN / číslo účtu.')]]";
    String FOREIGN_TO_DOMESTIC_BUTTON_XPATH = "//button[@data-testid='foreign-to-domestic-modal__switch' and text()='Přejít na tuzemskou']";
    String ALERT_AMOUNT_MUST_BE_HIGHER = "//div[@class='c-alert__text']//span[text()='Odesílaná částka musí být vyšší než poplatek za platbu. Navyšte ji nebo změňte typ poplatku.']";

    @TypeInto(IBAN_INPUT_XPATH)
    NewForeignPaymentPage setRecipientAccountNumber(String recipientAccountNumber);

    @TypeInto(AMOUNT_INPUT_XPATH)
    NewForeignPaymentPage setAmount(String amount);

    @TypeInto(RECIPIENT_BANK_BIC_INPUT_XPATH)
    NewForeignPaymentPage setRecipientBankBic(String recipientBankBic);

    @TypeInto(RECIPIENT_BANK_NAME_INPUT_XPATH)
    NewForeignPaymentPage setRecipientBankName(String recipientBankName);

    @TypeInto(RECIPIENT_BANK_ADDRESS_STREET_INPUT_XPATH)
    NewForeignPaymentPage setRecipientBankAddressStreet(String recipientBankAddressStreet);

    @TypeInto(RECIPIENT_NAME_INPUT_XPATH)
    NewForeignPaymentPage setRecipientName(String recipientName);

    @TypeInto(RECIPIENT_ADDRESS_STREET_INPUT_XPATH)
    NewForeignPaymentPage setRecipientAddressStreet(String recipientAddressStreet);

    @TypeInto(RECIPIENT_ADDRESS_CITY_INPUT_XPATH)
    NewForeignPaymentPage setRecipientAddressCity(String recipientAddressCity);

    @TypeInto(CONFIRMATION_EMAIL_INPUT_XPATH)
    NewForeignPaymentPage setConfirmationEmail(String confirmationEmail);

    @Click(SEND_PAYMENT_CONFIRMATION_EMAIL_CHECKBOX_XPATH)
    NewForeignPaymentPage clickOnPaymentConfirmationEmailCheckbox();

    @CustomAction
    default NewForeignPaymentPage setRecipientBankCountry(String recipientBankCountry) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.type(() -> RECIPIENT_BANK_COUNTRY_INPUT_XPATH, recipientBankCountry, true);
        newIbEndpoint.sendKeysOneAtATime(Key.ENTER);
        return null;
    }

    @CustomAction
    default NewForeignPaymentPage setRecipientAddressCountry(String recipientAddressCountry) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> RECIPIENT_ADDRESS_COUNTRY_BUTTON_XPATH);
        newIbEndpoint.sleepSeconds(1);
        newIbEndpoint.click(() -> String.format(RECIPIENT_ADDRESS_COUNTRY_OPTION_XPATH, recipientAddressCountry));
        return null;
    }

    @CustomAction
    default NewForeignPaymentPage setCurrency(String currency) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> CURRENCY_BTN_XPATH);
        newIbEndpoint.sleepSeconds(1);
        newIbEndpoint.click(() -> String.format(CURRENCY_OPTION_XPATH, currency));
        newIbEndpoint.waitForElementsToLoad(5, String.format(CURRENCY_OPTION_SELECTED_XPATH, currency));
        return null;
    }

    @TypeInto(value = RECIPIENT_MESSAGE_INPUT_XPATH)
    NewForeignPaymentPage setRecipientMessage(String recipientMessage);

    @Wait(value = RECEPIENT_BANKA_BIC_XPATH, by = Lookup.XPATH)
    @Click(CONTINUE_BUTTON_XPATH)
    NewForeignPaymentSummaryPage clickOnContinue();

    @Click(CONTINUE_BUTTON_XPATH)
    NewForeignPaymentPage clickContinueAndStayOnSamePage();

    @CheckElementPresent(value = BIC_VALUE_XPATH, isStringDynamicXpath = true)
    NewForeignPaymentPage checkBic(String bankCodeBic);

    @CheckElementPresent(value = BANK_NAME_VALUE_XPATH, isStringDynamicXpath = true)
    NewForeignPaymentPage checkBankName(String bankNameAddress);

    @CheckElementPresent(value = BANK_STREET_VALUE_XPATH, isStringDynamicXpath = true)
    NewForeignPaymentPage checkStreet(String recipientBankAddressStreet);

    @CheckElementPresent(value = BANK_CITY_VALUE_XPATH, isStringDynamicXpath = true)
    NewForeignPaymentPage checkCity(String recipientBankCity);

    @CheckElementContent(BANK_COUNTRY_VALUE_XPATH)
    NewForeignPaymentPage checkCountry(String country);

    @CheckElementPresent(value = VALIDATION_CURRENCY_IS_NOT_ACCEPTED_XPATH, isStringDynamicXpath = true)
    NewForeignPaymentPage checkValidationCurrencyIsNotAccepted(String validation);

    @CheckElementPresent(RECIPIENT_BANK_COUNTRY_BUTTON_DISABLED_XPATH)
    NewForeignPaymentPage checkDisabledRecipientBankCountryButton();

    @CheckElementPresent(value = ALERT_CZ_IBAN_XPATH, isStringDynamicXpath = true)
    NewForeignPaymentPage checkAlertCzIban(String alertCzIban);

    @Click(REDIRECT_TO_DOMESTIC_PAYMENT_BUTTON_XPATH)
    NewPaymentDomesticPage clickRedirectToDomesticPayment();

    @CheckElementPresent(value = ERROR_IBAN_XPATH, isStringDynamicXpath = true)
    NewForeignPaymentPage checkErrorIban(String errorIban);

    @CheckElementPresent(BIC_DISABLED_XPATH)
    NewForeignPaymentPage checkBicDisabled();

    @Click(BANK_COUNTRY_VALUE_XPATH)
    NewForeignPaymentPage clickBankCountryValue();

    @CheckElementPresent(RECIPIENT_BANK_NAME_DISABLED_XPATH)
    NewForeignPaymentPage checkRecipientBankNameDisabled();

    @CheckElementPresent(RECIPIENT_BANK_ADDRESS_STREET_DISABLED_XPATH)
    NewForeignPaymentPage checkRecipientBankAddressStreetDisabled();

    @CheckElementPresent(RECIPIENT_BANK_ADDRESS_CITY_DISABLED_XPATH)
    NewForeignPaymentPage checkRecipientBankAddressCityDisabled();

    @CustomAction
    default NewForeignPaymentPage setTypeOfPayment(String name) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> TYPE_OF_PAYMENT_XPATH);
        newIbEndpoint.sleepSeconds(2);
        newIbEndpoint.click(() -> String.format(OPTION_TYPE_OF_PAYMENT_XPATH, name));
        newIbEndpoint.waitForElementsToLoad(5, String.format(CHOOSE_TYPE_OF_PAYMENT_XPATH, name));
        return null;
    }

    @CheckElementPresent(value = FEE_FOREIGN_PRIORITY_PAYMENT_XPATH, isStringDynamicXpath = true)
    NewForeignPaymentPage checkFeeForeignPriorityPayment(String feeForeignPriorityPayment);

    @CheckElementPresent(value = CHARGE_BEARER_VALUE_XPATH, isStringDynamicXpath = true)
    NewForeignPaymentPage checkChargeBearerValue(String chargeBearerValue);

    @CheckElementPresent(CHARGE_BEARER_BUTTON_DISABLED_XPATH)
    NewForeignPaymentPage checkDisabledChargeBearerButton();

    @CheckElementPresent(value = ESTIMATED_AMOUNT_XPATH, isStringDynamicXpath = true)
    NewForeignPaymentPage checkEstimatedAmountCurrency(String estimatedAmountCurrency);

    @CheckElementPresent(value = SUBMIT_XPATH, isStringDynamicXpath = true)
    NewForeignPaymentPage checkSubmitSourceCurrency(String submitSourceCurrency);

    @CheckElementPresent(value = CONVERSION_FEE_XPATH, isStringDynamicXpath = true)
    NewForeignPaymentPage checkConversionFeeCurrency(String conversionFeeCurrency);

    @CheckElementPresent(value = PAYMENT_FEE_XPATH, isStringDynamicXpath = true)
    NewForeignPaymentPage checkPaymentFeeCurrency(String paymentFeeCurrency);

    @CheckElementPresent(SEPA_ALERT_XPATH)
    NewForeignPaymentPage checkSepaAlert();

    @TypeInto(value = CHANGED_IBAN_INPUT_XPATH, clear = true)
    NewForeignPaymentPage fillChangedRecipientAccountNumber(String changedRecipientAccountNumber);

    @CheckElementPresent(CHARGE_BEARER_BUTTON_ENABLED_XPATH)
    NewForeignPaymentPage checkEnabledChargeBearerButton();

    @CheckElementPresent(BIC_ENABLED_XPATH)
    NewForeignPaymentPage checkBicEnabled();

    @CheckElementPresent(RECIPIENT_BANK_NAME_ENABLED_XPATH)
    NewForeignPaymentPage checkRecipientBankNameEnabled();

    @CheckElementPresent(RECIPIENT_BANK_ADDRESS_STREET_ENABLED_XPATH)
    NewForeignPaymentPage checkRecipientBankAddressStreetEnabled();

    @CheckElementPresent(RECIPIENT_BANK_ADDRESS_CITY_ENABLED_XPATH)
    NewForeignPaymentPage checkRecipientBankAddressCityEnabled();

    @CheckElementPresent(RECIPIENT_BANK_COUNTRY_BUTTON_ENABLED_XPATH)
    NewForeignPaymentPage checkEnabledRecipientBankCountryButton();

    @CheckElementPresent(RECIPIENT_IBAN_ERROR_MESSAGE_XPATH)
    NewForeignPaymentPage checkRecipientIbanErrorMessage();

    @TypeInto(value = IBAN_INPUT_XPATH, clear = true)
    NewForeignPaymentPage fillRecipientAccountNumber(String recipientAccountNumber);

    @CheckElementPresent(RECIPIENT_NAME_INPUT_XPATH)
    NewForeignPaymentPage checkRecipientName();

    @CustomAction
    default NewForeignPaymentPage scrollToTop() {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.scrollIntoView("//body");
        return null;
    }

    @CustomAction
    default NewForeignPaymentPage setFees(String fees) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> CHARGE_BEARER_BUTTON_XPATH);
        newIbEndpoint.sleepSeconds(2);
        newIbEndpoint.click(() -> String.format(FEES_OPTION_XPATH, fees));
        return null;
    }

    @CheckElementPresent(CHARGE_BEARER_ERROR_MESSAGE_XPATH)
    NewForeignPaymentPage checkChargeBearerErrorMessage();

    @CheckElementPresent(TOOLTIP_FEES_XPATH)
    NewForeignPaymentPage checkTooltipFees();

    @CheckElementPresent(ALERT_OVERDRAFT_XPATH)
    NewForeignPaymentPage checkAlertOverdraft();

    @Click(INVALID_IBAN_MODAL_XPATH)
    NewForeignPaymentPage clickOnInvalidIbanModal();

    @Click(AMOUNT_INPUT_XPATH)
    NewForeignPaymentPage clickOnAmount();

    @CheckElementPresent(MODAL_CZK_TO_CZK_XPATH)
    NewForeignPaymentPage checkModalCzkToCzk();

    @Click(FOREIGN_TO_DOMESTIC_BUTTON_XPATH)
    NewPaymentDomesticPage clickOnForeignToDomesticButton();

    @CheckElementPresent(ALERT_AMOUNT_MUST_BE_HIGHER)
    NewForeignPaymentPage checkAlertAmountMustBeHigher();

    @CustomAction
    default NewForeignPaymentPage scrollToBottom() {
        NewIbEndpoint ep = getEndpoint(NewIbEndpoint.class);
        ep.sendKeysOneAtATime(Key.DOWN);
        ep.sendKeysOneAtATime(Key.END);
        return null;
    }
}