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

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.main.*;
import cz.moneta.test.dsl.ufo.banka.pages.products.current.account.HPO001_S001_CurrentAccountDetailsPage;
import cz.moneta.test.dsl.ufo.banka.pages.products.current.account.ZPL002_S003_ListOfSmoothPaymentsForVerificationPage;
import cz.moneta.test.dsl.ufo.banka.pages.products.current.account.ZPL_OBR001_SmoothPaymentInfo;
import cz.moneta.test.dsl.ufo.banka.pages.products.current.account.ZPL006_S002_ListOfForeignPaymentsPage;
import cz.moneta.test.dsl.ufo.banka.pages.search.HLO001_S001_ClientSearchPage;
import cz.moneta.test.dsl.ufo.banka.pages.search.HLO001_S001_ClientSearchPageTransition;
import cz.moneta.test.dsl.util.data.payment.PaymentData;

import java.util.function.Function;

import static cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage.CURRENT_ACCOUNT_NUMBER;
import static cz.moneta.test.dsl.ufo.banka.pages.products.current.account.ZPL_OBR001_SmoothPaymentInfo.SMOOTH_PAYMENT_ID_KEY;
import static cz.moneta.test.dsl.ufo.banka.pages.products.current.account.ZPL006_S001_ForeignPaymentInputPage.IDENTIFICATION_BANK_CODE_KEY;

public class PaymentTasks {
    private final Harness harness;

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

    public Function<HLO001_S001_ClientSearchPage, HLO001_S001_ClientSearchPageTransition> searchAccountByProduct(String accountNumber) {
        return start -> start.closeSearchedHistoryIfVisible()
                .fillSearchTerm(accountNumber)
                .clickSearchByProduct();
    }

    public Function<HLO001_S001_ClientSearchPageTransition, HPO001_S001_CurrentAccountDetailsPage> storeBalancesFromUfoBanka() {
        return start -> start.onCurrentAccountDetailsPage()
                .storeDispoBalance()
                .storeAccBalance();
    }

    public Function<HLO001_S001_ClientSearchPageTransition, PSP004_S001_PrintPage> createStandingOrder(PaymentData data) {
        return start -> start.onCurrentAccountDetailsPage()
                .openCreateStandingOrderPage()
                .fillFirstPaymentDate(data.getFirstPaymentDate())
                .fillLastPaymentDate(data.getLastPaymentDate())
                .fillReceiverAccountNumber(data.getCreditorAcc())
                .fillReceiverAccountBankCode(data.getBankCode())
                .fillReceiverAccountName(data.getName1())
                .fillFirstAmount(data.getAmount())
                .fillOptionalData(data.getVariableSymbol(), data.getConstantSymbol(),
                        data.getSpecificSymbol(), data.getName2(), data.getMessageMax2())
                .clickSave()
                .onPrintPage();
    }

    public Function<HLO001_S001_ClientSearchPageTransition, TRP001_S002_ListOfStandingOrdersPage> chooseStandingOrderFromList(String rootId) {
        return start -> start.onCurrentAccountDetailsPage()
                .openListOfStandingOrdersPage()
                .checkStandingOrderStatusValid(rootId)
                .clickOnStandingOrder(rootId);
    }

    public Function<TRP001_S002_ListOfStandingOrdersPage, TRP001_S009_ChangeStandingOrderPageTransitions> changeStandingOrderFromList(String amount) {
        return start -> start.clickChange()
                .fillAmount(amount)
                .clickSave();
    }

    public Function<PSP004_S001_PrintPage, OBR001_S001_PrintInstructionPageTransitions> clickSignedAndStoreRootId() {
        return start -> start.closePrintWindow()
                .onPrintInstructionPage()
                .clickRightButton()
                .onPrintInstructionPage()
                .storeRootId()
                .clickCenterButton();
    }

    public Function<TRP001_S009_ChangeStandingOrderPageTransitions, TRP001_S002_ListOfStandingOrdersPage> clickSignedAndStoreEditedRootId() {
        return start -> start.onPrintPage()
                .closePrintWindow()
                .onPrintInstructionPage()
                .clickRightButton()
                .onPrintInstructionPage()
                .storeRootId()
                .clickCenterButton()
                .onClientChargedPage()
                .clickOkToListTp();
    }

    public Function<TRP001_S002_ListOfStandingOrdersPage, TRP001_S002_ListOfStandingOrdersPage> deleteStandingOrderAndCheckStatusDeleted(String rootId) {
        return start -> start.clickDelete()
                .clickOnPrintDelete()
                .onPrintPage()
                .closePrintWindow()
                .onPrintInstructionPage()
                .clickRightButton()
                .onListOfStandingOrdersPage()
                .checkStandingOrderId(rootId)
                .checkStandingOrderStatusDeleted(rootId);
    }

