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

import cz.moneta.test.dsl.hypos.pages.HyposPageFlow;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.hypos.HyposEndpoint;
import cz.moneta.test.harness.support.web.*;
import org.openqa.selenium.TimeoutException;

import static cz.moneta.test.dsl.hypos.pages.contract.documents.DocumentGeneratePage.GENERATE_BUTTON;
import static cz.moneta.test.dsl.hypos.pages.contract.documents.DocumentGeneratePage.PAGE_TITLE;

@Wait({PAGE_TITLE, GENERATE_BUTTON})
public interface DocumentGeneratePage extends HyposPageFlow<DocumentGeneratePage>, StoreAccessor {
    String PAGE_TITLE = "//h1[contains(text(),'Volby generování dokumentu')]";
    String DOCUMENTS_MAKER_INPUT = "//input[@id='docsMaker']";
    String AUTO_VERIFY_CHECKBOX = "//input[@id='auto-verify']";
    String SIGN_LOCATION_PRAGUE_RADIO = "//input[@value='Praze']";
    String BOTTOM_LOADER = "//div[@class='loading-overlay']";

    String GENERATE_BUTTON = "//button[@id='butt-gen']";

    @TypeInto(value = DOCUMENTS_MAKER_INPUT, clear = true)
    DocumentGeneratePage fillCurrentBanker(String username);

    @Click(AUTO_VERIFY_CHECKBOX)
    DocumentGeneratePage clickAutoVerifyCheckbox();

    @Click(SIGN_LOCATION_PRAGUE_RADIO)
    DocumentGeneratePage clickSignInPrague();

    @CustomAction
    default DocumentsPage clickGenerateButton() {
        HyposEndpoint endpoint = getEndpoint(HyposEndpoint.class);
        endpoint.waitForElementsToLoad(20, Until.GONE, BOTTOM_LOADER);
        endpoint.waitForElementsToLoad(20, Until.CLICKABLE, GENERATE_BUTTON);
        endpoint.click(() -> GENERATE_BUTTON);
        try {
            endpoint.acceptAlert(120); //ne vzdy se zobrazi
        } catch (TimeoutException e) {
        }
        return null;
    }
}