package cz.moneta.test.dsl.ufo.banka.tasks;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.loans.VER017_S001_SecondBankerVerificationStatusPage;
import cz.moneta.test.dsl.ufo.banka.pages.loans.expres.NEP006_S001_ExpressScoringResultPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage;
import cz.moneta.test.dsl.util.data.client.NonClient;

import java.util.function.Function;

import static cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisProductClass.EXPRESS;

public final class NewLoanApplicationTasks {

    private final Harness harness;

    public NewLoanApplicationTasks(Harness harness) {
        this.harness = harness;
    }

    public Function<HKO101_S001_MainClientPage, NEP006_S001_ExpressScoringResultPage> createNewLoanApplication(NonClient nonClient, int loanAmount, int numberOfInstallments) {
        return start -> start.createNewProduct(EXPRESS)
                .onExpressPage()
                .fillAmount(loanAmount)
                .selectNumberOfInstallments(numberOfInstallments)
                .clickCalculateInstallment()
                .clickContinue()
                .onIdSelectionPage()
                .selectIdCardRadio()
                .selectPassportRadio()
                .clickDocumentsCheckAndContinue()
                .onCreditCardApplicantPage()
                .then(harness.tasks().ufo().banka().mainApplicantInformation().fillMainApplicantInformation(nonClient))
                .onApplicationDataPageTwo()
                .selectInsurance("Bez pojištění")
                .clickSaveButton()
                .onAddCodeOfRecommendedPersonPage()
                .checkProductNotRecommended()
                .clickWithoutRecommendationAndDismissAlertIfvisible()
                .onSignatureConfirmationPage()
                .clickApplicantSignatureCheckbox()
                .clickYes()
                .onScanDocumentsPage()
                .clickSkipButton()
                .onMegaExpressScoringPage()
                .waitForActiveCreateAccountButton()
                .storeAppId()
                .logAppState();
    }

    public Function<VER017_S001_SecondBankerVerificationStatusPage, NEP006_S001_ExpressScoringResultPage> finishExpressLoan() {
        return start -> start
                .clickReload()
                .clickContinue()
                .onPrintPage()
                .saveDocumentUrl()
                .closePrintWindow()
                .onSignContractPage()
                .clickFirstSignerCheckbox()
                .clickYesButtonToClientContractSignPage()
                .clickLeftButton()
                .onExpressScoringResultPage()
                .waitForStatusAndRefresh("Úvěr načerpán");
    }
}