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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.common.pages.OBR001_S001_PrintInstructionPage;
import cz.moneta.test.dsl.ufo.banka.pages.common.pages.OBR001_S001_PrintInstructionPageTransitions;
import cz.moneta.test.dsl.ufo.banka.pages.fon.CIF111_S016_CreateNewCifPage;
import cz.moneta.test.dsl.ufo.banka.pages.fon.DMS001_S001_ScanDocumentsPage;
import cz.moneta.test.dsl.ufo.banka.pages.fon.DMS001_S001_ScanDocumentsPageTransitions;
import cz.moneta.test.dsl.ufo.banka.pages.fon.newCif.CIF111_S001_NewCifDataNavigation;
import cz.moneta.test.dsl.ufo.banka.pages.fop.KUL004_S001_EditBusinessInfoPage;
import cz.moneta.test.dsl.ufo.banka.pages.fop.KUL004_S001_EditBusinessInfoPageTransitions;
import cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPageTransitions;
import cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage;
import cz.moneta.test.dsl.ufo.banka.pages.fop.KUL077_S001_ClientsBusinessAndPersonalData;
import cz.moneta.test.dsl.ufo.banka.pages.fop.KUL078_S001_AdditionalBusinessData;
import cz.moneta.test.dsl.ufo.banka.pages.main.PSP004_S001_PrintPageTransitions;
import cz.moneta.test.dsl.ufo.banka.pages.products.current.account.BPP001_S001_DocumentationSendPage;
import cz.moneta.test.dsl.ufo.banka.pages.products.debit.card.SIF004_S001_ContactSettingsForSendingEmailsPage;
import cz.moneta.test.dsl.ufo.banka.pages.products.saving.accounts.SPU001_S001_SavingAccountPage;
import cz.moneta.test.dsl.ufo.banka.pages.products.sme.*;
import cz.moneta.test.dsl.ufo.banka.tasks.clients.CreateStatutoryAuthority;
import cz.moneta.test.dsl.ufo.banka.tasks.clients.NewClientTasks;
import cz.moneta.test.dsl.util.data.client.MaritalStatus;
import cz.moneta.test.dsl.util.data.client.NonClient;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.Lookup;

import java.util.function.Function;

import static cz.moneta.test.dsl.ufo.banka.pages.products.sme.CHK001_S001_WaitingForModulationPage.CHK001_S001_PAGE_LABEL_XPATH;
import static cz.moneta.test.dsl.ufo.banka.pages.products.sme.KUL004_S004_AddressesAndContactsPage.KUL004_S004_PAGE_LABEL_XPATH;
import static cz.moneta.test.dsl.ufo.banka.pages.products.sme.KUL043_S001_ChangeSelectedParametersPage.INTEREST_RATE_KEY;
import static cz.moneta.test.dsl.ufo.banka.pages.products.sme.KUL043_S001_ChangeSelectedParametersPage.LIMITS_CREDIT_KEY;
import static cz.moneta.test.dsl.ufo.banka.pages.products.sme.KUL043_S001_ChangeSelectedParametersPage.PAYMENTS_COUNT_KEY;
import static cz.moneta.test.dsl.ufo.banka.pages.products.sme.SMA002_S001_SMEAutomatsApplicationOverview.ACCOUNT_NUMBER_KEY;

public class NewSmeAutomatSubTasks {

    private final CreateStatutoryAuthority newStatutoryAuthority;
    private final NewClientTasks newClientTasks;
    private final Harness harness;

    public NewSmeAutomatSubTasks(Harness harness) {
        this.newStatutoryAuthority = harness.tasks().ufo().banka().newStatutoryAuthority();
        this.newClientTasks = harness.tasks().ufo().banka().newClient();
        this.harness = harness;
    }

    /***
     * Stranka s rozpracovanymi ziadostami sa preskakuje v pripade, ze klient nejake rozpracovane ziadosti uz ma.
     */
    public Function<OBR001_S001_PrintInstructionPageTransitions, KUL001_S001_SelectProductsPage> skipProcessedRequestsIfOpened(Harness harness) {
        if (harness.getEndpoint(UfoEndpoint.class).isElementVisible(2, KUL066_S001_ProcessedRequestsPage.KUL066_S001_PAGE_LABEL_XPATH, Lookup.XPATH)) {
            return start -> start
                    .onProcessedRequestsPage()
                    .clickContinue()
                    .onSelectProductsPage();
        } else {
            return start -> start.onSelectProductsPage();
        }
    }

    public Function<HKO101_S001_MainClientPageTransitions, KUL001_S001_SelectProductsPage> selectSmeAutomatFop(Harness harness) {
        return start -> start
                .onRegistrationCertificatePage()
                .clickRightButton()
                .then(skipProcessedRequestsIfOpened(harness));
    }

