package cz.moneta.test.dsl.monetaportal.savings.childbuildingsavings;

import cz.moneta.test.harness.endpoints.ib.NewIbEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.monetaportal.savings.childbuildingsavings.ContractPage.PAGE_HEADER;
import static cz.moneta.test.dsl.monetaportal.savings.childbuildingsavings.ContractPage.CONTRACT_LOADER;

@Wait(value = CONTRACT_LOADER, until = Until.GONE, waitSecondsForElement = 60)
@Wait(PAGE_HEADER)
public interface ContractPage extends ChildBuildingSavingsPageFlow<ContractPage> {
    String PAGE_HEADER = "//h2[text()='Přečtěte si smlouvu s parametry produktu']";
    String CONTRACT_LOADER = "//div[contains(@class,'c-loader')]/following-sibling::h1[text() = 'Připravujeme Vám smlouvu, chvilku strpení prosím']";
    String GO_SIGN_CONTRACT = "//button[text()='Rozumím a souhlasím, pokračovat k podpisu' and not (@disabled)]";
    String SMS_KEY_INPUT = "//input[@id='mobileKey']";
    String SIGN = "//button[text()='Podepsat']";
    String PAGE = "//canvas[@class='react-pdf__Page__canvas']";


    @Click(GO_SIGN_CONTRACT)
    ContractPage goSign();

    @TypeInto(SMS_KEY_INPUT)
    VictoryPage typeSmsKey(String smsKey);

    @CustomAction
    default ContractPage scrollDown() {
        NewIbEndpoint ep = getEndpoint(NewIbEndpoint.class);
        Integer pageHeight = Integer.parseInt(ep.getAttribute(PAGE, "height"));
        Integer pageCount = ep.findAndGetListOfElements(PAGE, Lookup.XPATH).size();
        ep.executeScript("window.scrollBy(0," + pageCount * pageHeight + ")");

        return null;
    }

    @Click(SIGN)
    VictoryPage sign();
}