    public Function<HLO001_S001_ClientSearchPageTransition, PSP004_S001_PrintPage> createAuthorizationForDirectDebit(PaymentData data) {
        return start -> start.onCurrentAccountDetailsPage()
                .openListOfDirectDebitsPage()
                .clickCreateButton()
                .fillReceiverAccountNumber(data.getCreditorAcc())
                .fillReceiverAccountBankCode(data.getBankCode())
                .fillAmount(data.getAmount())
                .fillReceiverAccountName(data.getName1())
                .selectPeriod(data.getPeriod())
                .clickSaveToPrint();
    }

    public Function<HLO001_S001_ClientSearchPageTransition, INK001_S002_ListOfAuthorizationForDirectDebitsPage> chooseAuthorizationForDirectDebitList(String rootId) {
        return start -> start.onCurrentAccountDetailsPage()
                .openListOfDirectDebitsPage()
                .checkAuthorizationForDirectDebitValid(rootId)
                .clickOnAuthorizationForDirectDebit(rootId);
    }

    public Function<INK001_S002_ListOfAuthorizationForDirectDebitsPage, PSP004_S001_PrintPage> changeAuthorizationForDirectDebitFromList(String amount) {
        return start -> start.clickChange()
                .fillAmount(amount)
                .clickSave();
    }


    public Function<INK001_S002_ListOfAuthorizationForDirectDebitsPage, INK001_S002_ListOfAuthorizationForDirectDebitsPage> deleteAuthorizationForDirectDebitAndCheckStatus(String rootId) {
        return start -> start.clickDelete()
                .clickOnPrintDelete()
                .closePrintWindow()
                .onPrintInstructionPage()
                .clickRightButton()
                .onPrintInstructionPage()
                .clickCenterButton()
                .onListOfDirectDebitsPage()
                .checkAuthorizationForDirectDebitDeletd(rootId);
    }

    public Function<INK001_S002_ListOfAuthorizationForDirectDebitsPage, INK001_S002_ListOfAuthorizationForDirectDebitsPage> checkDirectDebitChange(String changeTitle, String changeAmount, String rootId) {
        return start -> start
                .clickChange()
                .fillTitle(changeTitle)
                .fillAmount(changeAmount)
                .clickSave()
                .closePrintWindow()
                .onPrintInstructionPage()
                .clickRightButton()
                .onPrintInstructionPage()
                .clickCenterButton()
                .onClientChargedPage()
                .clickOkToListOfAuthorizationForDirectDebitsPage()
                .checkDirectDebitTitle(rootId, changeTitle)
                .checkDirectDebitAmount(rootId, changeAmount);
    }

    public Function<HLO001_S001_ClientSearchPage, PKU001_S002_CreatePaymentOrderPage> createStandardPaymentMaxData(PaymentData data) {
        return start -> start.closeSearchedHistoryIfVisible()
                .clickOnCreatePaymentOrder()
                .fillDebtorAccNumber(data.getDebtorAcc())
                .fillAmountInput(data.getAmount())
                .fillOptionalData(data.getVariableSymbol(), data.getConstantSymbol(),
                        data.getVariableSymbol(), data.getMessageMax2())
                .clickTodayButton()
                .fillCreditorAccNumber(data.getCreditorAcc())
                .fillCreditorAccBankCode(data.getBankCode());
    }

    public Function<HLO001_S001_ClientSearchPage, PKU001_S002_CreatePaymentOrderPage> createStandardPayment(PaymentData data) {
        return start -> start.closeSearchedHistoryIfVisible()
                .clickOnCreatePaymentOrder()
                .fillDebtorAccNumber(data.getDebtorAcc())
                .fillAmountInput(data.getAmount())
                .clickTodayButton()
                .fillCreditorAccNumber(data.getCreditorAcc())
                .fillCreditorAccBankCode(data.getBankCode());
    }

    public Function<HLO001_S001_ClientSearchPage, PKU001_S002_CreatePaymentOrderPage> createPriorityPaymentMaxData(PaymentData data) {
        return start -> start.closeSearchedHistoryIfVisible()
                .clickOnCreatePaymentOrder()
                .fillDebtorAccNumber(data.getDebtorAcc())
                .fillAmountInput(data.getAmount())
                .fillOptionalData(data.getVariableSymbol(), data.getConstantSymbol(),
                        data.getVariableSymbol(), data.getMessageMax2())
                .clickTodayButton()
                .fillCreditorAccNumber(data.getCreditorAcc())
                .fillCreditorAccBankCode(data.getBankCode())
                .clickPriorityPaymentCheckbox();
    }