    public Function<HKO101_S001_MainClientPageTransitions, KUL001_S001_SelectProductsPage> selectSmeAutomatPo(Harness harness) {
        return start -> start
                .onRegistrationCertificatePage()
                .clickRightButton()
                .onStatutoryAuthorityPage()
                .clickContinue()
                .onPrintInstructionsTransitionsPage()
                .then(skipProcessedRequestsIfOpened(harness));
    }

    public Function<KUL002_S001_ProductDetailPage, KUL002_S001_ProductDetailPage> fillProductDetailsExpresBusiness(String financingReason, String loanExpresAmount) {
        return start -> start
                .then(fillRequiredLoanEb(loanExpresAmount))
                .selectFinancingReason(financingReason);
    }

    public Function<KUL002_S001_ProductDetailPage, KUL002_S001_ProductDetailPage> fillRequiredLoanEb(String loanExpresAmount) {
        return start -> start
                .fillRequiredLoanAmount(loanExpresAmount)
                .fillNumberOfPayments("120")
                .selectPaymentDay("01")
                .selectFirstCurrentAccount();
    }

    public Function<KUL003_S001_FlexiBusinessProductDetail, KUL003_S001_FlexiBusinessProductDetail> fillProductDetailsFlexiBusinessNoFopAssociation(String loanFlexiAmount) {
        return start -> start
                .fillRequiredLoanAmount(loanFlexiAmount)
                .clickNoFopAssociation()
                .then(fillRequiredLoanDetails());
    }

    public Function<KUL003_S001_FlexiBusinessProductDetail, KUL003_S001_FlexiBusinessProductDetail> fillProductDetailsFlexiBusinessYesFopAssociation(String loanFlexiAmount) {
        return start -> start
                .fillRequiredLoanAmount(loanFlexiAmount)
                .clickYesFopAssociation()
                .fillNumberOfAssociationMembers("2")
                .then(fillRequiredLoanDetails());
    }

    public Function<KUL003_S001_FlexiBusinessProductDetail, KUL003_S001_FlexiBusinessProductDetail> fillProductDetailsFlexiBusinessPo(String loanFlexiAmount) {
        return start -> start
                .fillRequiredLoanAmount(loanFlexiAmount)
                .then(fillRequiredLoanDetails());
    }

    public Function<KUL003_S001_FlexiBusinessProductDetail, KUL003_S001_FlexiBusinessProductDetail> fillRequiredLoanDetails() {
        return start -> start
                .selectFirstCurrentAccount()
                .selectOtherBankStatements("za 3 měsíce")
                .selectMonth()
                .fillAccountNumber("301030")
                .fillBankCode("0100")
                .fillFirstTurnover("200000")
                .fillSecondTurnover("200000")
                .fillThirdTurnover("200000");
    }

    /***
     * V pripade klienta, ktoremu bola dokumentacia uz raz zaslana, sa neodosiela znova. Noemu klientovi sa dokumentacia odosiela.
     */
    public Function<KUL001_S001_SelectProductsPageTransitions, KUL004_S001_EditBusinessInfoPage> sendDocumentationIfPageShownOtherwiseSkipPo(Harness harness) {
        if (harness.getEndpoint(UfoEndpoint.class).isElementVisible(2, BPP001_S001_DocumentationSendPage.SEND_DOCUMENTS_BUTTON, Lookup.ID)) {
            return start -> start
                    .onDocumentationSendPage()
                    .sendDocumentsToClient()
                    .clickLeftButton()
                    .onEditBusinessInfoPage();
        } else {
            return start -> start
                    .onEditBusinessInfoPage();
        }
    }

    public Function<KUL001_S001_SelectProductsPageTransitions, KUL077_S001_ClientsBusinessAndPersonalData> sendDocumentationIfPageShownOtherwiseSkipFop(Harness harness) {
        if (harness.getEndpoint(UfoEndpoint.class).isElementVisible(2, BPP001_S001_DocumentationSendPage.SEND_DOCUMENTS_BUTTON, Lookup.ID)) {
            return start -> start
                    .onDocumentationSendPage()
                    .sendDocumentsToClient()
                    .clickLeftButton()
                    .onBusinessInfoPage();
        } else {
            return start -> start
                    .onBusinessInfoPage();
        }
    }

    public Function<OBR001_S001_PrintInstructionPageTransitions, SPU001_S001_SavingAccountPage> sendDocumentationIfPageShownOtherwiseSkip(Harness harness) {
        if (harness.getEndpoint(UfoEndpoint.class).isElementVisible(2, BPP001_S001_DocumentationSendPage.SEND_DOCUMENTS_BUTTON, Lookup.ID)) {
            return start -> start
                    .onDocumentationSendPage()
                    .sendDocumentsToClient()
                    .clickLeftButton()
                    .onSavingsAccountPage();
        } else {
            return start -> start
                    .onSavingsAccountPage();
        }
    }

