package cz.moneta.test.dsl.salesforce.pages;

import cz.moneta.test.dsl.salesforce.components.PageHeader;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.sales_force.SalesForceEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.salesforce.components.PageHeader.HOME_LINK;
import static cz.moneta.test.dsl.salesforce.pages.MainPage.SPINNER_LOGO;

@Waits(value = {
        @Wait(value = SPINNER_LOGO, until = Until.GONE, waitSecondsForElement = 60),
        @Wait(value = HOME_LINK, until = Until.VISIBLE, explicitWaitSeconds = 1)
})
public interface MainPage extends WebFlow<MainPage>, PageHeader, StoreAccessor {
    String SPINNER_LOGO = "//div[contains(@class,'auraLoadingBox')]";

    MainPage onSalesForceMainPage();

    @TypeInto(value = SEARCH_INPUT, andWait = @Wait(value = SEARCH_AUTOCOMPLETE, until = Until.VISIBLE, explicitWaitSeconds = 1))
    MainPage typeIntoSearchBox(String searchByText);

    @Click(value = SEARCH_AUTOCOMPLETE_ITEM, isStringDynamicXpath = true)
    SearchPage selectSearchItem(String itemText);

    @Click(value = USER_PROFILE_LINK, andWait = @Wait(value = USER_PROFILE, until = Until.VISIBLE))
    MainPage clickUserProfile();

    @GetElementContent(USER_PROFILE)
    String getUsername();

    @CustomAction
    default SearchPage searchItem(String item) {
        SalesForceEndpoint endpoint = getEndpoint(SalesForceEndpoint.class);
        endpoint.click(() -> SEARCH_BUTTON);
        endpoint.type(() -> SEARCH_INPUT, item, false);
        endpoint.sendKeysOneAtATime(Key.ENTER);
        return null;
    }
}