    public Function<HLO001_S001_ClientSearchPage, OBR001_S001_PrintInstructionPage> createBankBookPaymentMaxData(PaymentData data) {
        return start -> start.closeSearchedHistoryIfVisible()
                .clickOnBillingOnBankBook()
                .selectCreditorAcc(data.getCreditorAcc())
                .fillDebtorAcc(data.getDebtorAcc())
                .fillAmount(data.getAmount())
                .fillMaxData(data.getMessageMax2(), data.getVariableSymbol(), data.getSpecificSymbol(), data.getConstantSymbol())
                .clickOnSave()
                .storeRootId();
    }

    public Function<HLO001_S001_ClientSearchPage, OBR001_S001_PrintInstructionPage> createBankBookPayment(PaymentData data) {
        return start -> start.closeSearchedHistoryIfVisible()
                .clickOnBillingOnBankBook()
                .selectCreditorAcc(data.getCreditorAcc())
                .fillDebtorAcc(data.getDebtorAcc())
                .fillAmount(data.getAmount())
                .clickOnSave()
                .storeRootId();
    }

    public Function<PKU001_S002_CreatePaymentOrderPage, PKU001_S002_CreatePaymentOrderPage> savePaymentOrder() {
        return start -> start.clickSaveButton()
                .closePrintWindow()
                .onPrintInstructionPage()
                .clickLeftButton()
                .onPrintInstructionPage()
                .storeRootId()
                .clickCenterButton()
                .onCreatePaymentOrderPage();
    }

    public Function<ZPL006_S002_ListOfForeignPaymentsPage, ZPL006_S002_ListOfForeignPaymentsPage>
    createNewSmoothPayment(String currency, String accountNumberReceiver, String receiverName, String receiverAddressStreet, String receiverAddressCity, String receiverAddressCountryCode) {
        return start -> start
                .clickNewPaymentAndDismissAlertIfPresent()
                .selectCurrency(currency)
                .clickNo()
                .fillAmount("101")
                .fillAccountAndWaitUntilLoaded(accountNumberReceiver)
                .fillReceiverName(receiverName)
                .fillReceiverAdressStreet(receiverAddressStreet)
                .fillReceiverAdressCity(receiverAddressCity)
                .fillReceiverAdressCountryCode(receiverAddressCountryCode)
                .storeIdentificationBankCode()
                .saveCreatedForeignPaymentAndDismissAllAlertsIfPresent()
                .saveDocumentUrl()
                .closePrintWindow()
                .onSignerPage()
                .clickLeftButton()
                .onSmoothPaymentInfo()
                .storeSmoothPaymentId()
                .clickOkButton()
                .checkPresentCreatedTransaction(harness.get(SMOOTH_PAYMENT_ID_KEY));
    }

    public Function<ZPL006_S002_ListOfForeignPaymentsPage, ZPL006_S002_ListOfForeignPaymentsPage> editSmoothPaymentAmount(String paymentId, String finalAmount) {
        return start -> start
                .clickPayment(paymentId)
                .clickEditAndAcceptAlert()
                .fillAmount(finalAmount)
                .clickSave()
                .storeSmoothPaymentId()
                .clickOkButton()
                .checkPaymentAmount(harness.get(SMOOTH_PAYMENT_ID_KEY), finalAmount);
    }

    public Function<ZPL002_S003_ListOfSmoothPaymentsForVerificationPage, ZPL_OBR001_SmoothPaymentInfo>
    verifySmoothPayment(String paymentId, String currency, String finalAmount, String accountNumberReceiver, String receiverAddressCountryCode) {
        return start -> start
                .clickPayment(paymentId)
                .clickVerification()
                .selectCurrency(currency)
                .fillAmount(finalAmount)
                .fillAccountNumberSender(harness.get(CURRENT_ACCOUNT_NUMBER))
                .fillAccountNumberReceiver(accountNumberReceiver)
                .fillCountryCode(receiverAddressCountryCode)
                .fillIdentificationBankCode(harness.get(IDENTIFICATION_BANK_CODE_KEY))
                .clickExpensesSha()
                .clickVerification()
                .checkPageLabelContent("Platba byla zverifikována");
    }

    public Function<ZPL006_S002_ListOfForeignPaymentsPage, ZPL_OBR001_SmoothPaymentInfo> cancelSmoothPayment(String paymentId) {
        return start -> start
                .clickPayment(paymentId)
                .clickCancelPayment()
                .clickCancel()
                .checkInfoLabelContent("Hladká platba byla zrušena.");
    }

}