    public Function<KUL078_S001_AdditionalBusinessData, KUL012_S001_RequestedDocumentsPage> editBusinessInformationExpresBusiness() {
        return start -> start
                .then(setExpresBusinessData())
                .clickContinue()
                .onRequestedDocumentPage();
    }

    public Function<KUL078_S001_AdditionalBusinessData, KUL012_S001_RequestedDocumentsPage> editBusinessInformationExpresBusinessWithFopAssociation() {
        return start -> start
                .then(setExpresBusinessDataWithAssociationFop())
                .clickContinue()
                .onRequestedDocumentPage();
    }

    public Function<KUL078_S001_AdditionalBusinessData, KUL012_S001_RequestedDocumentsPage> editBusinessInformationGreenExpressBusiness() {
        return start -> start
                .then(setGreenExpresBusinessData())
                .clickContinue()
                .onRequestedDocumentPage();
    }

    public Function<KUL078_S001_AdditionalBusinessData, KUL078_S001_AdditionalBusinessData> setExpresBusinessData() {
        return start -> start
                .clickBusinessNotForCertainTime()
                .fillNumberOfEmployees("0")
                .clickFirstEconomicActivity()
                .fillRevenueShare("100")
                .clickBusinessNotInterrupted()
                .clickNotTransformation()
                .clickAssociationFopNo();
    }

    public Function<KUL078_S001_AdditionalBusinessData, KUL078_S001_AdditionalBusinessData> setExpresBusinessDataWithAssociationFop() {
        return start -> start
                .clickBusinessNotForCertainTime()
                .fillNumberOfEmployees("0")
                .clickFirstEconomicActivity()
                .clickBusinessNotInterrupted()
                .clickNotTransformation()
                .clickAssociationFopYes()
                .fillAssociationFopMembersCount("2")
                .fillRevenueShare("100");
    }

    public Function<KUL078_S001_AdditionalBusinessData, KUL078_S001_AdditionalBusinessData> setGreenExpresBusinessData() {
        return start -> start
                .clickBusinessNotForCertainTime()
                .fillNumberOfEmployees("0")
                .clickFirstEconomicActivity()
                .fillRevenueShare("100")
                .clickBusinessNotInterrupted();
    }

    public Function<KUL078_S001_AdditionalBusinessData, KUL078_S001_AdditionalBusinessData> editBusinessInformationFlexiBusiness() {
        return start -> start
                .clickBusinessNotForCertainTime()
                .fillNumberOfEmployees("0")
                .clickFirstEconomicActivity()
                .fillRevenueShare("100")
                .clickBusinessNotInterrupted()
                .clickNotTransformation();
    }

    public Function<KUL078_S001_AdditionalBusinessData, KUL078_S001_AdditionalBusinessData> editEmailsToDummy(String dummyEmail) {
        return start -> start
                .clickEdit()
                .onEditPoFopPage()
                .fillEmail(dummyEmail)
                .clickSend()
                .onEditFoWhenEditingFopPage()
                .clickSend()
                .onAppendixConfirmationPage()
                .continueWithoutPrint()
                .onAdditionalBusinessData();
    }

    public Function<KUL078_S001_AdditionalBusinessData, KUL078_S001_AdditionalBusinessData> editEmailAndMaritalStatus(String dummyEmail) {
        return start -> start
                .clickEdit()
                .onEditPoFopPage()
                .fillEmail(dummyEmail)
                .clickSend()
                .onEditFoWhenEditingFopPage()
                .selectMaritalStatus(MaritalStatus.MARRIED.getUfoValue())
                .selectSJM("Ne")
                .fillEmail(dummyEmail)
                .clickNotPoliticallyExposedPerson()
                .clickSend()
                .onAppendixConfirmationPage()
                .continueWithoutPrint()
                .onAdditionalBusinessData();
    }

    public Function<KUL004_S001_EditBusinessInfoPage, KUL004_S001_EditBusinessInfoPage> editEmailsToDummyPo(String dummyEmail) {
        return start -> start
                .clickEdit()
                .onEditPoFopPage()
                .fillEmail(dummyEmail)
                .clickSend()
                .onAppendixConfirmationPage()
                .continueWithoutPrint()
                .onEditBusinessInfoPage();
    }

    public Function<KUL012_S001_RequestedDocumentsPage, KUL012_S001_RequestedDocumentsPage> assignDocuments(String assignedDocument, String selectDocument) {
        return start -> start
                .clickDocumentCategory(assignedDocument)
                .clickSelectDocument(selectDocument)
                .clickAddDocument();
    }

    public Function<KUL010_S001_FinancialSignpostPage, KUL010_S001_FinancialSignpostPage> fillFinancialSignpostDeclaration() {
        return start -> start
                .fillLastYear("2022")
                .selectLastYearIncome("Prohlášení o příjmech")
                .then(fillFinancialData());
    }

