package cz.moneta.test.dsl.ufo.banka.pages.products.sme;

import cz.moneta.test.dsl.Harness;
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.banka.pages.products.sme.KUL012_S001_RequestedDocumentsPage.*;
import static cz.moneta.test.dsl.ufo.operations.shared.SharedElements.LOAD_IMG_DIV_CLASS;

@Wait(value = KUL012_S001_PAGE_LABEL_XPATH, by = Lookup.XPATH, waitSecondsForElement = 60)
@Wait(CONTINUE_BUTTON)
public interface KUL012_S001_RequestedDocumentsPage extends WebFlow<KUL012_S001_RequestedDocumentsPage>, StoreAccessor {
    String KUL012_S001_PAGE_LABEL_XPATH = "//div[text()='KUL012_S001']";

    String CONTINUE_BUTTON = "KUL012_S001_B001";
    String ADD_DOCUMENT_BUTTON = "KUL012_S001_B003";

    String DOCUMENT_CATEGORY_INPUT_XPATH = "//*[@id=\"KUL012_S001_G001_UfoTable\"]//*[text()='%s']";
    String DOCUMENT_INPUT_XPATH = "//*[@id=\"KUL012_S001_G002_UfoTable\"]//*[text()='%s']";

    String DOCUMENTS_FOP_RADIO = "KUL012_S001_G001-0";
    String DOCUMENT_SELECTION_RADIO = "KUL012_S001_G002-0";

    @Click(value = DOCUMENT_CATEGORY_INPUT_XPATH, isStringDynamicXpath = true, by = Lookup.XPATH, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE, waitSecondsForElement = 60))
    KUL012_S001_RequestedDocumentsPage clickDocumentCategory(String documentCategory);

    @Click(value = DOCUMENT_INPUT_XPATH, isStringDynamicXpath = true, by = Lookup.XPATH, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE, waitSecondsForElement = 60))
    KUL012_S001_RequestedDocumentsPage clickSelectDocument(String selectDocument);

    @Click(value = ADD_DOCUMENT_BUTTON, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE, waitSecondsForElement = 60))
    KUL012_S001_RequestedDocumentsPage clickAddDocument();

    @Click(CONTINUE_BUTTON)
    KUL012_S001_RequestedDocumentsPageTransitions clickContinue();

    @Click(value = DOCUMENTS_FOP_RADIO, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    @Click(value = DOCUMENT_SELECTION_RADIO, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    @Click(value = ADD_DOCUMENT_BUTTON, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    KUL012_S001_RequestedDocumentsPage selectAndAddDocument();

    @CustomAction
    default KUL012_S001_RequestedDocumentsPage assignDocuments(int documentsCount) {

        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        for (int i = 0; i < documentsCount; ++i) {
            if (endpoint.isElementVisible(2, DOCUMENTS_FOP_RADIO, Lookup.ID)) {
                selectAndAddDocument();
            }
        }
        return null;
    }
}
