package cz.moneta.test.dsl.util.task.cagw.api;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.cagw.api.cbl.psd.aisp.my.accounts.id.transactions.Transactions;
import cz.moneta.test.dsl.cagw.api.cbl.psd.aisp.my.accounts.id.transactions.TransactionsResponse;
import cz.moneta.test.dsl.cagw.api.cbl.psd.aisp.my.accounts.id.transactions.TransactionsWithParams;
import org.apache.commons.lang.ArrayUtils;

import java.util.function.Function;

public class TransactionsTasks {

    private final Harness harness;

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

    public Function<Transactions, Transactions> setRequestValuesTransactions(String authToken, String accountId) {
        return start -> start.withAuthorizationBearer("Bearer " + authToken)
                .id(accountId);
    }

    public Function<TransactionsWithParams, TransactionsWithParams> setRequestValuesTransactionsWithAllParams(String authToken,
                                                                                                              String accountId,
                                                                                                              String fromDate,
                                                                                                              String toDate,
                                                                                                              String size,
                                                                                                              String page,
                                                                                                              String sort,
                                                                                                              String order) {
        return start -> start.withAuthorizationBearer("Bearer " + authToken)
                .id(accountId)
                .fromDate(fromDate)
                .toDate(toDate)
                .size(size)
                .page(page)
                .sort(sort)
                .order(order);
    }

    public Function<TransactionsWithParams, TransactionsWithParams> setRequestValuesTransactionsWithParams(String authToken,
                                                                                                           String accountId,
                                                                                                           String fromDate,
                                                                                                           String toDate,
                                                                                                           String size) {
        return start -> start.withAuthorizationBearer("Bearer " + authToken)
                .id(accountId)
                .fromDate(fromDate)
                .toDate(toDate)
                .size(size);
    }

    public Function<TransactionsWithParams, TransactionsResponse> sendAndWaitForTransaction(String variableSymbol) {
        return start -> start.pollUntil(t -> {
            if (!ArrayUtils.isEmpty(t.getTransactions())) {
                return t.getTransactions()[0].getEntryDetails().getTransactionDetails().getReferences().getEndToEndIdentification();
            }
            return null;
        })
                .maxRetries(120)
                .retryDelayMillis(3000)
                .isEqual(variableSymbol);
    }
}