    public Function<KUL010_S001_FinancialSignpostPage, KUL010_S001_FinancialSignpostPage> fillFinancialSignpostDp(String lastYear, String yearBeforeLast) {
        return start -> start
                .selectLastYearIncome("Daňové přiznání")
                .clickLastYearDp(lastYear)
                .selectYearBeforeLastIncome("Daňové přiznání")
                .clickYearBeforeLastDp(yearBeforeLast)
                .then(fillPersonalFinancialData());
    }

    public Function<KUL012_S001_RequestedDocumentsPage, KUL010_S001_FinancialSignpostPage> selectNoPsdData() {
        return start -> start
                .clickContinue()
                .onFinancialSignpostPage()
                .clickNoPsd2Data();
    }

    public Function<KUL010_S001_FinancialSignpostPage, KUL010_S001_FinancialSignpostPage> fillFinancialData() {
        return start -> start
                .fillIncomeFromBusiness("2000000")
                .fillGrossIncome("0")
                .fillTotalAssets("2000000")
                .fillProfit("1500000")
                .fillYearBeforeLast("2021")
                .selectYearBeforeLastIncome("Nezadává");
    }

    public Function<KUL010_S001_FinancialSignpostPage, KUL010_S001_FinancialSignpostPage> fillPersonalFinancialData() {
        return start -> start
                .fillNumberOfMainCustomers("2")
                .clickNoMoreThanHalfMainCustomer()
                .clickNoMoreThanHalfMainContractor()
                .fillBusinessLoanOutsideMmb("0")
                .clickYesClientProperty()
                .clickYesTaxAdvisor();
    }

    public Function<KUL010_S001_FinancialSignpostPage, KUL010_S001_FinancialSignpostPage> fillFinancialSignpostNettoIncome() {
        return start -> start
                .fillNettoIncomeOfHousehold("200000")
                .fillOtherLivingCosts("0")
                .fillOtherMonthlyPayments("0");
    }

    public Function<KUL006_S001_ListOfPersonsOnRequestPage, PSP004_S001_PrintPageTransitions> summarizeRequestAndPrint() {
        return start -> start
                .clickContinue()
                .onRequestSummaryPage()
                .clickContinue()
                .onRequestForSMEPage()
                .clickPrintRequest()
                .onPrintPagePSP004()
                .saveDocumentUrl()
                .closePrintWindow();
    }

    public Function<KUL006_S001_ListOfPersonsOnRequestPage, PSP004_S001_PrintPageTransitions> summarizeRequestAndPrintAssociation() {
        return start -> start
                .clickContinue()
                .onRequestSummaryPage()
                .clickContinue()
                .onPrintPage()
                .saveDocumentUrl()
                .closePrintWindow();
    }

    public Function<KUL006_S001_ListOfPersonsOnRequestPage, KUL006_S001_ListOfPersonsOnRequestPage> addHusbandOrWife(NonClient wifeData) {
        return start -> start
                .addHusbandOrWife()
                .fillFirstName(wifeData.firstName())
                .fillLastName(wifeData.lastName())
                .fillBirthNumber(wifeData.birthNumber())
                .clickCzechResidency()
                .clickAddressLookupMagnifyGlass()
                .fillSearchedAddress("Horni lan 445")
                .searchAddressAndSelectFirst()
                .clickConsentPersonallySigned()
                .clickNextToListOfPersonsOnRequestPage();
    }

    public Function<CHK001_S001_WaitingForModulationPage, KUL008_S001_ModulationPage> waitForModulation(Harness harness) {
        return start -> start
                .waitForModulation(120)
                .onModulationPage();
    }

    public Function<KUL008_S001_ModulationPage, DMS001_S001_ScanDocumentsPageTransitions> uploadScannedDocuments(Harness harness) {
        return start -> start
                .clickDocumentScanning()
                .onScanDocumentsPage()
                .then(assignScannedDocuments(harness))
                .clickContinue();
    }

    /**
     * Special method for EaSI, because there is different page path
     *
     * @param harness
     * @return
     */
    public Function<KUL008_S001_ModulationPage, HKO101_S001_MainClientPage> uploadScannedDocumentsEaSI(Harness harness) {
        return start -> start
                .clickDocumentScanning()
                .onScanDocumentsPage()
                .then(assignScannedDocuments(harness))
                .clickContinue()
                .onDocumentationSendPage()
                .sendDocumentsToClient()
                .clickLeftButton()
                .onMainClientPage();
    }

    /***
     * Na prostredi PPE niektore automaty vyzaduju upload viacerych dokumentov, v porovnani s TST1, kde tieto dokumenty potrebne nie su.
     */
    public Function<DMS001_S001_ScanDocumentsPage, DMS001_S001_ScanDocumentsPage> assignScannedDocuments(Harness harness) {
        return start -> start.assignAllDocuments(harness);
    }

