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

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

import static org.junit.jupiter.api.Assertions.assertTrue;

public interface ExchangePage extends NewIbPageFlow<ExchangePage>, VerticalMainMenu {

    String EXCHANGE_TITLE_TEXT_XPATH = "//h1 [@class='t-title t-title--h3'] [text()='Směnárna']";
    String FIRST_AMOUNT_INPUT_XPATH = "//input[@id='debitAmount']";
    String REQUEST_EXCHANGE_RATE_XPATH = "//button[@data-testid='submit']";
    String CONCLUDE_SALE_XPATH = "//button[@data-testid='submitExchange']";
    String CURRENCY_EXCHANGE_VICTORY_TITLE_XPATH = "//h2 [@class='t-title t-title--h5'] [text()='Směnný obchod byl odeslán ke zpracování']";
    String NEW_SALE_XPATH = "//button [@data-testid='back']";
    String SWAMP_ACCOUNTS_XPATH = "//button [@data-testid='swapCurrencies']";
    String SECOND_AMOUNT_INPUT_XPATH = "//input[@id='creditAmount']";
    String BACK_TO_OVERVIEW_XPATH = "//button [@data-testid='submit']";
    String FOREIGN_BANNER_TITLE_TEXT_XPATH = "//h2[@class='t-title t-title--h5 u-mb--xSmall'] [text()='Výhodný kurz %s s našim cizoměnovým účtem']";
    String NEW_FOREIGN_ACCOUNT_CREATE_XPATH = "//button[@data-testid='Button'] [text()='Založit účet']";
    String BUY_FOREIGN_CURRENCY_XPATH = "//h2[@class='t-title t-title--h5' and normalize-space(.)='Nákup %s']";
    String EXPIRED_ALERT_PRESENT = "//div[@class='c-alert__text'] [text()='Platnost nabízeného kurzu vypršela. Zažádejte si o nový kurz.']";
    String MINIMUM_DEBIT_AMOUNT_ALERT_PRESENT = "//div[@id='debitAmount_errorMessage']";
    String MINIMUM_CREDIT_AMOUNT_ALERT_PRESENT = "//div[@id='creditAmount_errorMessage']";
    String SALE_AMOUNT_VICTORY_PRESENT = "//p[@data-testid='TextComponent' and contains(., 'Prodáváte') and contains(., '2') and contains(., '000') and contains(., 'CZK')]";
    String BUY_AMOUNT_VICTORY_PRESENT = "//p[@data-testid='TextComponent' and contains(., 'nakupujete za ně') and contains(., '2') and contains(., '000,00') and contains(., 'CZK')]";
    String SELL_FOREIGN_CURRENCY_XPATH = "//h2[@class='t-title t-title--h5' and normalize-space(.)='Prodej %s']";
    String BUY_CURRENCY_AMOUNT_VICTORY_PRESENT = "//p[@data-testid='TextComponent']//strong[contains(., '200,00') and contains(., '%s')]";
    String SELL_CURRENCY_AMOUNT_VICTORY_PRESENT = "//p[@data-testid='TextComponent' and contains(., 'Prodáváte') and contains(., '200,00') and contains(., '%s')]";
    String VALIDITY_OFFERED_RATE_TEXT_XPATH = "//p[@data-testid='TextComponent']//small[contains(., 'Platnost nabízeného') and contains(., 'kurzu')]";
    String TO_ACCOUNT_BUTTON_XPATH = "//button[@id='creditAccountId_button']";
    String SELECT_FROM_ACCOUNT_XPATH = "//li[@role='option' and .//div[@class='c-dropdown__itemContent' and contains(., 'Cizoměnový  (EUR)')]]";

    @CustomAction
    default ExchangePage checkUrl() {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.switchToOtherWindow(5, 2);
        String actualUrl = newIbEndpoint.getCurrentUrl();
        String urlContains = "ucty-a-karty/cizomenovy-ucet";
        assertTrue(actualUrl.contains(urlContains));
        return null;
    }

    @CheckElementPresent(EXCHANGE_TITLE_TEXT_XPATH)
    ExchangePage checkExchangeTitleIsPresent();

    @TypeInto(value = FIRST_AMOUNT_INPUT_XPATH, clear = true)
    ExchangePage setFirstExchangeAmount(String firstAmount);

    @Click(REQUEST_EXCHANGE_RATE_XPATH)
    ExchangePage clickOnRequestExchangeRate();

    @Click(CONCLUDE_SALE_XPATH)
    ExchangePage clickOnConcludeSale();

    @CheckElementPresent(CURRENCY_EXCHANGE_VICTORY_TITLE_XPATH)
    ExchangePage checkVictoryScreenIsPresent();

    @Click(NEW_SALE_XPATH)
    ExchangePage clickOnNewSale();

    @Click(SWAMP_ACCOUNTS_XPATH)
    ExchangePage clickOnSwapAccounts();

    @TypeInto(value = SECOND_AMOUNT_INPUT_XPATH, clear = true)
    ExchangePage setSecondExchangeAmount(String secondAmount);

    @Click(BACK_TO_OVERVIEW_XPATH)
    ExchangePage clickOnBackToOverview();

    @CheckElementPresent(value = FOREIGN_BANNER_TITLE_TEXT_XPATH, isStringDynamicXpath = true)
    ExchangePage checkForeignTitleIsPresent(String currency);

    @Click(NEW_FOREIGN_ACCOUNT_CREATE_XPATH)
    NewForeignAccountPromoPage clickOnCreateAccount();

    @CheckElementPresent(value = BUY_FOREIGN_CURRENCY_XPATH, isStringDynamicXpath = true)
    ExchangePage checkBuyCurrency(String currency);

    @Wait(explicitWaitSeconds = 45)
    @CheckElementPresent(EXPIRED_ALERT_PRESENT)
    ExchangePage waitUntilTimerRunsOutCheckAlert();

    @CheckElementPresent(MINIMUM_DEBIT_AMOUNT_ALERT_PRESENT)
    ExchangePage checkMinimumDebitAmountAlert();

    @CheckElementPresent(MINIMUM_CREDIT_AMOUNT_ALERT_PRESENT)
    ExchangePage checkMinimumCreditAmountAlert();

    @CheckElementPresent(SALE_AMOUNT_VICTORY_PRESENT)
    ExchangePage checkSaleAmountIsPresent();

    @CheckElementPresent(BUY_AMOUNT_VICTORY_PRESENT)
    ExchangePage checkBuyAmountIsPresent();

    @CheckElementPresent(value = SELL_FOREIGN_CURRENCY_XPATH, isStringDynamicXpath = true)
    ExchangePage checkSellCurrency(String currency);

    @Click(value = REQUEST_EXCHANGE_RATE_XPATH)
    ExchangePage clickOnContinue();

    @CheckElementPresent(value = BUY_CURRENCY_AMOUNT_VICTORY_PRESENT, isStringDynamicXpath = true)
    ExchangePage checkBuyCurrencyAmountIsPresent(String currency);

    @CheckElementPresent(value = SELL_CURRENCY_AMOUNT_VICTORY_PRESENT, isStringDynamicXpath = true)
    ExchangePage checkSellCurrencyAmountIsPresent(String currency);

    @CheckElementPresent(VALIDITY_OFFERED_RATE_TEXT_XPATH)
    ExchangePage checkValidityOfferedRateIsPresent();

    @Click(TO_ACCOUNT_BUTTON_XPATH)
    ExchangePage clickOnTomAccount();

    @Click(SELECT_FROM_ACCOUNT_XPATH)
    ExchangePage selectToAccount();
}
