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.common.pages.TIS004_S002_SignerSelectionPage;
import cz.moneta.test.dsl.ufo.banka.pages.common.pages.TIS046_S001_DocumentsProvidedToClientPage;
import cz.moneta.test.dsl.ufo.banka.pages.ib.ADC047_S001_IbSettingsPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.ADC012_S001_SettingADC;
import cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.client.settings.PVX002_S002_AccountDispositionOwnerPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.client.settings.PVX002_S002_AccountDispositionOwnerPageTransitions;
import cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisProductClass;
import cz.moneta.test.dsl.ufo.banka.pages.main.components.BUF051_S001_CreateCurrentAccountPage;
import cz.moneta.test.dsl.ufo.banka.pages.products.current.account.BPP001_S001_DocumentationSendPage;
import cz.moneta.test.dsl.ufo.banka.pages.products.current.account.BUF001_S001_CurrentAccountPage;
import cz.moneta.test.dsl.ufo.banka.pages.requests.DUP009_S001_CreateElectronicDistributionChannel;
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;

public class NewCurrentAccountTasks {

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> createCurrentAccountFop(Harness harness, NonClient nonClient) {
        return start -> start
                .then(selectCurrentAccountProduct())
                .then(sendDocumentationIfPageShownOtherwiseSkip(harness, nonClient))
                .then(fillCurrentAccountFormFop())
                .then(setDisposition())
                .then(setAdcChannelsAndPrint())
                .then(summaryOfFoundedProducts());
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> createCurrentAccountPo(Harness harness, NonClient nonClient) {
        return start -> start
                .then(selectCurrentAccountProduct())
                .then(sendDocumentationIfPageShownOtherwiseSkip(harness, nonClient))
                .then(fillCurrentAccountFormPo())
                .clickRightButton()
                .onSettingADCPage()
                .then(setAdcChannelsAndPrintPo())
                .then(summaryOfFoundedProducts());
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> createCurrentAccountFon() {
        return start -> start
                .then(fillContactAddress())
                .then(fillCurrentAccountDetails())
                .clickContinue()
                .onPrintPage()
                .closePrintWindow()
                .onPrintInstructionPage()
                .clickRightButton()
                .onDocumentsProvidedToClientPage()
                .clickContinue()
                .onAccountDispositionOwnerPage()
                .clickOwnerOnly()
                .onPrintPage()
                .closePrintWindow()
                .onPrintInstructionPage()
                .then(fillAdcDetails())
                .clickContinue()
                .onPrintInstructionPage()
                .clickCenterButton()
                .onAddCodeOfRecommendedPersonPage()
                .checkProductNotRecommended()
                .clickWithoutRecommendationAndDismissAlertIfvisible()
                .onPrintInstructionPage()
                .clickCenterButton()
                .onDispositionGeneratePage()
                .clickGenerateNow()
                .onPrintPage()
                .closePrintWindow()
                .onPrintPage()
                .closePrintWindow()
                .onPrintInstructionPage()
                .clickRightButton()
                .onSummaryStartDocumentsPage()
                .clickContinue()
                .onMainClientPage();
    }

    private Function<HKO101_S001_MainClientPage, BUF001_S001_CurrentAccountPage> fillContactAddress() {
        return start -> start
                .createNewProduct(HCisProductClass.CURRENT_ACCOUNT)
                .onRegistrationCertificatePage()
                .clickRightButton()
                .onEnterContactAddressPage()
                .clickUseResidencyAsContactAndConfirm()
                .clickSave()
                .onCurrentAccountPage();
    }

    private Function<BUF001_S001_CurrentAccountPage, TIS004_S002_SignerSelectionPage> fillCurrentAccountDetails() {
        return start -> start
                .selectFeePlan("Tom Plus")
                .clickDispositionRadioYes()
                .clickConfirm()
                .onCitizenSignatureInformation();
    }

    private Function<OBR001_S001_PrintInstructionPage, TIS046_S001_DocumentsProvidedToClientPage> fillAdcDetails() {
        return start -> start
                .clickRightButton()
                .onPrintInstructionPage()
                .clickRightButton()
                .onPrintInstructionPage()
                .clickRightButton()
                .onIbPasswordKeyPage()
                .clickFillFromCifCheckbox()
                .scrollIntoSendButton()
                .clickContinue()
                .onDocumentsProvidedToClientPage();
    }

    private Function<HKO101_S001_MainClientPage, OBR001_S001_PrintInstructionPageTransitions> selectCurrentAccountProduct() {
        return start -> start
                .createNewProduct(HCisProductClass.CURRENT_ACCOUNT)
                .onRegistrationCertificatePage()
                .clickRightButton();
    }

    public Function<OBR001_S001_PrintInstructionPageTransitions, BUF051_S001_CreateCurrentAccountPage> sendDocumentationIfPageShownOtherwiseSkip(Harness harness, NonClient nonClient) {
        if (harness.getEndpoint(UfoEndpoint.class).isElementVisible(3, BPP001_S001_DocumentationSendPage.SEND_DOCUMENTS_BUTTON, Lookup.ID)) {
            return start -> start
                    .onDocumentationSendPage()
                    .fillEmail(nonClient.email())
                    .sendDocumentsToClient()
                    .clickLeftButton()
                    .onCurrentAccountPage();
        } else {
            return start -> start
                    .onCreateCurrentAccountPage();
        }
    }

    private Function<BUF051_S001_CreateCurrentAccountPage, PVX002_S002_AccountDispositionOwnerPage> fillCurrentAccountFormFop() {
        return start -> start
                .selectAccountFlag("Konto")
                .selectProductType("BU-Genius Business FOP/R/")
                .selectAccountType("Konto PRO podnikání")
                .changeMfbSetting()
                .selectDistributionType("Elektronicky IB")
                .continueToCreateElectronicDistributionChannel()
                .then(selectDistributionChannelFop());
    }

    private Function<BUF051_S001_CreateCurrentAccountPage, OBR001_S001_PrintInstructionPage> fillCurrentAccountFormPo() {
        return start -> start
                .selectAccountFlag("Konto")
                .selectProductType("BU-Genius Business PO/R/")
                .selectAccountType("Konto PRO podnikání")
                .selectDistributionType("Elektronicky IB")
                .continueToCreateElectronicDistributionChannel()
                .then(selectDistributionChannelPo());
    }

    private Function<DUP009_S001_CreateElectronicDistributionChannel, PVX002_S002_AccountDispositionOwnerPage> selectDistributionChannelFop() {
        return start -> start
                .selectDistributionChannel("IB Heslo/MK")
                .clickContinueDismissAlertIfPresent()
                .onAccountDispositionOwnerPage();
    }

    private Function<DUP009_S001_CreateElectronicDistributionChannel, OBR001_S001_PrintInstructionPage> selectDistributionChannelPo() {
        return start -> start
                .selectDistributionChannel("IB Heslo/MK")
                .clickContinueDismissAlertIfPresent()
                .onPrintInstructionPage();
    }

    private Function<PVX002_S002_AccountDispositionOwnerPage, ADC047_S001_IbSettingsPage> setDisposition() {
        return start -> start
                .then(setWithoutDisposition())
                .onIbSettingsPage();
    }

    private Function<PVX002_S002_AccountDispositionOwnerPage, PVX002_S002_AccountDispositionOwnerPageTransitions> setWithoutDisposition() {
        return start -> start
                .clickNotCreateNow()
                .clickSkipCreate();
    }

    private Function<ADC047_S001_IbSettingsPage, OBR001_S001_PrintInstructionPage> setAdcChannelsAndPrint() {
        return start -> start
                .clickActiveOperation()
                .fillDailyLimit("200000")
                .fillWeeklyLimit("0")
                .clickContinue()
                .clickModalWarningContinue()
                .onSettingsADCPage()
                .clickSaveAndAcceptAlert()
                .onPrintInstructionPage()
                .clickCenterButton()
                .onPrintInstructionPage()
                .clickRightButton()
                .onSignerSelectionsPage()
                .clickSignedAndContinueToPrintPage()
                .saveDocumentUrl()
                .closePrintWindow()
                .onPrintPage()
                .saveDocumentUrl()
                .closePrintWindow()
                .onPrintInstructionPage();
    }

    private Function<ADC012_S001_SettingADC, OBR001_S001_PrintInstructionPage> setAdcChannelsAndPrintPo() {
        return start -> start
                .clickSaveAndAcceptAlert()
                .onPrintInstructionPage()
                .clickCenterButton()
                .onDispositionGeneratePage()
                .clickGenerateNow()
                .onPrintPage()
                .saveDocumentUrl()
                .closePrintWindow()
                .onPrintPage()
                .saveDocumentUrl()
                .closePrintWindow()
                .onPrintInstructionPage();
    }

    private Function<OBR001_S001_PrintInstructionPage, HKO101_S001_MainClientPage> summaryOfFoundedProducts() {
        return start -> start
                .clickRightButton()
                .onSummaryStartDocumentsPage()
                .clickContinue()
                .onPrintInstructionPage()
                .clickRightButton()
                .onMainClientPage();
    }
}
