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

import cz.moneta.test.dsl.ib.account.current.CurrentAccountPage;
import cz.moneta.test.dsl.ib.account.saving.SavingsAccountPage;
import cz.moneta.test.dsl.ib.authentication.LogoutSuccessfulPage;
import cz.moneta.test.dsl.ib.cards.CardsOverviewPage;
import cz.moneta.test.dsl.ib.insurance.InsurancePage;
import cz.moneta.test.dsl.ib.loans.IbLoansPage;
import cz.moneta.test.dsl.ib.mortgages.OverviewOfMortgagesPage;
import cz.moneta.test.dsl.ib.welcome.WelcomePage;
import cz.moneta.test.dsl.newib.mainpage.InvestmentsPage;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ib.IbEndpoint;
import cz.moneta.test.harness.support.web.*;

import java.util.List;

import static cz.moneta.test.dsl.ib.components.MainMenu.*;

@Wait({ACCOUNTS_AND_TRANSACTIONS_BUTTON, LOG_OFF_BUTTON})
public interface MainMenu extends Builder, StoreAccessor {

    String MENU_PARENT = "//div[@id='document']//a";
    String ACCOUNTS_AND_TRANSACTIONS_BUTTON = MENU_PARENT + "/img[@alt='Moje účty']";
    String SAVINGS_ACCOUNTS_BUTTON = MENU_PARENT + "/img[@alt='Spoření']";
    String CARDS_BUTTON = MENU_PARENT + "/img[@alt='karty']";
    String LOANS_BUTTON = MENU_PARENT + "/img[@alt='Půjčky']";
    String INSURANCE_BUTTON = MENU_PARENT + "/img[@alt='Pojištění']";
    String MORTGAGE_BUTTON = MENU_PARENT + "/img[@alt='Hypotéky']";
    String INVESTMENT_BUTTON = MENU_PARENT + "/img[@alt='Investování']";
    String LOG_OFF_BUTTON = MENU_PARENT + "/img[contains(@src, 'button_login.gif')]";
    String WELCOME_PAGE_BUTTON = MENU_PARENT + "//img[@alt='WELCOME PAGE']";
    String ITEM_IN_MENU_NOT_ACTIVE = MENU_PARENT + "//img[@alt='%s' and not(@onclick)]/parent::td";
    String ITEM_IN_MENU_ACTIVE = MENU_PARENT + "//img[@alt='%s' and @onclick]/parent::a";

    @Click(ACCOUNTS_AND_TRANSACTIONS_BUTTON)
    CurrentAccountPage clickAccountsAndTransactions();

    @Click(SAVINGS_ACCOUNTS_BUTTON)
    SavingsAccountPage clickSavings();

    @Click(CARDS_BUTTON)
    CardsOverviewPage clickCards();

    @Click(LOANS_BUTTON)
    IbLoansPage clickLoans();

    @Click(INSURANCE_BUTTON)
    InsurancePage clickInsurance();

    @Click(MORTGAGE_BUTTON)
    OverviewOfMortgagesPage clickMortgages();

    @Click(INVESTMENT_BUTTON)
    InvestmentsPage clickInvestments();

    @CustomAction
    default void clickLogOff() {
        IbEndpoint endpoint = this.getEndpoint(IbEndpoint.class);
        endpoint.click(() -> LOG_OFF_BUTTON);
        endpoint.waitForElementsToLoad(10, LogoutSuccessfulPage.PAGE_NAME);
        this.closeEndpoint(endpoint);
    }

    @Click(WELCOME_PAGE_BUTTON)
    WelcomePage clickToWelcomePageButton();

    @CustomAction
    default WelcomePage checkMainMenuItemsIsNotClickable(List<String> mainMenuItems) {
        IbEndpoint ibEndpoint = getEndpoint(IbEndpoint.class);
        mainMenuItems.forEach(s -> ibEndpoint.isElementPresent(String.format(ITEM_IN_MENU_NOT_ACTIVE, s), Lookup.XPATH));
        return null;
    }

    @CustomAction
    default WelcomePage checkMainMenuItemsIsClickable(List<String> mainMenuItems) {
        IbEndpoint ibEndpoint = getEndpoint(IbEndpoint.class);
        mainMenuItems.forEach(s -> ibEndpoint.isElementPresent(String.format(ITEM_IN_MENU_ACTIVE, s), Lookup.XPATH));
        return null;
    }
}