package cz.moneta.test.dsl.hypos.pages.contract.clients;

import cz.moneta.test.dsl.hypos.pages.HyposPageFlow;
import cz.moneta.test.dsl.util.data.hypos.HyposContract;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.hypos.HyposEndpoint;
import cz.moneta.test.harness.support.web.CustomAction;
import cz.moneta.test.harness.support.web.Until;
import cz.moneta.test.harness.support.web.Wait;

import static cz.moneta.test.dsl.hypos.pages.contract.clients.LoadCifPage.PAGE_TITLE;

@Wait(PAGE_TITLE)
public interface LoadCifPage extends HyposPageFlow<LoadCifPage>, StoreAccessor {
    String PAGE_TITLE = "//h1[text()='Načtení CIF z ICBS']";
    String NO_CIF_RADIO_XPATH = "(//input[@name='cif'])[%s]";
    String SAVE_BUTTON_XPATH = "(//th//input[@value='Uložit'])[%s]";
    String LOADER_IMG_XPATH = "//div[not(@style='display: none;')]/img[@src='/img/common/loadingAnimation.gif']";

    @CustomAction
    default ClientsListPage setNoCifRadio(HyposContract hyposContract) {
        HyposEndpoint endpoint = getEndpoint(HyposEndpoint.class);
        for (int i = 0; i < hyposContract.getClients().size(); i++) {
            String client = String.valueOf(i+1);
            endpoint.click(() -> String.format(NO_CIF_RADIO_XPATH, client));
            endpoint.click(() -> String.format(SAVE_BUTTON_XPATH, client));
            endpoint.waitForElementsToLoad(20, Until.GONE, LOADER_IMG_XPATH);
        }
        return null;
    }
}