    public Function<DMS001_S001_ScanDocumentsPage, DMS001_S001_ScanDocumentsPage> assignDocumentByIndex(String index) {
        return start -> start
                .clickScannedDocuments()
                .onDocumentsSummaryPage()
                .clickBack()
                .uploadFakeExtractZL()
                .selectFirstUploadedDocument()
                .selectDocumentToAssign(index)
                .assignLoadedDocument();
    }

    public Function<DMS001_S001_ScanDocumentsPage, DMS001_S001_ScanDocumentsPage> assignFirstDocument() {
        return start -> start
                .uploadFakeExtractZL()
                .selectFirstUploadedDocument()
                .selectFirstDocumentToAssign()
                .assignLoadedDocument();
    }

    public Function<DMS001_S001_ScanDocumentsPage, DMS001_S001_ScanDocumentsPage> assignSecondDocument() {
        return start -> start
                .clickScannedDocuments()
                .onDocumentsSummaryPage()
                .clickBack()
                .uploadFakeExtractZL()
                .selectFirstUploadedDocument()
                .selectSecondDocumentToAssign()
                .assignLoadedDocument();
    }

    public Function<DMS001_S001_ScanDocumentsPage, DMS001_S001_ScanDocumentsPage> assignThirdDocument() {
        return start -> start
                .clickScannedDocuments()
                .onDocumentsSummaryPage()
                .clickBack()
                .uploadFakeExtractZL()
                .selectFirstUploadedDocument()
                .selectThirdDocumentToAssign()
                .assignLoadedDocument();
    }

    public Function<HKO101_S001_MainClientPageTransitions, KUL002_S001_ProductDetailPage> selectMonetaBusinessCard(Harness harness, String mbcProduct) {
        return start -> start
                .onRegistrationCertificatePage()
                .clickRightButton()
                .then(skipProcessedRequestsIfOpened(harness))
                .selectExpresBusiness(mbcProduct);
    }

    public Function<KUL058_S001_CommercialCreditCardDetailPage, SIF004_S001_ContactSettingsForSendingEmailsPage> fillCreditCardDetails() {
        return start -> start
                .fillAmount("60000")
                .selectDistribution("Elektronicky a e-mailem")
                .clickAccountAtMMB()
                .selectFirstAccount(2)
                .clickContinue()
                .onContactSettingsForSendingEmailsPage();
    }

    public Function<SIF004_S001_ContactSettingsForSendingEmailsPage, SIF004_S001_ContactSettingsForSendingEmailsPage> fillContactSettings() {
        return start -> start
                .clickFillEmailFromCif()
                .clickFillPhoneFromCif();
    }

    public Function<KUL004_S001_EditBusinessInfoPage, KUL004_S004_AddressesAndContactsPage> fillBusinessInformation(String employeesCount) {
        return start -> start
                .fillBaseBusinessCapital("200000,00")
                .selectBusinessForCertainTime("Ne")
                .fillNumberOfPersonsActCompany("1")
                .fillNumberOfEmployees(employeesCount)
                .selectStatutoryAuthority("Jednatel")
                .clickFirstEconomicActivityRadio()
                .clickNoActivityInterrupted()
                .clickNoTransformation()
                .fillRevenueShare("100")
                .clickContinue()
                .onAddressesAndContactsPage();
    }

    public Function<KUL006_S001_ListOfPersonsOnRequestPage, KUL004_S004_AddressesAndContactsPage> selectStatutoryAuthority(String deposit) {
        return start -> start
                .addStatutoryAuthority()
                .clickFirstRepresentative()
                .clickContinue()
                .onClientConsensPage()
                .clickYes()
                .continueToSignerSelectionPage()
                .clickSignedPersonallyAndContinue()
                .saveDocumentUrl()
                .closePrintWindow()
                .onPrintInstructionPage()
                .clickRightButton()
                .onClientRequestDetailPage()
                .checkContactPerson()
                .then(fillStatutoryAuthorityDetailAndContinue(deposit));
    }

    public Function<KUL006_S001_ListOfPersonsOnRequestPage, KUL004_S004_AddressesAndContactsPage> selectStatutoryAuthorityCooperativeOwnership() {
        return start -> start
                .addStatutoryAuthority()
                .clickFirstRepresentative()
                .clickContinue()
                .onClientsDataORequestPage()
                .clickContactPerson()
                .clickSignsAgreement()
                .clickContinue();
    }

    public Function<KUL006_S001_ListOfPersonsOnRequestPage, KUL004_S004_AddressesAndContactsPage> selectSecondStatutoryAuthority(String deposit) {
        return start -> start
                .addStatutoryAuthority()
                .clickSecondRepresentative()
                .clickContinue()
                .onClientConsensPage()
                .clickYes()
                .continueToSignerSelectionPage()
                .clickSignedPersonallyAndContinue()
                .saveDocumentUrl()
                .closePrintWindow()
                .onPrintInstructionPage()
                .clickRightButton()
                .onClientRequestDetailPage()
                .then(fillStatutoryAuthorityDetailAndContinue(deposit));
    }

