package cz.moneta.test.regression.transactional.payments.cashless.payment_engine.foreign.outgoing;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.payment_engine.enums.ForeignChannelIdentities;
import cz.moneta.test.dsl.payment_engine.home.PaymentEngineHomePage;
import cz.moneta.test.dsl.payment_engine.model.FileForInputPreparation;
import cz.moneta.test.dsl.payment_engine.parts_of_menu.menu_search.PaymentEngineSearchPage;
import cz.moneta.test.dsl.util.data.payment.PaymentData;
import cz.moneta.test.harness.endpoints.payment_engine.PaymentEngineEndpoint;
import cz.moneta.test.harness.support.auth.Credentials;
import cz.moneta.test.dsl.payment_engine.enums.TransactionReasonPAE;
import org.apache.commons.lang.StringUtils;

import java.io.*;

import static cz.moneta.test.dsl.payment_engine.enums.TransactionReasonPAE.DISTRIBUTION;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

public abstract class AbstractOutgoingXctClientPaymentTest extends FileForInputPreparation {

    protected static String transactionId;
    protected static File file;
    private static String CREDITOR_ACCOUNT;
    private static String CREDITOR_IBAN;
    private static String CREDITOR_BIC;
    private static String CREDITOR_NAME;
    private static String CREDITOR_BANK_NAME;
    private static String MESSAGE;
    private static String CURRENCY;
    private static final String MT101_PATH = "regression/transactional/cashless/MT101.out";
    private static final String MT101 = "0105X.out";

    abstract void verifyForeignPaymentXct(Harness harness, Credentials credentials);

    abstract void createForeignPaymentXct(Harness harness, Credentials credentials);

    abstract void outgoingXctAutomaticTracing(Harness harness, Credentials user1, Credentials user2);

    abstract void outgoingXctManualTracing(Harness harness, Credentials user1, Credentials user2);

    abstract void outgoingXctBasedOnIncomingMT101(Harness harness, Credentials credentials) throws IOException;

    protected static PaymentEngineHomePage logInPae(Harness harness, Credentials credentials) {
        return harness.withPaymentEngine()
                .openLoginPage()
                .login(credentials.getUsername(), credentials.getPassword())
                .clickOkButton();
    }

    protected static void verifyForeignPaymentAndClosePAE(Harness harness, Credentials credentials) {
        verifyTransactionAndWaitForStatus(harness, credentials, DISTRIBUTION);
        harness.closeEndpoint(harness.getEndpoint(PaymentEngineEndpoint.class));
        harness.log("Transaction " + transactionId + " was successfully verified");
    }

    protected static PaymentEngineSearchPage verifyTransactionAndWaitForStatus(Harness harness, Credentials credentials,
                                                                               TransactionReasonPAE status) {
        return logInPae(harness, credentials)
                .clickTransactionAndChooseForVerificationFromCreateManualMenu()
                .pressSearch()
                .fillId(transactionId)
                .searchUnverified()
                .selectTransaction(transactionId)
                .clickVerifyTransaction()
                .clickSubmit()
                .clickSubmitIfStillVisible()
                .search()
                .typeId(transactionId)
                .clickSearch()
                .waitForStatus(harness, status);
    }

    protected void createForeignPayment(Harness harness, Credentials credentials, PaymentData data) {
        logInPae(harness, credentials)
                .clickTransactionAndChooseCreateXctClientPaymentFromCreateManualMenu()
                .clickOnChannelIdentity()
                .selectChannelIdentity(ForeignChannelIdentities.PAE)
                .fillManualData(data.getAmount(),
                        CREDITOR_ACCOUNT,
                        CREDITOR_IBAN,
                        MESSAGE,
                        CURRENCY,
                        CREDITOR_NAME,
                        CREDITOR_BIC,
                        CREDITOR_BANK_NAME)
                .clickToExecutionDateCalendar()
                .chooseTodayExecution()
                .selectDebtorByIban(data.getDebtorIban())
                .clickSubmit()
                .storeTransactionId()
                .clickNo();

        transactionId = StringUtils.substringBetween(harness.get("TRANSACTION_ID_KEY"), "ID ", ".");
    }


    protected static void setTransactionData(String creditorAccount, String creditorIban, String message, String currency,
                                             String creditorName, String creditorBic, String creditorBankName) {
        CREDITOR_ACCOUNT = creditorAccount;
        CREDITOR_IBAN = creditorIban;
        MESSAGE = message;
        CURRENCY = currency;
        CREDITOR_NAME = creditorName;
        CREDITOR_BIC = creditorBic;
        CREDITOR_BANK_NAME = creditorBankName;
    }

    protected void assumption_BicExistence(Harness harness, Credentials credentials, String bic, boolean wantExistBic) {

        try {
            logInPae(harness, credentials)
                    .clickAdministrationAndChooseXCTRoutingTableFromTransactionsRouting()
                    .choseBicAndApply(bic)
                    .checkBicExistence();
        } catch (Exception e) {
            harness.closeEndpoint(harness.getEndpoint(PaymentEngineEndpoint.class));
            assumeTrue(!wantExistBic);
            return;
        }
        harness.closeEndpoint(harness.getEndpoint(PaymentEngineEndpoint.class));
        assumeTrue(wantExistBic);
    }

    protected void createMT101TemFile(Harness harness, PaymentData data) throws IOException {
        String mt101FileName = MT101.replace("X", StringUtils.leftPad(String.valueOf(lastField + Integer.valueOf(data.getRandom())), 4, "0"));
        data.setIban(CREDITOR_IBAN);
        file = prepareFileForInput(harness, MT101_PATH, mt101FileName, data);
    }
}
