package cz.moneta.test.dsl.ufo.cb.landing;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.cb.landing.proposal.pages.DEL401_S001_Cru;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.exception.HarnessException;
import cz.moneta.test.harness.support.web.*;
import org.junit.jupiter.api.Assertions;

import static cz.moneta.test.dsl.ufo.cb.landing.AUT402_S001_KOCriteriaListPage.PAGE_LABEL_DIV;
import static cz.moneta.test.dsl.ufo.cb.landing.AUT402_S001_KOCriteriaListPage.LOADER_DIV;

@Wait(value = PAGE_LABEL_DIV, waitSecondsForElement = 120, until = Until.VISIBLE)
@Wait(value = LOADER_DIV, until = Until.GONE)
public interface AUT402_S001_KOCriteriaListPage extends WebFlow<AUT402_S001_KOCriteriaListPage>, StoreAccessor {

    String UFO_SECOND_WINDOW_SESSION_ID_KEY = "UFO_SECOND_WINDOW_SESSION_ID";
    String UFO_SECOND_WINDOW_SESSION_ID_XPATH = "//input[@name = 'sessionId']";
    String KO_CRITERIA_CALCULATION_TIME_KEY = "KO_CRITERIA_CALCULATION_TIME";
    String PAGE_LABEL_DIV = "AUT402_S001_X007";
    String LOADER_DIV = "AUT402_S001_X002";
    String RUN_KO_CALCULATION_BUTTON = "AUT402_S001_B011";
    String CALCULATION_TIME_DIV = "AUT402_S001_L002";
    String FIRST_KO_CRITERIUM_RADIO = "ufo-et-AUT402_S001_F001_row_1_sel";
    String NEXT_PAGE_BUTTON = "AUT402_S001_S001";

    @Click(value = RUN_KO_CALCULATION_BUTTON, andWait = @Wait(value = LOADER_DIV, until = Until.GONE, waitSecondsForElement = 60))
    AUT402_S001_KOCriteriaListPage clickRunKOCalculation();

    @StoreElementContent(storeKey = KO_CRITERIA_CALCULATION_TIME_KEY, value = CALCULATION_TIME_DIV)
    AUT402_S001_KOCriteriaListPage storeLastKOCriteriaCalculationTime();

    @CheckElementPresent(FIRST_KO_CRITERIUM_RADIO)
    AUT402_S001_KOCriteriaListPage checkFirstKOCriteriumIsPresent();

    @CustomAction
    default AUT402_S001_KOCriteriaListPage checkChangeOfCalculatedTime(String previousCalculatedTime) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        String calculatedTime = endpoint.getText(CALCULATION_TIME_DIV, Lookup.ID);
        if (calculatedTime.equals(previousCalculatedTime)) {
            throw new HarnessException("Calculated time did not change");
        }
        return null;
    }

    @CustomAction
    default AUT402_S001_KOCriteriaListPage logSecondWindowSessionId() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        String ufoSecondWindowSessionId = endpoint.getAttribute(UFO_SECOND_WINDOW_SESSION_ID_XPATH, Lookup.XPATH, "value");
        log("UFO second window session ID: " + ufoSecondWindowSessionId);
        store(UFO_SECOND_WINDOW_SESSION_ID_KEY, ufoSecondWindowSessionId);
        return null;
    }

    @CustomAction
    default AUT402_S001_KOCriteriaListPage checkCalculatedTimeNotEmpty() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        String calculatedTime = endpoint.getText(CALCULATION_TIME_DIV, Lookup.ID);
        Assertions.assertNotEquals("", calculatedTime);
        return null;
    }

    @Click(NEXT_PAGE_BUTTON)
    DEL401_S001_Cru clickNextPage();

}
