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.investments.InvestmentsPurchaseDialog;
import cz.moneta.test.dsl.newib.investments.InvestmentsQuestionnaire;
import cz.moneta.test.dsl.newib.investments.InvestmentsSaleDialog;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ib.NewIbEndpoint;
import cz.moneta.test.harness.support.web.*;
import org.openqa.selenium.WebElement;
import java.util.List;
import java.util.Random;

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

//@Wait({DISCLAIMER_CONFIRMATION_BTN_XPATH})
public interface InvestmentsPage extends NewIbPageFlow<InvestmentsPage>, StoreAccessor, VerticalMainMenu {

    String DISCLAIMER_CONFIRMATION_BTN_XPATH = "//button[@data-testid='confirm'][text()='Rozumím']";
    String INVESTMENTS_TITLE_XPATH = "//h2[@class='t-title t-title--h5'][text()='Přehled našich produktů']";
    String INVESTMENTS_HEADER_XPATH = "//h1[@class='t-title t-title--h3 u-mb--xLarge u-ml--0'][text()='Jaký typ investice vás zajímá?']";
    String INVESTMENTS_PRODUCT_PROMO_XPATH = "//div[@data-testid='newProductCard-promo']";
    String MENU_SHORT_TERM_FUNDS = "//*[./*[text()='Krátkodobější investice, stabilnější fondy']]";
    String MENU_MEDIUM_TERM_FUNDS = "//*[./*[text()='Střednědobé investice, vyvážené fondy']]";
    String MENU_LONG_TERM_FUNDS = "//*[./*[text()='Dlouhodobé investice, kolísavé fondy']]";
    String MENU_INVESTMENT = "//div[contains(@data-testid, 'sidebar-fund-')]";
    String INVEST_BUTTON = "//button[@data-testid='investBtn']";
    String RECOMMENDATION_CATEGORIES = "//li[@data-testid='MenuItem' and contains(@class,'m-menu__item') and .//ul/li]/div";
    String RECOMMENDATION_CATEGORIES_NOT_OPEN = "//li[@data-testid='MenuItem' and @class = 'm-menu__item' and .//ul/li]/div";
    String INVESTMENT_BY_PROFILE = "//div[contains(@data-testid, 'sidebar-fund-') and .//span[@class='c-avatar__text' and text()='%s']]";
    String INVESTMENT_BY_ISIN = "//div[@data-testid='sidebarFund-%s']";
    String FUND_RISK_PROFILE_NUMBER = "//div[contains(@data-testid, 'sidebar-fund-')]//span[@class='c-avatar__text' and not(text()='')]";
    String YOUR_INVESTMENT_OPPORTUNITY_MENU_ITEM = "//div[@class = 'c-card__header c-card__collapseTrigger']";
    String MY_PORTFOLIO = "//div[@data-testid='sidebar-investments']";
    String SALE_BUTTON = "//button[@data-testid='saleInvestment']";
    String INVESTMENTS_CARD = "//div[@data-testid='investments-card']";

    @Click(INVESTMENTS_CARD)
    InvestmentsPage clickInvestmentsCard();

    @Click(DISCLAIMER_CONFIRMATION_BTN_XPATH)
    InvestmentsPage confirmDisclaimer();

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

    @CheckElementPresent(INVESTMENTS_TITLE_XPATH)
    InvestmentsPage checkInvestmentsTitleIsVisible();

    @CheckElementPresent(INVESTMENTS_HEADER_XPATH)
    InvestmentsPage checkInvestmentsHeaderIsVisible();

    @CheckElementPresent(INVESTMENTS_PRODUCT_PROMO_XPATH)
    InvestmentsPage checkInvestmentsProductPromoIsVisible();

    @Click(MENU_SHORT_TERM_FUNDS)
    InvestmentsPage clickShortTermCategory();

    @Click(MENU_MEDIUM_TERM_FUNDS)
    InvestmentsPage clickMediumTermCategory();

    @Click(MENU_LONG_TERM_FUNDS)
    InvestmentsPage clickLongTermCategory();

    @Click(MENU_INVESTMENT)
    InvestmentsPage clickInvestment();

    @Click(INVEST_BUTTON)
    InvestmentsQuestionnaire clickGoInvestAndFillQuestionnaire();

    @Click(INVEST_BUTTON)
    InvestmentsPurchaseDialog clickGoInvest();

    @CustomAction
    default InvestmentsPage uncoverEntireFundList() {
        NewIbEndpoint ep = getEndpoint(NewIbEndpoint.class);
        ep.waitForElementsToLoad(5, RECOMMENDATION_CATEGORIES);
        List<WebElement> closedMenuCategories = ep.findAndGetListOfElements(RECOMMENDATION_CATEGORIES_NOT_OPEN, Lookup.XPATH);
        closedMenuCategories.forEach(e -> {
            waitForMenu(); //there are some animations so it has to wait
            ep.moveToElement(RECOMMENDATION_CATEGORIES_NOT_OPEN);
            e.click();
        });
        return null;
    }

    @Click(value = INVESTMENT_BY_PROFILE, isStringDynamicXpath = true)
    InvestmentsPage clickInvestmentByProfile(String profileNumber);

    @Click(value = INVESTMENT_BY_ISIN, isStringDynamicXpath = true)
    InvestmentsPage clickInvestmentByIsin(String isin);

    @CustomAction
    default InvestmentsPage clickRandomInvestment(){
        uncoverEntireFundList();
        NewIbEndpoint ep = getEndpoint(NewIbEndpoint.class);
        int fundCount = ep.findAndGetListOfElements(MENU_INVESTMENT, Lookup.XPATH).size();
        int randomNumber = new Random().nextInt(fundCount) + 1;
        ep.click(()->"(" + MENU_INVESTMENT + ")["+ randomNumber +"]");
        return null;
    }

    @Wait(value = YOUR_INVESTMENT_OPPORTUNITY_MENU_ITEM, explicitWaitSeconds = 2)
    InvestmentsPage waitForMenu();

    @Click(MY_PORTFOLIO)
    InvestmentsPage clickMyPortfolio();

    @Click(SALE_BUTTON)
    InvestmentsSaleDialog clickSale();
}