    public Function<KUL004_S008_ClientRequestDetailPage, KUL004_S004_AddressesAndContactsPage> fillStatutoryAuthorityDetailAndContinue(String deposit) {
        return start -> start
                .checkCompanion()
                .fillDeposit(deposit)
                .checkSigner()
                .clickContinue();
    }

    public Function<KUL006_S001_ListOfPersonsOnRequestPage, KUL008_S001_ModulationPage> finalizeSmeForPo(Harness harness) {
        return start -> start
                .clickContinue()
                .onRequestSummaryPage()
                .clickContinue()
                .onPrintPage()
                .saveDocumentUrl()
                .closePrintWindow()
                .onSmeApplicationSignPage()
                .clickContinue()
                .onWaitingForModulationPage()
                .waitForModulation(120, 7)
                .onModulationPage();
    }

    public Function<KUL052_S002_ManuallyAddedCommitmentsPage, KUL052_S001_ConsolidationCommitmentsPage> setCommitment() {
        return start -> start
                .selectCommitmentType("Úvěr")
                .selectCompany("ESSOX s. r. o. (nebo Finance Consumer Credit)")
                .fillCurrentPrincipalAmount("80000")
                .addCommitment()
                .clickContinueToPreliminaryEvaluationPage();
    }

    public Function<KUL053_S002_EditCommitmentPage, KUL053_S001_CompleteCommitmentsPage> fillCommitmentData() {
        return start -> start
                .selectCommitmentExistingDocument("Smlouva o úvěru")
                .selectCommitmentPaymentDocument("Výpis z běžného účtu")
                .fillBankCode("0100")
                .fillPaymentAccountNumber("301030")
                .fillCreditContractNumber("11111111")
                .fillVariableSymbol("11111111")
                .fillSpecificSymbol("1111")
                .fillOriginalLoanAmount("100000")
                .fillPaymentsNumber("24")
                .fillInstallmentAmount("50000")
                .fillEstablishDate("1.1.2024")
                .selectPaymentPeriod("Měsíční")
                .fillAnnualInterestRate("5,2")
                .fillFirstPaymentDate("1.2.2024")
                .clickSaveCommitment()
                .fillCreditContractNumberInControl("11111111")
                .clickSave();
    }

    public Function<KUL008_S001_ModulationPage, HKO101_S001_MainClientPage> uploadScannedDocumentsConsolidation(Harness harness) {
        return start -> start
                .clickDocumentScanning()
                .onScanDocumentsPage()
                .assignAllDocuments(harness)
                .clickContinue()
                .onScanDocumentsPage()
                .uploadFakeExtractZL()
                .selectFirstUploadedDocument()
                .selectFirstClientsDocumentToAssign()
                .assignClientsLoadedDocument()
                .clickContinue()
                .onMainClientPage();
    }

    /***
     * Vypocet modelace niekedy trva dlhsie, preto je nutne kliknut na tlacidlo Znova.
     */
    public Function<OBR001_S001_PrintInstructionPageTransitions, KUL051_S001_PreliminaryEvaluationPage> waitForPreliminaryEvaluation(Harness harness) {
        for (int i = 0; i < 8; i++) {
            if (harness.getEndpoint(UfoEndpoint.class).isElementVisible(2, OBR001_S001_PrintInstructionPage.OBR001_S001_PAGE_LABEL_XPATH, Lookup.XPATH)) {
                harness.getEndpoint(UfoEndpoint.class).click(() -> OBR001_S001_PrintInstructionPage.RIGHT_BUTTON);
            } else {
                break;
            }
        }
        return start -> start
                .onPreliminaryEvaluationPage();
    }

    public Function<KUL006_S001_ListOfPersonsOnRequestPage, KUL006_S001_ListOfPersonsOnRequestPage> addAssociatedMember(NonClient associatedMember) {
        return start -> start
                .addPersonToRequest()
                .then(fillDataAboutAssociatedMember(associatedMember))
                .then(fillAssociatedFonCif(associatedMember))
                .then(newStatutoryAuthority.fillStatutoryAuthorityData(associatedMember))
                .clickSignedPersonallyPhysicallyAndContinue()
                .onPrintPage()
                .saveDocumentUrl()
                .closePrintWindow()
                .onSignerPage()
                .clickRightButton()
                .onClientContractSignPage()
                .clickLeftButton()
                .onScanDocumentsPage()
                .clickSkipButton()
                .onClientConsentsPoPage()
                .clickYes()
                .continueToSignerSelectionPage()
                .clickSignedPersonallyPhysicallyAndContinue()
                .onPrintPage()
                .saveDocumentUrl()
                .closePrintWindow()
                .onPrintInstructionPage()
                .clickRightButton()
                .onClientRequestDetailPage()
                .clickFopAssociationMemberRadio()
                .clickContinue()
                .clickPermanentResidencyFopRadio()
                .clickContinue()
                .onListOfPersonsOnRequestPage();
    }

