package cz.moneta.test.dsl.ib.welcome;

import cz.moneta.test.dsl.ib.IbPageFlow;
import cz.moneta.test.dsl.ib.cards.application.CardDetailsOfferBanner;
import cz.moneta.test.dsl.ib.components.MainMenu;
import cz.moneta.test.dsl.ib.components.UserMenu;
import cz.moneta.test.dsl.ib.loan.ProductSelectorPage;
import cz.moneta.test.dsl.ib.loan.creditcard.CCProductConfiguratorPage;
import cz.moneta.test.dsl.ib.settings.ib.SettingsPage;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.WebEndpoint;
import cz.moneta.test.harness.endpoints.ib.IbEndpoint;
import cz.moneta.test.harness.support.util.Level;
import cz.moneta.test.harness.support.web.*;

import java.util.concurrent.TimeUnit;

import static cz.moneta.test.dsl.ib.welcome.WelcomePage.SETTINGS_PAGE_LINK;

@Wait(value = SETTINGS_PAGE_LINK, waitSecondsForElement = 90)
public interface WelcomePage extends IbPageFlow<WelcomePage>, MainMenu, CardDetailsOfferBanner<WelcomePage>, StoreAccessor, UserMenu {

    String WELCOME_PAGE_HOME_IMAGE = "//div[@id='document']//img[contains(@src, 'images_new/home_down')]";
    String VALIDATE_MY_LOGIN_BUTTON = "//span[@testid='open-modal-button']";
    String CAT_BANNER = "//*[@id='x31809652']/div[2]/div[2]/div[4]";
    String COMMERCIAL_LOAN_BANNER = "//div[contains(@class,'salesBanner')]";
    String INTERESTED_IN_COMMERCIAL_LOAN_BUTTON = "//span[contains(@class,'salesStartButton')]";
    String COMMERCIAL_CREDIT_CARD_BANNER = "//div[contains(@class,'salesBanner-multi-creditCard benefit-reward')]";
    String INTERESTED_IN_COMMERCIAL_CARD_BUTTON = "//button[contains(@class,'startSalesProcess')]";
    String BANNER_DIV = "//div[contains(@class,'banner')]";
    String CLOSE_DIV = "//div[@data-event='closeWindow']";
    String OVERLAY_DIV = "//div[@class='curtain']";
    String OVERLAY_INACTIVE_DIV = "//div[contains(@class,'curtain inactive')]";
    String COOKIES_POP_XPATH = "//div[@id='cookiesBar']";
    String COOKIES_BUTTON_XPATH = "//span[@id='acceptButton']";
    String DISPO_CURRENT_ACCOUNT_XPATH = "//td[@class='accName' and contains(text(), 'Běžný účet')]/following-sibling::td[@class='dispoAmount']";
    String DISPO_SAVING_ACCOUNT_XPATH = "//td[@class='accName' and contains(text(), 'Spořicí účet')]/following-sibling::td[@class='dispoAmount']";
    String CURRENT_ACCOUNT_NUMBER = "//td[contains(text(), 'Běžný účet')]";
    String NAME_WELCOME_PAGE = "//img[@class='avatarImg']/following-sibling::span";
    String CURRENT_ACCOUNT_DISPO_KEY = "CURRENT_ACCOUNT_DISPO";
    String SAVING_ACCOUNT_DISPO_KEY = "SAVING_ACCOUNT_DISPO";
    String MY_NAME_KEY = "MY_NAME";

    //Handling of random displayed banner. We do not test the banner so this solution is feasible.
    default void loadPage(WebEndpoint endpoint) {
        if (endpoint.isElementPresent(OVERLAY_DIV, Lookup.DEFAULT)) {
            endpoint.click(() -> CLOSE_DIV);
            endpoint.waitForElementsToLoad(10, Until.GONE, BANNER_DIV);
            endpoint.sleep(TimeUnit.MILLISECONDS, 200);
            endpoint.waitForElementsToLoad(10, OVERLAY_INACTIVE_DIV);
            log(Level.DEBUG, "Banner with AD is displayed and then closed");
        }
    }

    @CheckElementPresent(WELCOME_PAGE_HOME_IMAGE)
    WelcomePage checkWelcomePageImageIsPresent();

    @Click(VALIDATE_MY_LOGIN_BUTTON)
    LoginToBankModalPage openLoginToBankModal();

    @Click(CAT_BANNER)
    WelcomePage dismissAnnoyingCatBanner();

    @Click(value = "acceptButton", by = Lookup.ID)
    WelcomePage consentToCookies();

    @Wait(value = SETTINGS_PAGE_LINK, until = Until.VISIBLE)
    @Click(SETTINGS_PAGE_LINK)
    SettingsPage goToSettings();

    @CheckElementPresent(COMMERCIAL_LOAN_BANNER)
    WelcomePage checkUserHasALoanBanner();

    @Click(INTERESTED_IN_COMMERCIAL_LOAN_BUTTON)
    ProductSelectorPage clickInterestedInLoan();

    @CheckElementPresent(COMMERCIAL_CREDIT_CARD_BANNER)
    WelcomePage checkUserHasACommercialCreditCardBanner();

    @Click(INTERESTED_IN_COMMERCIAL_CARD_BUTTON)
    CCProductConfiguratorPage clickInterestedInCard();

    @CustomAction
    default WelcomePage checkAndConfirmCookies() {
        IbEndpoint endpoint = getEndpoint(IbEndpoint.class);
        if (endpoint.isElementPresent(COOKIES_POP_XPATH, Lookup.XPATH))
            endpoint.click(() -> COOKIES_BUTTON_XPATH);
        return null;
    }

    @StoreElementContent(storeKey = SAVING_ACCOUNT_DISPO_KEY, value = DISPO_SAVING_ACCOUNT_XPATH)
    WelcomePage getDispoSavingAccount();

    @StoreElementContent(storeKey = CURRENT_ACCOUNT_DISPO_KEY, value = DISPO_CURRENT_ACCOUNT_XPATH)
    WelcomePage getDispoCurrentAccount();

    @StoreElementContent(storeKey = MY_NAME_KEY, value = NAME_WELCOME_PAGE)
    WelcomePage getNameFromWelcomePage();
}