package cz.moneta.test.dsl.ufo.cb.landing;

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.cb.landing.DMS401_S002_DocumentsPage.PAGE_LABEL;
import static cz.moneta.test.dsl.ufo.cb.landing.DMS401_S002_DocumentsPage.PAGE_LOADER_END;

@Wait(value = PAGE_LABEL, until = Until.VISIBLE)
@Wait(PAGE_LOADER_END)
public interface DMS401_S002_DocumentsPage extends StoreAccessor, WebFlow<DMS401_S002_DocumentsPage> {

    String PAGE_LABEL = "DMS401_S002_L005";
    String SEARCH_INPUT_BUTTON = "DMS401_S002_B011";
    String OTHER_DOCUMENT_INPUT = "DMS401_S002_G001-0";
    String DETAIL_BUTTON = "DMS401_S002_B001";
    String NEW_DOCUMENT_BUTTON = "DMS401_S002_B005";
    String DOCUMENT_NAME = "//table[@id='DMS401_S002_G001_UfoTable']//td[3]";
    String FIRST_DOCUMENT_TITLE_XPATH = "//table[@id='DMS401_S002_G008_UfoTable']/tbody/tr[1]/td[3]";
    String PAGE_LOADER_END = "ufo-ts-pageLoadEnd";

    @Click(DETAIL_BUTTON)
    DMS401_S004_DocumentDetailPage clickDocumentDetail();

    @Click(NEW_DOCUMENT_BUTTON)
    DMS401_S003_SaveDocument clickNewDocumentButton();

    @CheckElementContent(value = DOCUMENT_NAME, by = Lookup.XPATH)
    void checkDocumentName(String documentName);

    @CustomAction
    default DMS401_S002_DocumentsPage checkDocument() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        String value = endpoint.getAttribute(OTHER_DOCUMENT_INPUT, "value");
        String xpath = "//input[@name='DMS401_S002_G001' and @value='" + value + "']";
        endpoint.click(() -> OTHER_DOCUMENT_INPUT);
        endpoint.waitForElementsToLoad(5, Lookup.XPATH, xpath);
        return null;
    }

    @CheckElementContent(value = FIRST_DOCUMENT_TITLE_XPATH, by = Lookup.XPATH)
    DMS401_S002_DocumentsPage checkFirstDocumentName(String documentName);

}
