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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.cards.debit.OBR003_S001_ClientContractSignPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisProductClass;
import cz.moneta.test.dsl.ufo.banka.pages.products.debit.card.SIF004_S001_ContactSettingsForSendingEmailsPageTransitions;
import cz.moneta.test.dsl.ufo.banka.pages.products.sme.KUL001_S001_SelectProductsPage;
import cz.moneta.test.dsl.ufo.banka.pages.products.sme.KUL002_S001_ProductDetailPage;
import cz.moneta.test.dsl.ufo.banka.pages.products.sme.KUL010_S001_FinancialSignpostPage;
import cz.moneta.test.dsl.ufo.banka.pages.requests.BAL006_S002_ClientRequestPage;

import java.util.function.Function;

import static cz.moneta.test.dsl.ufo.banka.pages.cards.credit.OPN008_S001_SignContractPage.APPLICATION_ID_KEY;

public class NewSmeAutomatPoTasks {

    private final NewSmeAutomatSubTasks smeAutomatSubTasks;

    public NewSmeAutomatPoTasks(Harness harness) {
        this.smeAutomatSubTasks = harness.tasks().ufo().banka().newSmeAutomatSubTasks();
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> withExpresBusiness(Harness harness, String loanExpresAmount, String lastYear, String yearBeforeLast, String financingReason) {
        return start -> start
                .createNewProduct(HCisProductClass.SME_AUTOMATS)
                .then(smeAutomatSubTasks.selectSmeAutomatPo(harness))
                .selectExpresBusiness("Expres Business")
                .then(smeAutomatSubTasks.fillProductDetailsExpresBusiness(financingReason, loanExpresAmount))
                .clickContinue()
                .clickContinue()
                .then(smeAutomatSubTasks.sendDocumentationIfPageShownOtherwiseSkipPo(harness))
                .then(smeAutomatSubTasks.fillBusinessInformation("1"))
                .then(smeAutomatSubTasks.selectCompanyHeadquarter())
                .then(smeAutomatSubTasks.assignDocuments("Doklady PO", "Výpis z OR"))
                .clickContinue()
                .onFinancialSignpostPage()
                .then(smeAutomatSubTasks.fillFinancialSignpostDp(lastYear, yearBeforeLast))
                .continueToListOfPersonsOnRequestPage()
                .then(smeAutomatSubTasks.selectStatutoryAuthority("200000,00"))
                .then(smeAutomatSubTasks.selectPermanentResidencyForFop())
                .then(smeAutomatSubTasks.finalizeSmeForPo(harness))
                .then(smeAutomatSubTasks.uploadScannedDocuments(harness))
                .onMainClientPage();
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> withFlexiBusiness(Harness harness, String loanFlexiAmount, String lastYear, String yearBeforeLast) {
        return start -> start
                .createNewProduct(HCisProductClass.SME_AUTOMATS)
                .then(smeAutomatSubTasks.selectSmeAutomatPo(harness))
                .selectFlexiBusiness("Flexi Business")
                .then(smeAutomatSubTasks.fillProductDetailsFlexiBusinessPo(loanFlexiAmount))
                .clickContinue()
                .onSelectProductsPage()
                .clickContinue()
                .then(smeAutomatSubTasks.sendDocumentationIfPageShownOtherwiseSkipPo(harness))
                .then(smeAutomatSubTasks.fillBusinessInformation("1"))
                .then(smeAutomatSubTasks.selectCompanyHeadquarter())
                .then(smeAutomatSubTasks.assignDocuments("Doklady PO", "Výpis z OR"))
                .clickContinue()
                .onFinancialSignpostPage()
                .then(smeAutomatSubTasks.fillFinancialSignpostDp(lastYear, yearBeforeLast))
                .continueToListOfPersonsOnRequestPage()
                .then(smeAutomatSubTasks.selectStatutoryAuthority("200000,00"))
                .then(smeAutomatSubTasks.selectPermanentResidencyForFop())
                .then(smeAutomatSubTasks.finalizeSmeForPo(harness))
                .then(smeAutomatSubTasks.uploadScannedDocuments(harness))
                .onMainClientPage();
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> withMonetaBusinessCard(Harness harness, String dummyEmail, String lastYear, String yearBeforeLast) {
        return start -> start
                .createNewProduct(HCisProductClass.SME_AUTOMATS)
                .then(smeAutomatSubTasks.selectSmeAutomatPo(harness))
                .then(addMonetaBusinessCard())
                .onSelectProductsPage()
                .clickContinue()
                .then(smeAutomatSubTasks.sendDocumentationIfPageShownOtherwiseSkipPo(harness))
                .then(smeAutomatSubTasks.editEmailsToDummyPo(dummyEmail))
                .then(smeAutomatSubTasks.fillBusinessInformation("1"))
                .then(smeAutomatSubTasks.selectCompanyHeadquarter())
                .then(smeAutomatSubTasks.assignDocuments("Doklady PO", "Výpis z OR"))
                .clickContinue()
                .onFinancialSignpostPage()
                .then(smeAutomatSubTasks.fillFinancialSignpostDp(lastYear, yearBeforeLast))
                .continueToListOfPersonsOnRequestPage()
                .then(smeAutomatSubTasks.selectStatutoryAuthority("200000,00"))
                .then(smeAutomatSubTasks.selectPermanentResidencyForFop())
                .then(smeAutomatSubTasks.finalizeSmeForPo(harness))
                .then(smeAutomatSubTasks.uploadScannedDocuments(harness))
                .onMainClientPage();
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> withReserveProBusiness(Harness harness, String dummyEmail, String loanReserveAmount, String lastYear, String yearBeforeLast) {
        return start -> start
                .createNewProduct(HCisProductClass.SME_AUTOMATS)
                .then(smeAutomatSubTasks.selectSmeAutomatPo(harness))
                .selectFlexiBusiness("Rezerva PRO podnikání")
                .fillRequiredLoanAmount(loanReserveAmount)
                .clickContinue()
                .onSelectProductsPage()
                .clickContinue()
                .then(smeAutomatSubTasks.sendDocumentationIfPageShownOtherwiseSkipPo(harness))
                .then(smeAutomatSubTasks.editEmailsToDummyPo(dummyEmail))
                .then(smeAutomatSubTasks.fillBusinessInformation("1"))
                .then(smeAutomatSubTasks.selectCompanyHeadquarter())
                .then(smeAutomatSubTasks.assignDocuments("Doklady PO", "Výpis z OR"))
                .clickContinue()
                .onFinancialSignpostPage()
                .then(smeAutomatSubTasks.fillFinancialSignpostDp(lastYear, yearBeforeLast))
                .continueToListOfPersonsOnRequestPage()
                .then(smeAutomatSubTasks.selectStatutoryAuthority("200000,00"))
                .then(smeAutomatSubTasks.selectPermanentResidencyForFop())
                .then(smeAutomatSubTasks.finalizeSmeForPo(harness))
                .then(smeAutomatSubTasks.uploadScannedDocuments(harness))
                .onMainClientPage();
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> withGreenExpresBusiness(Harness harness, String dummyEmail, String loanGreenAmount, String lastYear, String yearBeforeLast) {
        return start -> start
                .createNewProduct(HCisProductClass.SME_AUTOMATS)
                .then(smeAutomatSubTasks.selectSmeAutomatPo(harness))
                .selectExpresBusiness("Zelený Expres Business")
                .then(smeAutomatSubTasks.fillProductDetailsExpresBusiness("Zelené střechy", loanGreenAmount))
                .clickContinue()
                .clickContinue()
                .then(smeAutomatSubTasks.sendDocumentationIfPageShownOtherwiseSkipPo(harness))
                .then(smeAutomatSubTasks.editEmailsToDummyPo(dummyEmail))
                .then(smeAutomatSubTasks.fillBusinessInfoGreenAndConsolidation())
                .then(smeAutomatSubTasks.selectCompanyHeadquarter())
                .then(smeAutomatSubTasks.assignDocuments("Doklady PO", "Výpis z OR"))
                .then(smeAutomatSubTasks.assignDocuments("Čestné prohlášení Zelený EB", "Čestné prohlášení Zelený EB"))
                .then(smeAutomatSubTasks.assignDocuments("Projekt / Záměr / Smlouva", "Projekt / Záměr / Smlouva"))
                .clickContinue()
                .onFinancialSignpostPage()
                .then(smeAutomatSubTasks.fillFinancialSignpostDp(lastYear, yearBeforeLast))
                .continueToListOfPersonsOnRequestPage()
                .then(smeAutomatSubTasks.selectStatutoryAuthority("200000,00"))
                .then(smeAutomatSubTasks.selectPermanentResidencyForFop())
                .then(smeAutomatSubTasks.zebAndEasiPrintRequestPo(harness))
                .then(smeAutomatSubTasks.uploadScannedDocuments(harness))
                .onMainClientPage();
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> withExpresBusinessEasi(Harness harness, String dummyEmail, String loanEasiAmount, String lastYear, String yearBeforeLast) {
        return start -> start
                .createNewProduct(HCisProductClass.SME_AUTOMATS)
                .then(smeAutomatSubTasks.selectSmeAutomatPo(harness))
                .selectExpresBusiness("Expres Business EIF EaSI")
                .then(smeAutomatSubTasks.fillRequiredLoanEb(loanEasiAmount))
                .clickContinue()
                .clickContinue()
                .then(smeAutomatSubTasks.sendDocumentationIfPageShownOtherwiseSkipPo(harness))
                .then(smeAutomatSubTasks.editEmailsToDummyPo(dummyEmail))
                .then(smeAutomatSubTasks.fillBusinessInformation("1"))
                .then(smeAutomatSubTasks.selectCompanyHeadquarter())
                .then(smeAutomatSubTasks.assignDocuments("Doklady PO", "Výpis z OR"))
                .then(smeAutomatSubTasks.assignDocuments("Čestné prohlášení EaSI", "Čestné prohlášení EaSI"))
                .clickContinue()
                .onFinancialSignpostPage()
                .then(smeAutomatSubTasks.fillFinancialSignpostDp(lastYear, yearBeforeLast))
                .continueToListOfPersonsOnRequestPage()
                .then(smeAutomatSubTasks.selectStatutoryAuthority("200000,00"))
                .then(smeAutomatSubTasks.selectPermanentResidencyForFop())
                .then(smeAutomatSubTasks.zebAndEasiPrintRequestPo(harness))
                .then(smeAutomatSubTasks.uploadScannedDocumentsEaSI(harness));
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> withBusinessCreditConsolidation(Harness harness, String dummyEmail, String loanConsolidationAmount, String lastYear, String yearBeforeLast) {
        return start -> start
                .createNewProduct(HCisProductClass.SME_AUTOMATS)
                .then(smeAutomatSubTasks.selectSmeAutomatPo(harness))
                .selectExpresBusiness("Konsolidace podnikatelských úvěrů")
                .then(smeAutomatSubTasks.fillProductDetailsExpresBusiness("nákup strojů, výrobních zařízení", loanConsolidationAmount))
                .clickContinue()
                .clickContinue()
                .then(smeAutomatSubTasks.sendDocumentationIfPageShownOtherwiseSkipPo(harness))
                .then(smeAutomatSubTasks.editEmailsToDummyPo(dummyEmail))
                .then(smeAutomatSubTasks.fillBusinessInfoGreenAndConsolidation())
                .then(smeAutomatSubTasks.selectCompanyHeadquarter())
                .then(smeAutomatSubTasks.assignDocuments("Doklady PO", "Výpis z OR"))
                .clickContinue()
                .onFinancialSignpostPage()
                .then(smeAutomatSubTasks.fillFinancialSignpostDp(lastYear, yearBeforeLast))
                .continueToListOfPersonsOnRequestPage()
                .then(smeAutomatSubTasks.selectStatutoryAuthority("200000,00"))
                .then(smeAutomatSubTasks.selectPermanentResidencyForFop())
                .clickContinue()
                .onRequestSummaryPage()
                .clickContinue()
                .onPrintPage()
                .saveDocumentUrl()
                .closePrintWindow()
                .onSmeApplicationSignPage()
                .clickContinue()
                .onPrintInstructionPage()
                .then(smeAutomatSubTasks.waitForPreliminaryEvaluation(harness))
                .clickContinueToConsolidationCommitmentsPage()
                .addCommitmentManually()
                .then(smeAutomatSubTasks.setCommitment())
                .clickContinueToModulationPage()
                .waitForModulation(120, 7)
                .onModulationPage()
                .clickCompleteCommitmentInfoButton()
                .onCompleteCommitmentsPage()
                .clickFirstCommitment()
                .clickEditCommitment()
                .then(smeAutomatSubTasks.fillCommitmentData())
                .clickContinueToModulationPage()
                .waitForModulation(120, 7)
                .onModulationPage()
                .then(smeAutomatSubTasks.uploadScannedDocumentsConsolidation(harness));
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> withExpresBusinessAndFlexiBusinessAndMonetaBusinessCard(Harness harness, String loanExpresAmount, String loanFlexiAmount, String lastYear, String yearBeforeLast, String financingReason) {
        return start -> start
                .createNewProduct(HCisProductClass.SME_AUTOMATS)
                .then(smeAutomatSubTasks.selectSmeAutomatPo(harness))
                .selectExpresBusiness("Expres Business")
                .then(smeAutomatSubTasks.fillProductDetailsExpresBusiness(financingReason, loanExpresAmount))
                .clickContinue()
                .selectFlexiBusiness("Flexi Business")
                .then(smeAutomatSubTasks.fillProductDetailsFlexiBusinessPo(loanFlexiAmount))
                .clickContinue()
                .onSelectProductsPage()
                .then(addMonetaBusinessCard())
                .onSelectProductsPage()
                .clickContinue()
                .then(smeAutomatSubTasks.sendDocumentationIfPageShownOtherwiseSkipPo(harness))
                .then(smeAutomatSubTasks.fillBusinessInformation("1"))
                .then(smeAutomatSubTasks.selectCompanyHeadquarter())
                .then(smeAutomatSubTasks.assignDocuments("Doklady PO", "Výpis z OR"))
                .clickContinue()
                .onFinancialSignpostPage()
                .then(smeAutomatSubTasks.fillFinancialSignpostDp(lastYear, yearBeforeLast))
                .continueToListOfPersonsOnRequestPage()
                .then(smeAutomatSubTasks.selectStatutoryAuthority("200000,00"))
                .then(smeAutomatSubTasks.selectPermanentResidencyForFop())
                .then(smeAutomatSubTasks.finalizeSmeForPo(harness))
                .then(smeAutomatSubTasks.uploadScannedDocuments(harness))
                .then(smeAutomatSubTasks.sendDocumentsForMonetaBusinessCardIfPageShownOtherwiseSkip(harness));
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> withExpresBusinessAndFlexiBusinessAndMonetaBusinessCardWithTwoRepresentatives(Harness harness, String loanExpresAmount, String loanFlexiAmount, String lastYear, String yearBeforeLast, String financingReason) {
        return start -> start
                .createNewProduct(HCisProductClass.SME_AUTOMATS)
                .then(smeAutomatSubTasks.selectSmeAutomatPo(harness))
                .selectExpresBusiness("Expres Business")
                .then(smeAutomatSubTasks.fillProductDetailsExpresBusiness(financingReason, loanExpresAmount))
                .clickContinue()
                .selectFlexiBusiness("Flexi Business")
                .then(smeAutomatSubTasks.fillProductDetailsFlexiBusinessPo(loanFlexiAmount))
                .clickContinue()
                .onSelectProductsPage()
                .then(addMonetaBusinessCard())
                .onSelectProductsPage()
                .clickContinue()
                .then(smeAutomatSubTasks.sendDocumentationIfPageShownOtherwiseSkipPo(harness))
                .then(smeAutomatSubTasks.fillBusinessInformation("2"))
                .then(smeAutomatSubTasks.selectCompanyHeadquarter())
                .then(smeAutomatSubTasks.assignDocuments("Doklady PO", "Výpis z OR"))
                .clickContinue()
                .onFinancialSignpostPage()
                .then(smeAutomatSubTasks.fillFinancialSignpostDp(lastYear, yearBeforeLast))
                .continueToListOfPersonsOnRequestPage()
                .then(smeAutomatSubTasks.selectStatutoryAuthority("100000,00"))
                .then(smeAutomatSubTasks.selectPermanentResidencyForFop())
                .then(smeAutomatSubTasks.selectSecondStatutoryAuthority("100000,00"))
                .then(smeAutomatSubTasks.selectPermanentResidencyForFop())
                .then(smeAutomatSubTasks.finalizeSmeForPo(harness))
                .then(smeAutomatSubTasks.uploadScannedDocuments(harness))
                .then(smeAutomatSubTasks.sendDocumentsForMonetaBusinessCardIfPageShownOtherwiseSkip(harness));
    }

    public Function<KUL001_S001_SelectProductsPage, SIF004_S001_ContactSettingsForSendingEmailsPageTransitions> addMonetaBusinessCard() {
        return start -> start
                .selectProductToOnboard("Moneta Business Card")
                .clickAddProduct()
                .onCommercialCreditCardDetail()
                .then(smeAutomatSubTasks.fillCreditCardDetails())
                .then(smeAutomatSubTasks.fillContactSettings())
                .clickContinue();
    }

    public Function<OBR003_S001_ClientContractSignPage, HKO101_S001_MainClientPage> finishDrawingDownPo(Harness harness, String expectedState) {
        return start -> start
                .clickRightButton()
                .onClientContractSignPage()
                .checkSuccessfulVerification()
                .clickLeftButton()
                .onPrintPage()
                .saveDocumentUrl()
                .closePrintWindow()
                .onPrintPage()
                .closePrintWindow()
                .onPrintPage()
                .closePrintWindow()
                .onSMESignedContractDocumentation()
                .clickSigned()
                .waitForModulation(120)
                .onClientContractSignPage()
                .checkSuccessfulyEstablishedProducts()
                .clickLeftButton()
                .onClientRequestPage()
                .clickCancelFilter()
                .checkApplicationState(harness.get(APPLICATION_ID_KEY), expectedState)
                .clickBackToMainPage();
    }

    public Function<BAL006_S002_ClientRequestPage, OBR003_S001_ClientContractSignPage> drawDownFeeExpresBusiness(Harness harness, String bankerName) {
        return start -> start
                .selectStateFilter("PODMÍNĚNÝ SOUHLAS")
                .selectFirstApplication()
                .clickOpenRequestDetail()
                .onSMEAutomatsApplicationsOverview()
                .then(smeAutomatSubTasks.setParametersForExpresBusinessApplication(harness))
                .clickAcceptation()
                .clickAcceptationModalIfPresent()
                .then(smeAutomatSubTasks.waitForModulationIfPresent(120))
                .clickApplication("Expres Business")
                .clickPrintSignDeal()
                .onRequestedDocumentsPage()
                .selectAndAddDocument()
                .selectAndAddDocument()
                .clickContinue()
                .onScanDocumentsPage()
                .uploadAndAssignFakeDebtFreeConfirmationFU()
                .reloadPage()
                .uploadAndAssignFakeDebtFreeConfirmationCSSZ()
                .clickContinue()
                .onListOfPersonsPage()
                .clickFirstPerson()
                .clickEstablishEditCif()
                .clickSend()
                .onIncomeEditPage()
                .clickBackButton()
                .onPrintInstructionPage()
                .clickLeftButton()
                .onListOfPersons()
                .clickContinue()
                .clickCenterButton()
                .onWaitingForModulationPage()
                .waitForModulation(180)
                .onBankerRepresentaionForVerification()
                .fillBankerName(bankerName)
                .clickSearchBanker()
                .clickContinue()
                .storeApplicationId(harness);
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> withExpresBusinessCooperativeOwnership(Harness harness, String loanExpresAmount, String numberOfPayments, String lastYear, String yearBeforeLast, String financingReason) {
        return start -> start
                .createNewProduct(HCisProductClass.SME_AUTOMATS)
                .then(smeAutomatSubTasks.selectSmeAutomatPo(harness))
                .selectExpresBusiness("Expres Business pro SVJ a BD")
                .then(fillRequiredLoanEbCooperativeOwnership(loanExpresAmount, numberOfPayments))
                .selectFinancingReason(financingReason)
                .then(fillCooperativeOwnershipInfo())
                .clickLoadFinancedAddressFromCif()
                .clickContinue()
                .clickContinue()
                .then(smeAutomatSubTasks.sendDocumentationIfPageShownOtherwiseSkipPo(harness))
                .then(smeAutomatSubTasks.editBusinessInfoCooperativeOwnership())
                .clickContinue()
                .then(smeAutomatSubTasks.loadBusinessNameIfNecessaryAndContinue())
                .onAddressesAndContactsPage()
                .then(smeAutomatSubTasks.selectCompanyHeadquarter())
                .assignDocuments(6)
                .clickContinue()
                .onFinancialSignpostPage()
                .then(fillFinancialSignpostDpCooperativeOwnership(lastYear, yearBeforeLast))
                .then(fillOtherDataAboutEnterprise())
                .continueToListOfPersonsOnRequestPage()
                .then(smeAutomatSubTasks.selectStatutoryAuthorityCooperativeOwnership())
                .then(smeAutomatSubTasks.selectPermanentResidencyForFop())
                .then(smeAutomatSubTasks.finalizeSmeForPo(harness))
                .then(smeAutomatSubTasks.uploadScannedDocuments(harness))
                .onMainClientPage();
    }

    public Function<KUL002_S001_ProductDetailPage, KUL002_S001_ProductDetailPage> fillRequiredLoanEbCooperativeOwnership(String loanExpresAmount, String numberOfPayments) {
        return start -> start
                .fillRequiredLoanAmount(loanExpresAmount)
                .fillNumberOfPayments(numberOfPayments)
                .fillInterestRateApprovedByRecord("4")
                .selectPaymentDay("26")
                .selectFirstCurrentAccount();
    }

    public Function<KUL002_S001_ProductDetailPage, KUL002_S001_ProductDetailPage> fillCooperativeOwnershipInfo() {
        return start -> start.fillResidentalUnitsCount("20")
                .fillNonResidentalUnitsCount("2")
                .fillMonthlyContributionToFondAmount("200000");
    }

    public Function<KUL002_S001_ProductDetailPage, KUL002_S001_ProductDetailPage> fillFinancedObjectAddress() {
        return start -> start
                .fillFinancedObjectStreet("Dr. Milady Horákové")
                .fillFinancedObjectDescriptiveNumber("786")
                .fillFinancedObjectCity("Rožnov pod Roadhoštěm")
                .fillFinancedObjectZipCode("75661")
                .clickFinancedObjectCzechCountry();
    }

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

    public Function<KUL010_S001_FinancialSignpostPage, KUL010_S001_FinancialSignpostPage> fillCooperativeOwnershipDetails() {
        return start -> start
                .fillDebtorsCount("0")
                .fillAmountOwedSummary("0")
                .fillApproximateBalancesOnAllBankAccounts("1000000");
    }

    public Function<KUL010_S001_FinancialSignpostPage, KUL010_S001_FinancialSignpostPage> fillOtherDataAboutEnterprise() {
        return start -> start
                .fillBusinessLoanOutsideMmb("0")
                .fillMonthlyPaymentsOutsideMMB("0")
                .clickYesTaxAdvisor();
    }
}
