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

import cz.moneta.test.dsl.Harness;
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.payments.MobileOperatorsPaymentPage;
import cz.moneta.test.dsl.newib.payments.NewPaymentDomesticPage;
import cz.moneta.test.dsl.newib.settings.payments.standingorders.StandingOrdersPage;
import cz.moneta.test.dsl.newib.settings.payments.templates.TemplatesPage;
import cz.moneta.test.dsl.monetaportal.savings.childbuildingsavings.CalculatorPage;
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.mainpage.DashboardPage.*;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Wait(value = {PAGE_TITLE, OVERVIEW_PAGE_TITLE, THEME_SWITCHER_XPATH}, waitSecondsForElement = 60)
public interface DashboardPage extends NewIbPageFlow<DashboardPage>, HorizontalMainMenu, VerticalMainMenu, StoreAccessor {
    String PAGE_TITLE = "//title[contains(text(),'Internet Banka - MONETA Money Bank')]";
    String OVERVIEW_PAGE_TITLE = "//h1[contains(text(),'Přehled')]";
    String THEME_SWITCHER_XPATH = "//div[@data-testid='themeSwitcherNotification']";
    String STANDING_ORDERS_BTN_XPATH = "(//button[@class='c-btn c-btn--small c-btn--border c-btn--primary c-compact-buttons__button'][text()='Trvalé příkazy'])[1]";
    String NEW_PAYMENT_BTN_XPATH = "(//button[starts-with(@data-testid,'account-new-payment')][text()='Zaplatit'])[1]";
    String TEMPLATES_BTN_XPATH = "(//span[@class='c-icon c-icon--l c-icon--clipboardArrowOut'][text()='Šablony'])[1]";
    String CREDIT_TOP_UP_PAYMENT_BTN_XPATH = "(//button[@class='c-btn c-btn--small c-btn--border'][text()='Dobít kredit'])[1]";
    String INCOME_EXPENSES_OVERVIEW_XPATH = "//div[@class='c-card__header__text']//ancestor::h3[text()='Příjmy a výdaje']";
    String AVAILABLE_BALANCE_XPATH = "//div[@class='t-amountWithUnit t-amountWithUnit--xxLarge t-amountWithUnit--bold styles_productCard__ballance__2MYoY']";
    String AVAILABLE_BALANCE_VALUE_KEY = "AVAILABLE_BALANCE_VALUE";
    String HISTORY_BTN_XPATH = "(//button[@class='c-btn c-btn--small c-btn--border c-compact-buttons__button'][text()='Historie'])[1]";
    String APPLICATION_HEADER_TEXT_DIV_XPATH = "//div[@data-testid='commercial-loan-banner' and contains(normalize-space(),'podnikatelský úvěr')]";
    String CHECK_AND_SIGN_NEW_DOCUMENT_BUTTON_XPATH = "//button[@data-testid='documentForSign-notification-sign-btn']";
    String BANNER_TV_CZK = "//span[starts-with(text(), 'Termínovaný vklad s')]";
    String BANNER_TV_EUR = "//span[starts-with(text(), 'Termínovaný vklad v EUR')]";
    String BANNER_SU_EUR = "//span[starts-with(text(), 'EUR spořicí účet s úrokem')]";
    String CARDS_BTN_XPATH = "//li[@data-testid='cards-link']";
    String RTV_CARD = "//small[contains(., '%s')]";
    String PENSION_CARD_XPATH = "//small[contains(., 'Doplňkové penzijní spoření')]";
    String PENSION_CARD_SAVED_TEXT_XPATH = "//*[@data-testid='TextComponent']//small[normalize-space(.)='Naspořeno']";
    String USE_MOBILE_KEY_BUTTON_XPATH = "//button[@data-testid='buttonUseMobileKey']";
    String CHECK_BOX_MOBILE_KEY_XPATH = "//span[@class='f-checkbox__indicator']";
    String CONFIRMATION_MOBILE_KEY_XPATH = "//button[@data-testid='confirmButton']";
    String MOBILE_KEY_INPUT = "//label[text()='Mobilní klíč']/following::input[@id='mobileKey']";
    String FINAL_MOBILE_KEY_CONFIRM_XPATH = "//button[@data-testid='secondFactorSend']";