    public Function<KUL016_S001_FirstDataAboutClientPage, CIF111_S016_CreateNewCifPage> fillDataAboutAssociatedMember(NonClient associatedMember) {
        return start -> start
                .fillFirstName(associatedMember.firstName())
                .fillLastName(associatedMember.lastName())
                .fillBirthNumber(associatedMember.birthNumber())
                .dismissAlertIfVisibleAndContinue()
                .clickCenterButton()
                .onIdDocumentsCopy()
                .then(newClientTasks.selectDocumentIdCard("OP se strojově čitelnými údaji"))
                .onCreateNewCifpage();
    }

    public Function<CIF111_S016_CreateNewCifPage, CIF111_S001_NewCifDataNavigation> fillAssociatedFonCif(NonClient associatedMember) {
        return start -> start
                .selectIdType("Občanský průkaz")
                .fillIdCardNumber(associatedMember.idNumber())
                .clickContinue()
                .then(harness.tasks().ufo().banka().newClient().skipNotRegisteredWarningIfVisible())
                .tryCloseIdentificationPopup();
    }

    public Function<KUL004_S008_ClientRequestDetailPage, KUL004_S004_AddressesAndContactsPage> chooseNoFamilyCompany() {
        return start -> start
                .selectFamilyCompany("Ne")
                .clickContinue();
    }

    public Function<KUL004_S004_AddressesAndContactsPage, KUL012_S001_RequestedDocumentsPage> selectPermanentResidency() {
        return start -> start
                .clickPermanentResidencyRadio()
                .clickContinue()
                .onRequestedDocumentsPage();
    }

    public Function<KUL010_S001_FinancialSignpostPage, KUL071_S001_RequestForSMEPage> continueToRequestPrint() {
        return start -> start
                .continueToListOfPersonsOnRequestPage()
                .clickContinue()
                .onRequestSummaryPage()
                .clickContinue()
                .onRequestForSMEPage();
    }

    public Function<KUL071_S001_RequestForSMEPage, KUL071_S001_RequestForSMEPage> printRequest() {
        return start -> start
                .clickPrintRequest()
                .onPrintPagePSP004()
                .saveDocumentUrl()
                .closePrintWindow()
                .onRequestForSMEPage();
    }

    public Function<KUL071_S001_RequestForSMEPage, KUL071_S001_RequestForSMEPage> zebAndEasiPrintRequest() {
        return start -> start
                .clickPrintRequest()
                .onPrintPagePSP012()
                .closePrintWindow()
                .onPrintPage()
                .saveDocumentUrl()
                .closePrintWindow()
                .onRequestForSMEPage();
    }

    public Function<KUL004_S004_AddressesAndContactsPage, KUL006_S001_ListOfPersonsOnRequestPage> selectPermanentResidencyForFop() {
        return start -> start
                .clickPermanentResidencyFopRadio()
                .clickContinue()
                .onListOfPersonsOnRequestPage();
    }

    public Function<KUL004_S004_AddressesAndContactsPage, KUL012_S001_RequestedDocumentsPage> selectCompanyHeadquarter() {
        return start -> start
                .clickCompanyHeadquarterRadio()
                .clickContinue()
                .onRequestedDocumentsPage();
    }

    public Function<KUL004_S001_EditBusinessInfoPage, KUL004_S004_AddressesAndContactsPage> fillBusinessInfoGreenAndConsolidation() {
        return start -> start
                .fillBaseBusinessCapital("200000")
                .selectBusinessForCertainTime("Ne")
                .fillNumberOfPersonsActCompany("1")
                .fillNumberOfEmployees("1")
                .selectStatutoryAuthority("Jednatel")
                .clickFirstEconomicActivityRadio()
                .fillRevenueShare("100")
                .clickNoActivityInterrupted()
                .clickContinue()
                .onAddressesAndContactsPage();
    }

    public Function<KUL006_S001_ListOfPersonsOnRequestPage, KUL008_S001_ModulationPage> zebAndEasiPrintRequestPo(Harness harness) {
        return start -> start
                .clickContinue()
                .onRequestSummaryPage()
                .clickContinue()
                .onPrintPagePSP012()
                .closePrintWindow()
                .onPrintPage()
                .saveDocumentUrl()
                .closePrintWindow()
                .onSmeApplicationSignPage()
                .clickContinue()
                .onWaitingForModulationPage()
                .waitForModulation(120, 7)
                .onModulationPage();
    }

