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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.tasks.accountenums.DebitCardFeePlan;
import cz.moneta.test.dsl.ufo.banka.pages.common.pages.OBR001_S001_PrintInstructionPageTransitions;
import cz.moneta.test.dsl.ufo.banka.pages.loans.CAS001_S001_ScoringPageTransitions;
import cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage;
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.NPK001_S001_DebitCardRequestPage;
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.DEBIT_CARD;

public class DebitCardTasks {

    private final Harness harness;

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

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> createDebitCard(String debitCardType, DebitCardFeePlan debitCardFeePlan) {
        return start -> start
                .createNewProduct(DEBIT_CARD)
                .onRegistrationCertificatePage()
                .clickRightButton()
                .then(sendDocumentationIfPageShownOtherwiseSkip())
                .then(fillDebitCardInformation(debitCardType, debitCardFeePlan))
                .then(finalizeDebitCard());
    }

    public Function<NPK001_S001_DebitCardRequestPage, CAS001_S001_ScoringPageTransitions> fillDebitCardInformation(String debitCard, DebitCardFeePlan debitCardFeePlan) {
        return start -> start
                .selectCurrentAccountByIndex(2)
                .selectDebitCardType(debitCard)
                .typeActivatePassword("111111")
                .clickClientDataHolderButton()
                .clickEnterButton()
                .onPrintInstructionPage()
                .clickLeftButton()
                .onDebitCardRequestPage()
                .clickPermanentAddressForPinRadio()
                .dismissPinChargeAlert()
                .clickCardDeliveryToPermanentAddress()
                .selectDebitCardFeePlan(debitCardFeePlan)
                .clickDesign()
                .clickSend()
                .onScoringPage()
                .waitForLoader();
    }

    public Function<CAS001_S001_ScoringPageTransitions, HKO101_S001_MainClientPage> finalizeDebitCard() {
        return start -> start
                .onModulationPage()
                .waitForModulation(120)
                .onSignerSelectionPageTIS050()
                .clickSignedAndContinueToPrintPage()
                .saveDocumentUrl()
                .closePrintWindow()
                .onPrintInstructionPage()
                .clickRightButton()
                .onClientContractSignPage()
                .clickCenterButton()
                .onWaitingForModulationPage()
                .waitForModulation(120)
                .onSummaryOfProductFoundationPage()
                .clickOk()
                .onMainClientPage();
    }

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