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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.cards.credit.HPO039_S001_MbcDetailPageTransitions;
import cz.moneta.test.dsl.ufo.banka.pages.cards.credit.KUL058_S002_ChangeCardFramePage;
import cz.moneta.test.dsl.ufo.banka.pages.cards.credit.KUL058_S002_ChangeCardFramePageTransitions;
import cz.moneta.test.dsl.ufo.banka.pages.fop.KUL004_S001_EditBusinessInfoPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.client.settings.ODS003_S001_AddCodeOfRecommendedPersonPage;
import cz.moneta.test.dsl.ufo.banka.pages.products.sme.KUL001_S001_SelectProductsPage;
import cz.moneta.test.dsl.ufo.banka.pages.products.sme.KUL004_S004_AddressesAndContactsPageTransitions;
import cz.moneta.test.dsl.ufo.banka.pages.products.sme.KUL066_S001_ProcessedRequestsPage;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.Lookup;

import java.util.function.Function;

public class ClipDeclipFopTasks {

    private final NewSmeAutomatSubTasks smeAutomatSubTasks;

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

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> declipMbc(Harness harness) {
        return start -> start
                .clickMbcDetail()
                .mouseOverFrameChangeAndDecrease()
                .then(skipProcessedRequestsIfOpenedDeclip(harness))
                .changeActualFrameBy(-10000)
                .clickContinue()
                .then(setContactsIfShownOtherwiseSkipOnRecommendation(harness))
                .checkProductNotRecommended()
                .clickWithoutRecommendationAndDismissAlertIfvisible()
                .onBusinessInfoPage()
                .clickContinue()
                .onAdditionalBusinessData()
                .then(smeAutomatSubTasks.setExpresBusinessData())
                .clickContinue()
                .onListOfPersonsOnRequestPage()
                .clickContinue()
                .onRequestSummaryPage()
                .clickContinue()
                .onMainClientPage();
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> clipMbc(Harness harness, String lastYear, String yearBeforeLast) {
        return start -> start
                .clickMbcDetail()
                .mouseOverFrameChangeAndIncrease()
                .then(skipProcessedRequestsIfOpenedClip(harness))
                .selectProductToOnboard("Moneta Business Card")
                .clickAddProduct()
                .onChangeCardFramePage()
                .changeActualFrameBy(10000)
                .clickContinue()
                .then(setContactsIfShownOtherwiseSkipOnRecommendation(harness))
                .checkProductNotRecommended()
                .clickWithoutRecommendationAndDismissAlertIfvisible()
                .onSelectProductSmeCommercial()
                .clickContinue()
                .then(smeAutomatSubTasks.sendDocumentationIfPageShownOtherwiseSkipFop(harness))
                .clickContinue()
                .onAdditionalBusinessData()
                .then(smeAutomatSubTasks.setExpresBusinessData())
                .clickNotFamilyBusiness()
                .clickAddressSameAsPermanentResidence()
                .clickContinue()
                .onRequestedDocumentPage()
                .then(smeAutomatSubTasks.assignDocuments("Doklady FOP", "Živnostenský list / výpis z ŽR"))
                .clickContinue()
                .onFinancialSignpostPage()
                .clickNoPsd2Data()
                .then(smeAutomatSubTasks.fillFinancialSignpostDp(lastYear, yearBeforeLast))
                .continueToListOfPersonsOnRequestPage()
                .clickContinue()
                .onRequestSummaryPage()
                .clickContinue()
                .onRequestForSMEPage()
                .clickPrintRequest()
                .onPrintPagePSP004()
                .saveDocumentUrl()
                .closePrintWindow()
                .onRequestForSMEPage()
                .clickYes()
                .onWaitingForModulationPage()
                .waitForModulation(120)
                .onModulationPage()
                .clickDocumentScanning()
                .onScanDocumentsPage()
                .then(smeAutomatSubTasks.assignScannedDocuments(harness))
                .clickContinue()
                .onDocumentationSendPage()
                .sendDocumentsToClient()
                .clickLeftButton()
                .onMainClientPage();
    }

    /***
     * Stranka so zadanim kontaktnych informacii sa preskakuje v pripade, ze uz boli zadane a pokracuje na doporucenie.
     */
    public Function<KUL058_S002_ChangeCardFramePageTransitions, ODS003_S001_AddCodeOfRecommendedPersonPage> setContactsIfShownOtherwiseSkipOnRecommendation(Harness harness) {
        if (harness.getEndpoint(UfoEndpoint.class).isElementVisible(2, KUL058_S002_ChangeCardFramePage.KUL058_S002_PAGE_LABEL_XPATH, Lookup.XPATH)) {
            return start -> start
                    .onContactSettingsForSendingEmailsPage()
                    .clickFillPhoneFromCif()
                    .clickFillEmailFromCif()
                    .clickContinue()
                    .onAddCodeOfRecommendedPersonPage();
        } else {
            return start -> start
                    .onAddCodeOfRecommendedPersonPage();
        }
    }

    public Function<KUL004_S001_EditBusinessInfoPage, KUL004_S004_AddressesAndContactsPageTransitions> setBusinessDetail() {
        return start -> start
                .clickContinue()
                .onClientRequestDetailPage()
                .selectFamilyCompany("Ne")
                .clickContinue()
                .clickPermanentResidencyRadio()
                .clickContinue();
    }

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

    /***
     * Stranka s rozpracovanymi ziadostami sa preskakuje v pripade, ze klient nejake rozpracovane ziadosti uz ma.
     */
    public Function<HPO039_S001_MbcDetailPageTransitions, KUL001_S001_SelectProductsPage> skipProcessedRequestsIfOpenedClip(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();
        }
    }
}
