package cz.moneta.test.dsl.ib.loans.flexicredit.contract;

import cz.moneta.test.dsl.ib.IbPageFlow;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ib.IbEndpoint;
import cz.moneta.test.harness.support.web.CustomAction;

import java.util.stream.IntStream;

public interface FlexicreditContractInformationPage extends IbPageFlow<FlexicreditContractInformationPage>, StoreAccessor {

    String PDF_FORM = "//div[@id = 'pdfViewModal']";
    String END_OF_THE_PAGE = PDF_FORM + "//div[@data-page-number = '7']//div[text() = 'Tel.: +420 224 443 636 | www.moneta.cz'][last()]";
    String DOCUMENTS_TO_READ = "//span[text() = 'Vaše dokumenty k přečtení']";

    String BACK_BUTTON = "//span[text() = 'Zpět']//parent::button";
    String DOCUMENT = "//div[@id='pdfViewModal']//div[@class =  'react-pdf__Page' and @data-page-number = '%s']";

    @CustomAction
    default AfterReadContractPage readContractAndBack() {
        IbEndpoint ibEndpoint = getEndpoint(IbEndpoint.class);
        IntStream.range(1, 6)
                .forEachOrdered(index -> ibEndpoint.dragAndDrop(String.format(DOCUMENT, index), String.format(DOCUMENT, index + 1)));
        ibEndpoint.scrollIntoView(END_OF_THE_PAGE);
        ibEndpoint.waitForElementsToLoad(20, BACK_BUTTON);
        ibEndpoint.click(() -> BACK_BUTTON);
        return null;
    }
}
