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_ClientContractSignPageTransitions;
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.main.HKO101_S001_MainClientPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisProductClass;
import cz.moneta.test.dsl.ufo.banka.pages.products.current.account.BPP001_S001_DocumentationSendPage;
import cz.moneta.test.dsl.ufo.banka.pages.products.one.click.BAL023_S001_RecapitulationOfClientDataPage;
import cz.moneta.test.dsl.ufo.banka.pages.products.one.click.BAL024_S001_OneClickPage;
import cz.moneta.test.dsl.ufo.banka.pages.products.one.click.BAL024_S001_OneClickPageTransitions;
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.main.codelists.HCisProductClass.ONE_CLICK;
import static cz.moneta.test.dsl.ufo.banka.pages.products.one.click.BAL024_S001_OneClickPage.AGREE_BUTTON;
import static cz.moneta.test.dsl.ufo.banka.pages.products.sme.CHK001_S001_WaitingForModulationPage.CHK001_S001_PAGE_LABEL_XPATH;

public class OneClickTasks {

    private final Harness harness;

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

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> createOneClickFop(Harness harness) {
        return start -> start
                .createNewProduct(HCisProductClass.ONE_CLICK)
                .onRegistrationCertificatePage()
                .clickRightButton()
                .then(sendDocumentationIfPageShownOtherwiseSkip(harness))
                .selectProductType("GB Complete")
                .selectCardType("Business")
                .clickContinue()
                .onRecapitulationOfClientDataPage()
                .clickContinueToScoringPage()
                .waitForLoader()
                .onModulationPage()
                .waitForModulation(60)
                .onPrintPage()
                .saveDocumentUrl()
                .closePrintWindow()
                .onPrintPage()
                .saveDocumentUrl()
                .closePrintWindow()
                .onSignerPage()
                .clickRightButton()
                .onClientContractSignPage()
                .clickCenterButton()
                .onWaitingForModulationPage()
                .waitForModulation(100)
                .onEmailSentToClientPage()
                .clickCenterButton()
                .onSummaryOfProductFoundationPage()
                .clickOk()
                .onMainClientPage();
    }

    /***
     * Novemu klientovi sa odosiela dokumentacia. V pripade existujuce klienta sa tento krok preskakuje.
     */
    public Function<OBR001_S001_PrintInstructionPageTransitions, BAL024_S001_OneClickPage> sendDocumentationIfPageShownOtherwiseSkip(Harness harness) {
        if (harness.getEndpoint(UfoEndpoint.class).isElementVisible(3, BPP001_S001_DocumentationSendPage.SEND_DOCUMENTS_BUTTON, Lookup.ID)) {
            return start -> start
                    .onDocumentationSendPage()
                    .sendDocumentsToClient()
                    .clickLeftButton()
                    .onOneClickPage();
        } else {
            return start -> start
                    .onOneClickPage();
        }
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> createOneClickPo(Harness harness) {
        return start -> start
                .createNewProduct(HCisProductClass.ONE_CLICK)
                .onRegistrationCertificatePage()
                .clickRightButton()
                .then(sendDocumentationIfPageShownOtherwiseSkip(harness))
                .selectProductType("Konto PRO podnikání")
                .selectCardType("Business")
                .clickContinue()
                .then(clickAgreeIfVisible())
                .clickContinueToScoringPage()
                .waitForLoader()
                .onModulationPage()
                .waitForModulation(60)
                .onPrintPage()
                .saveDocumentUrl()
                .closePrintWindow()
                .onPrintPage()
                .saveDocumentUrl()
                .closePrintWindow()
                .onPrintInstructionPage()
                .clickRightButton()
                .onClientContractSignPage()
                .clickCenterButton()
                .then(waitForModulationIfVisible())
                .clickCenterButton()
                .onSummaryOfProductFoundationPage()
                .clickOk()
                .onMainClientPage();
    }

    public Function<OBR003_S001_ClientContractSignPageTransitions, OBR001_S001_PrintInstructionPage> waitForModulationIfVisible() {
        UfoEndpoint endpoint = this.harness.getEndpoint(UfoEndpoint.class);
        if (endpoint.isElementVisible(5, CHK001_S001_PAGE_LABEL_XPATH, Lookup.XPATH)) {
            return start -> start
                    .onWaitingForModulationPage()
                    .waitForModulation(100)
                    .onEmailSentToClientPage();
        } else {
            return start -> start
                    .onPrintInstructionPage();
        }
    }

    public Function<BAL024_S001_OneClickPageTransitions, BAL023_S001_RecapitulationOfClientDataPage> clickAgreeIfVisible() {
        UfoEndpoint endpoint = this.harness.getEndpoint(UfoEndpoint.class);
        if (endpoint.isElementVisible(5, AGREE_BUTTON, Lookup.ID)) {
            return start -> start
                    .onOneClickPage()
                    .clickAgree()
                    .clickContinue()
                    .onRecapitulationOfClientDataPage();
        } else {
            return start -> start.onRecapitulationOfClientDataPage();
        }
    }


    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> createOneClickFon() {
        return start -> start.createNewProduct(ONE_CLICK)
                .onOneClickRetailPage()
                .clickNoProductMediatedByAdvisor()
                .clickNoDispositionRights()
                .clickNoSaStatementsEndOfMonth()
                .typeSaName("spořící účet")
                .clickPlasticCard()
                .selectCardDesign("Standard")
                .clickContinue()
                .onModulationPage()
                .waitForModulation(120)
                .onPrintPage()
                .closePrintWindow()
                .onPrintInstructionPage()
                .clickRightButton()
                .onDocumentsProvidedToClientPage()
                .clickContinue()
                .onPrintPage()
                .closePrintWindow()
                .onPrintInstructionPage()
                .clickRightButton()
                .onDocumentsProvidedToClientPage()
                .clickContinue()
                .onPrintPage()
                .closePrintWindow()
                .onPrintInstructionPage()
                .clickRightButton()
                .onClientContractSignPage()
                .clickCenterButton()
                .onWaitingForModulationPage()
                .waitForModulation(120)
                .onEmailSentToClientPage()
                .clickCenterButton()
                .onSummaryOfProductFoundationRetailPage()
                .clickArrangeAppointmentIfChecked()
                .clickContinueButton()
                .onMainClientPage();
    }
}