    public Function<DMS001_S001_ScanDocumentsPageTransitions, HKO101_S001_MainClientPage> sendDocumentsForMonetaBusinessCardIfPageShownOtherwiseSkip(Harness harness) {
        if (harness.getEndpoint(UfoEndpoint.class).isElementVisible(2, BPP001_S001_DocumentationSendPage.SEND_DOCUMENTS_BUTTON, Lookup.ID)) {
            return start -> start
                    .onDocumentationSendPage()
                    .sendDocumentsToClient()
                    .clickLeftButton()
                    .onMainClientPage();
        } else {
            return start -> start
                    .onMainClientPage();
        }
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> storeAccountNumber(String applicationId, String application) {
        return start -> start
                .selectFromListOfRequests()
                .clickRequest(applicationId)
                .clickOpenRequestDetail()
                .onSMEAutomatsApplicationsOverview()
                .storeAccountNumber(application)
                .clickBackToApplicationsList()
                .clickBackToMainPage();
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> checkEstablishedExpresBusinessAccountDetail(Harness harness) {
        return start -> start
                .clickExpressBusinessDetail(harness.get(ACCOUNT_NUMBER_KEY))
                .checkAccountState("Otevřený")
                .checkLimitsCredit(harness.get(LIMITS_CREDIT_KEY))
                .checkInterestRate(harness.get(INTEREST_RATE_KEY))
                .checkAnnuityPayments("Ano")
                .checkPaymentsCount(harness.get(PAYMENTS_COUNT_KEY))
                .clickBackToHKO();
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> checkEstablishedKontokorentDetail(String accountNumber) {
        return start -> start
                .clickKontokorentDetail()
                .checkAccountNumberInternal(accountNumber)
                .returnToMainClient();
    }

    public Function<SMA002_S001_SMEAutomatsApplicationOverview, SMA002_S001_SMEAutomatsApplicationOverview> setParametersForExpresAndFlexiBusinessApplications(Harness harness) {
        return start -> start
                .then(setParametersForExpresBusinessApplication(harness))
                .then(setParametersForFlexiBusinessApplication(harness));
    }

    public Function<SMA002_S001_SMEAutomatsApplicationOverview, SMA002_S001_SMEAutomatsApplicationOverview> setParametersForExpresBusinessApplication(Harness harness) {
        return start -> start
                .clickApplication("Expres Business")
                .clickParametersVariants("Expres Business")
                .onChangeSelectedParametersExpresBusinessPageAndAcceptAlertIfPresent()
                .fillSuggestedSummaryInterestRate(harness)
                .selectInterestRateType("Pevná po celou dobu splácení")
                .selectBenefitFromCampaign("Benefit nezvolen")
                .selectFirstValidRepaymentCharge()
                .clickRepayInsuranceChargeNo()
                .clickRecalculate()
                .then(storeApplicationDetails())
                .clickContinue();
    }

    public Function<SMA002_S001_SMEAutomatsApplicationOverview, SMA002_S001_SMEAutomatsApplicationOverview> setParametersForFlexiBusinessApplication(Harness harness) {
        return start -> start
                .clickApplication("Flexi Business")
                .clickParametersVariants("Flexi Business")
                .onChangeSelectedParametersFlexiBusinessPageAndAcceptAlertIfPresent()
                .clickRecalculateAndSave()
                .clickContinue();
    }

    public Function<KUL043_S001_ChangeSelectedParametersPage, KUL043_S001_ChangeSelectedParametersPage> storeApplicationDetails() {
        return start -> start
                .storeLimitsCredit()
                .storeAnnuityPayment()
                .storePaymentsCount();
    }

    public Function<SMA002_S001_SMEAutomatsApplicationsOverviewTransitions, SMA002_S001_SMEAutomatsApplicationOverview> waitForModulationIfPresent(int timeout) {
        UfoEndpoint endpoint = harness.getEndpoint(UfoEndpoint.class);
        if (endpoint.isElementVisible(10, CHK001_S001_PAGE_LABEL_XPATH, Lookup.XPATH)) {
            return start -> start.onWaitingForModulationPage()
                    .waitForModulation(timeout)
                    .onSMEAutomatsApplicationOverviewPage();
        } else {
            return start -> start
                    .onSMEAutomatsApplicationOverview();
        }
    }

    public Function<KUL004_S001_EditBusinessInfoPage, KUL004_S001_EditBusinessInfoPage> editBusinessInfoCooperativeOwnership() {
        return start -> start
                .fillNumberOfPersonsActCompany("1")
                .fillNumberOfEmployees("0")
                .clickFirstEconomicActivityRadio()
                .fillRevenueShare("100");
    }

    public Function<KUL004_S001_EditBusinessInfoPageTransitions, KUL004_S001_EditBusinessInfoPageTransitions> loadBusinessNameIfNecessaryAndContinue() {
        UfoEndpoint endpoint = this.harness.getEndpoint(UfoEndpoint.class);
        if (endpoint.isElementVisible(5, KUL004_S004_PAGE_LABEL_XPATH, Lookup.XPATH))
            return start -> start;
        else {
            return start -> start
                    .onEditBusinessInfoPage()
                    .clickLoadBusinessName()
                    .clickContinue();
        }
    }

}