package cz.moneta.test.dsl.ufo.operations.search;

import cz.moneta.test.dsl.ufo.operations.shared.topmenu.XXX701_SharedTopMenu;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.ufo.operations.search.OPS001_S001_ClientSearchPageUfoOperations.MEETINGS_AND_SMS_BUTTON;
import static cz.moneta.test.dsl.ufo.operations.search.OPS001_S001_ClientSearchPageUfoOperations.RECORDER_APP_BUTTON;
import static cz.moneta.test.dsl.ufo.operations.search.OPS001_S001_ClientSearchPageUfoOperations.SEARCH_BUTTON;
import static cz.moneta.test.dsl.ufo.operations.search.OPS001_S001_ClientSearchPageUfoOperations.TASKS_BUTTON;
import static cz.moneta.test.dsl.ufo.operations.search.OPS001_S001_SharedSearchElements.SEARCH_INPUT;
import static cz.moneta.test.dsl.ufo.operations.shared.SharedElements.LOAD_IMG_DIV_CLASS;

@Wait(value = {RECORDER_APP_BUTTON, SEARCH_INPUT, SEARCH_BUTTON, TASKS_BUTTON, MEETINGS_AND_SMS_BUTTON}, waitSecondsForElement = 60)
@Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE)
@Wait(explicitWaitSeconds = 1) // Workaround for page loading
public interface OPS001_S001_ClientSearchPageUfoOperations extends WebFlow<OPS001_S001_ClientSearchPageUfoOperations>, StoreAccessor, OPS001_S001_SharedSearchElements<OPS001_S001_ClientSearchPageUfoOperations>, OPS001_S001_LeftMenu, XXX701_SharedTopMenu {

    String SEARCH_BUTTON = "OPS001_S001_B006";
    String SEARCH_BY_RADIOS = "OPS001_S001_R003_%s";
    String BIRTH_NUMBER_RADIO = "OPS001_S001_R003_2";
    String CARDS_RADIO = "OPS001_S001_R001_2";

    @Click(BIRTH_NUMBER_RADIO)
    OPS001_S001_ClientSearchPageUfoOperations clickRcRadio();

    @Click(value = CARDS_RADIO, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, until = Until.GONE, by = Lookup.CLASSNAME))
    OPS001_S001_CardsSearch clickCardsRadio();

    @Click(SEARCH_BY_RADIOS)
    @Click(SEARCH_BUTTON)
    OPS001_S001_ClientSearchPageTransitions searchBy(SearchBy searchBy);

    @CustomAction
    default OPS001_S001_ClientSearchPageTransitions searchByAndAcceptAlertIfVisible(SearchBy searchBy) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        searchBy(searchBy);
        if (endpoint.isAlertPresent(10)) {
            endpoint.acceptAlert(3);
        }
        return null;
    }

    @Wait(value = SEARCH_BUTTON, until = Until.PRESENT_IN_DOM)
    @Click(SEARCH_BUTTON)
    OPS001_S001_ClientSearchPageTransitions clickSearchClient();

    @CheckElementPresent(SEARCH_BUTTON)
    OPS001_S001_ClientSearchPageUfoOperations checkSearchInputInMainPage();

    /**
     * Workaround
     * Nepravidelně se stává, že se tlačítko Vyhledat v OPS pouze označí, ale na click nezareaguje
     */
    @CustomAction
    default OPS001_S001_ClientSearchPageTransitions clickSearchClientWithHover() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.moveToElement(SEARCH_BUTTON);
        endpoint.click(() -> SEARCH_BUTTON);
        return null;
    }
}