    @Click(CHECK_AND_SIGN_NEW_DOCUMENT_BUTTON_XPATH)
    SigningModalPage clickCheckAndSignNewDocument();

    @Click(STANDING_ORDERS_BTN_XPATH)
    StandingOrdersPage goToStandingOrders();

    @Click(value = APPLICATION_HEADER_TEXT_DIV_XPATH, jsClick = true)
    ApplicationPage clickApplicationHeaderText();

    @CheckElementPresent(STANDING_ORDERS_BTN_XPATH)
    DashboardPage checkStandingOrdersBtnIsPresent();

    @CheckElementPresent(OVERVIEW_PAGE_TITLE)
    DashboardPage checkLoadDashboard();

    @CheckElementPresent(HISTORY_BTN_XPATH)
    DashboardPage checkHistoryBtnIsPresent();

    @Click(NEW_PAYMENT_BTN_XPATH)
    NewPaymentDomesticPage goToNewPayment();

    @CheckElementPresent(NEW_PAYMENT_BTN_XPATH)
    DashboardPage checkNewPaymentBtnIsPresent();

    @Click(TEMPLATES_BTN_XPATH)
    TemplatesPage goToTemplates();

    @CheckElementPresent(TEMPLATES_BTN_XPATH)
    DashboardPage checkTemplatesBtnIsPresent();

    @Click(CREDIT_TOP_UP_PAYMENT_BTN_XPATH)
    MobileOperatorsPaymentPage goToCreditTopUpPayment();

    @CheckElementPresent(CREDIT_TOP_UP_PAYMENT_BTN_XPATH)
    DashboardPage checkCreditTopUpBtnIsPresent();

    @CheckElementPresent(THEME_SWITCHER_XPATH)
    DashboardPage checkThemeSwitcherIsPresent();

    @CheckElementPresent(INCOME_EXPENSES_OVERVIEW_XPATH)
    DashboardPage checkIncomeExpensesOverviewIsPresent();

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

    @StoreElementContent(storeKey = AVAILABLE_BALANCE_VALUE_KEY, value = AVAILABLE_BALANCE_XPATH)
    DashboardPage getAvailableBalance();

    @CustomAction
    default CalculatorPage goToChildBuildingSavings(Harness harness) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.changeUrl(harness.getConfig("endpoints.new.ib.url") + "savings/buildingSavings/child/calculator");
        return null;
    }

    @Wait(value = BANNER_TV_CZK, waitSecondsForElement = 5)
    @CheckElementsPresent({
            @CheckElementPresent(value = BANNER_TV_CZK),
            @CheckElementPresent(value = BANNER_TV_EUR)})
    DashboardPage checkBannersPresent();

    @Click(CARDS_BTN_XPATH)
    CardsPage clickCards();

    @Click(value = RTV_CARD, isStringDynamicXpath = true)
    SavingsPage clickOnRtvCard(String fromAccount);

    @CheckElementPresent(PENSION_CARD_XPATH)
    DashboardPage checkPensionCardTitle();

    @CheckElementPresent(PENSION_CARD_SAVED_TEXT_XPATH)
    DashboardPage checkPensionCardSavedText();

    @Click(USE_MOBILE_KEY_BUTTON_XPATH)
    DashboardPage clickAgreeWithMobileKey();

    @Click(CHECK_BOX_MOBILE_KEY_XPATH)
    @Click(CONFIRMATION_MOBILE_KEY_XPATH)
    DashboardPage clickAcceptMobileKey();

    @TypeInto(MOBILE_KEY_INPUT)
    DashboardPage typeSmsKey(String smsKey);

    @Click(FINAL_MOBILE_KEY_CONFIRM_XPATH)
    DashboardPage clickFinalAgreeWithMobileKey();
}