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

import cz.moneta.test.dsl.newib.NewIbPageFlow;
import cz.moneta.test.dsl.newib.components.VerticalMainMenu;
import cz.moneta.test.dsl.newib.savings.DpsPage;
import cz.moneta.test.dsl.newib.savings.RtvServicingPage;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ib.NewIbEndpoint;
import cz.moneta.test.harness.support.web.CheckElementPresent;
import cz.moneta.test.harness.support.web.Click;
import cz.moneta.test.harness.support.web.CustomAction;
import cz.moneta.test.harness.support.web.Wait;

import static cz.moneta.test.dsl.newib.mainpage.SavingsPage.*;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Wait({SAVINGS_TITLE_XPATH})
public interface SavingsPage extends NewIbPageFlow<SavingsPage>, StoreAccessor, VerticalMainMenu {

    String SAVINGS_TITLE_XPATH = "//h1[@class='t-title t-title--h5 u-ml--small'][text()='Spoření']";
    String YOU_MIGHT_BE_INTERESTED_XPATH = "//h1[@class='t-title t-title--h5 u-ml--small'][text()='Výhodné nabídky pro Vás']";
    String BUILDING_SAVINGS_PRODUCT_CARD_XPATH = "//div[@data-testid='newProductCard-buildingSavings']//small[text()='Stavební spoření']";
    String SAVING_ACCOUNT_PRODUCT_CARD_XPATH = "//div[@data-testid='newProductCard-savingAccount']";
    String TERM_DEPOSIT_CZK_BANNER = "//span[starts-with(text(), 'Termínovaný vklad s')]";
    String TERM_DEPOSIT_EUR_USD_BANNER = "//span[starts-with(text(), 'Termínovaný vklad v EUR i USD')]";
    String SAVING_ACCOUNT_CZK_BANNER = "//small[text()='Spořicí účet']";
    String RTV_DEPOSIT_BUTTON = "//button[@aria-label='Vložit peníze']";
    String RTV_DEPOSIT_YELLOW_ALERT_TEXT_XPATH = "//div[@class='c-alert__text' and text()='Aby vklad proběhl, mějte poslední pracovní den před dnem obnovy do 20:00 dostatečný zůstatek na účtu.']";
    String RTV_DEPOSIT_BLUE_ALERT_TEXT_XPATH = "//div[@class='c-alert__text'][contains(., 'Vklad na RTV,') and contains(., '10') and contains(., '000') and contains(., 'CZK') and contains(., 'Můžete to udělat nejpozději poslední pracovní den před dnem obnovy do 20:00.')]";
    String RTV_SERVICING_STORNO_BUTTON = "//button[@data-testid='rtv-waiting-action-link' and text()='Chcete žádost zrušit?']";
    String RTV_WITHDRAWAL_BUTTON = "//button[@aria-label='Vybrat peníze']";
    String RTV_WITHDRAWAL_BLUE_ALERT_TEXT_XPATH = "//div[@class='c-alert__text'][contains(., 'Výběr z RTV,') and contains(., '10') and contains(., '000') and contains(., 'CZK') and contains(., 'Můžete to udělat nejpozději poslední pracovní den před dnem obnovy do 20:00.')]";
    String RTV_CANCEL_BUTTON = "//button[@aria-label='Zrušit']";
    String PENSION_BUTTON_XPATH = "//div[@class='u-fontWeightBold' and text()='Penze']";

    @CustomAction
    default SavingsPage checkUrl() {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        String actualUrl = newIbEndpoint.getCurrentUrl();
        String urlContains = "savings";
        assertTrue(actualUrl.contains(urlContains));
        return null;
    }

    @CheckElementPresent(SAVING_ACCOUNT_PRODUCT_CARD_XPATH)
    SavingsPage checkSavingAccountProductCardIsPresent();

    @CheckElementPresent(BUILDING_SAVINGS_PRODUCT_CARD_XPATH)
    SavingsPage checkBuildingSavingsProductCardIsPresent();

    @CustomAction
    default TermDepositPage clickOnCreateTvForCurrency(String currency) {
        if ("CZK".equals(currency)) {
            return clickOnCreateTvCzk();
        } else if ("EUR".equals(currency) || "USD".equals(currency)) {
            return clickOnCreateTvEurUsd();
        }
        throw new IllegalArgumentException("Unsupported currency: " + currency);
    }

    @Click(TERM_DEPOSIT_CZK_BANNER)
    TermDepositPage clickOnCreateTvCzk();

    @Click(TERM_DEPOSIT_EUR_USD_BANNER)
    TermDepositPage clickOnCreateTvEurUsd();

    @Click(BUILDING_SAVINGS_PRODUCT_CARD_XPATH)
    BuildingSavingsPage clickOnBuildingSavingsButton();

    @Click(RTV_DEPOSIT_BUTTON)
    RtvServicingPage clickOnRtvDepositButton();

    @CheckElementPresent(RTV_DEPOSIT_YELLOW_ALERT_TEXT_XPATH)
    SavingsPage checkRtvDepositYellowAlertText();

    @CheckElementPresent(RTV_DEPOSIT_BLUE_ALERT_TEXT_XPATH)
    SavingsPage checkRtvDepositBlueAlertText();

    @Click(value = RTV_SERVICING_STORNO_BUTTON)
    RtvServicingPage clickOnStornoButton();

    @Click(RTV_WITHDRAWAL_BUTTON)
    RtvServicingPage clickOnRtvWithdrawalButton();

    @CheckElementPresent(RTV_WITHDRAWAL_BLUE_ALERT_TEXT_XPATH)
    SavingsPage checkRtvWithdrawalBlueAlertText();

    @Click(RTV_CANCEL_BUTTON)
    RtvServicingPage clickOnRtvCancelButton();

    @Click(PENSION_BUTTON_XPATH)
    DpsPage clickOnPensionButton();
}
