package cz.moneta.test.dsl.monetaportal.products;

import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.MonetaPortalEndpoint;
import cz.moneta.test.harness.exception.HarnessException;
import cz.moneta.test.harness.support.web.*;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.WebElement;

import java.util.List;
import java.util.regex.Pattern;

@Wait(value = InvestmentFundDetailPage.CHART_CURVE, until = Until.VISIBLE)
@Wait(value = InvestmentFundDetailPage.FUND_CHART_PERIOD_UNSELECTED_BUTTON, until = Until.VISIBLE)
public interface InvestmentFundDetailPage extends WebFlow<InvestmentFundDetailPage>, StoreAccessor {
    String FUND_CHART_WIDGET = "//mutual-fund-chart-widget";
    String CHART_CURVE = FUND_CHART_WIDGET + "//*[contains(@class,'recharts-curve')]";
    String FUND_CHART_PERIOD_UNSELECTED_BUTTON = FUND_CHART_WIDGET + "//button[@aria-selected='false']";
    String FIRST_CHART_DATE = FUND_CHART_WIDGET + "//*[contains(@class, 'xAxis')]//*[@class='recharts-text recharts-cartesian-axis-tick-value']/*"; //tags that cannot be found using xpath (g,text,tspan)
    String CHART_DATE_ELEMENT_WITH_SPECIFIC_DATE = FIRST_CHART_DATE + "[text()='%s']";
    String REGEX_DATE_PATTERN = "\\d{2}\\.\\d{2}\\.\\d{4}";
    String WRONG_FORMAT_MESSAGE_PREFIX = "Date format does not match dd.MM.yyyy: ";

    @GetElementContent(FIRST_CHART_DATE)
    String getFirstChartDate();

    private void selectPeriodAndWaitForChange(int unselectedPeriodButtonNumber) {
        MonetaPortalEndpoint e = getEndpoint(MonetaPortalEndpoint.class);
        String originalDate = getFirstChartDate();
        Assertions.assertTrue(Pattern.matches(REGEX_DATE_PATTERN, originalDate),
                WRONG_FORMAT_MESSAGE_PREFIX + originalDate);
        String originalDateXpath = String.format(CHART_DATE_ELEMENT_WITH_SPECIFIC_DATE, originalDate);
        e.waitForElementsToLoad(0,Until.VISIBLE, originalDateXpath); //checking if the originalDateXpath works
        e.click(() -> FUND_CHART_PERIOD_UNSELECTED_BUTTON + "[" + unselectedPeriodButtonNumber + "]");
        e.waitForElementsToLoad(5, Until.GONE, originalDateXpath); //checking if the element with originalDateXpath at least disappears
        String dateAfterChange = getFirstChartDate();
        Assertions.assertNotEquals(originalDate, dateAfterChange, "The start date of the period remained the same");
        Assertions.assertTrue(Pattern.matches(REGEX_DATE_PATTERN, dateAfterChange),
                WRONG_FORMAT_MESSAGE_PREFIX + dateAfterChange);
    }

    @CustomAction
    default InvestmentFundDetailPage verifyChartBehavior() {
        MonetaPortalEndpoint e = getEndpoint(MonetaPortalEndpoint.class);
        List<WebElement> periodButtons = e.findAndGetListOfElements(FUND_CHART_PERIOD_UNSELECTED_BUTTON, Lookup.XPATH);
        if (periodButtons.isEmpty()) {
            throw new HarnessException("Missing period buttons (xpath: \"" + FUND_CHART_PERIOD_UNSELECTED_BUTTON + "\")");
        }
        for (int i = 1; i <= periodButtons.size(); i++) {
            selectPeriodAndWaitForChange(i);
        }
        return null;
    }


}
