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.CurrencyPAE;
import cz.moneta.test.dsl.payment_engine.enums.TransactionReasonPAE;
import cz.moneta.test.dsl.payment_engine.parts_of_menu.menu_search.PaymentEngineSearchPage;
import cz.moneta.test.dsl.payment_engine.tasks.PaymentEngineTasks;
import cz.moneta.test.dsl.payment_engine.utils.PaymentEngineUtils;
import cz.moneta.test.dsl.util.DateUtils;
import cz.moneta.test.dsl.util.credentials.Keys;
import cz.moneta.test.dsl.util.data.payment.PaymentData;
import cz.moneta.test.dsl.util.task.wso2.payments.PaymentsTasks;
import cz.moneta.test.harness.annotations.Environment;
import cz.moneta.test.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenarioWithOrder;
import cz.moneta.test.harness.support.auth.Credentials;
import cz.moneta.test.harness.support.auth.Key;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Order;

import java.util.concurrent.ThreadLocalRandom;

import static cz.moneta.test.dsl.payment_engine.enums.CurrencyPAE.CURRENCY_EUR_SHORT;
import static cz.moneta.test.dsl.payment_engine.enums.TransactionReasonPAE.ARCHIV;

@TestScenarioWithOrder(name = "Create foreign IFCY payment")
public class ForeignIFCY {

    private String TOKEN_CBL;
    private PaymentData data;
    private PaymentEngineTasks tasks;
    private String transactionId;

    @BeforeAll
    void setUpTestData(Harness harness) {
        data = PaymentData.builder()
                .typeOfMessage("11")
                .todayDate(DateUtils.convertRawDateForWso2(DateUtils.getTodayRawDate()))
                .exchangeRate("24.5289")
                .payScheme("IFCY")
                .debtorAcc(harness.getConfig("payments_cashless.domesticPayment.accountCZK"))
                .creditorAcc(harness.getConfig("payments_cashless.domesticPayment.accountEUR"))
                .build();
        TOKEN_CBL = harness.tasks().wso2().authorization().getToken("DigitalchannelsSB_CBL");
    }

    @BeforeEach
    void setUpUniqueTestData(Harness harness) {
        data.setAmount(String.valueOf(ThreadLocalRandom.current().nextInt(1, 199)));
        tasks = new PaymentEngineTasks(harness);
    }

    private void createIFCYTransaction(Harness harness, CurrencyPAE currencyPAE) {
        String request = harness.tasks().wso2().payments().createRequestForIFCYTransactionCreating(data, currencyPAE);
        harness.tasks()
                .wso2()
                .payments()
                .createIFCYTransaction(TOKEN_CBL, request);
        transactionId = harness.get(PaymentsTasks.TRANSACTION_ID_KEY);
    }

    private void checkTransactionStatusBooked(Harness harness, String account, String amountICBS, String transactionId) {
        String request = harness.tasks()
                .wso2()
                .payments()
                .createRequestForTransactionCheck(account, data.getTodayDate(), amountICBS);
        harness.tasks()
                .wso2()
                .payments()
                .checkTransactionStatusBooked(TOKEN_CBL, request, transactionId);
    }

    private void checkTransactionStatusPaeAndStoreEquivalentAmount(Harness harness, Credentials user2,
                                                                   TransactionReasonPAE status, String transactionRow) {
        tasks.loginTasks().logInPae(harness, user2)
                .then(tasks.searchTransactionAndWaitForStatus(status, transactionId))
                .clickLifeCycle()
                .refreshLifeCycle()
                .wait3sec()
                .storeEquivalentAmountCzk(transactionRow);
        harness.withPaymentEngine().closePaymentEngine();
        data.setEquivalentAmount(harness.get(PaymentEngineSearchPage.EQUIVALENT_AMOUNT_KEY)
                .toString().replace(".", "").replace(",", "."));
    }

    @Order(1)
    @JiraTestCase(id = "PAY-T3609")
    @TestCase(name = "Create foreign IFCY payment with FEXR exchange rate type", environments = {Environment.TST1, Environment.PPE})
    void createForeignIfcyPaymentWithFexrRateType(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user2) {
        data.setRateType("FEXR");
        data.setIcbsProfile("COUFX");

        createIFCYTransaction(harness, CURRENCY_EUR_SHORT);
        checkTransactionStatusPaeAndStoreEquivalentAmount(harness, user2, ARCHIV, "2");

        checkTransactionStatusBooked(harness, data.getDebtorAcc(), data.getEquivalentAmount(), transactionId);
        checkTransactionStatusBooked(harness, data.getCreditorAcc(), data.getAmount(), transactionId);

        PaymentEngineUtils.logResultWithEquivalentAmount(harness, transactionId, data.getDebtorAcc(),
                data.getCreditorAcc(), data.getAmount() + " EUR", data.getEquivalentAmount() + " CZK");
    }

    @Order(2)
    @JiraTestCase(id = "PAY-T3610")
    @TestCase(name = "Create foreign IFCY payment with BANK exchange rate type", environments = {Environment.TST1, Environment.PPE})
    void createForeignIfcyPaymentWithBankRateType(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user2) {
        data.setRateType("BANK");
        data.setIcbsProfile("COU15");

        createIFCYTransaction(harness, CURRENCY_EUR_SHORT);
        checkTransactionStatusPaeAndStoreEquivalentAmount(harness, user2, ARCHIV, "2");

        checkTransactionStatusBooked(harness, data.getDebtorAcc(), data.getEquivalentAmount(), transactionId);
        checkTransactionStatusBooked(harness, data.getCreditorAcc(), data.getAmount(), transactionId);

        PaymentEngineUtils.logResultWithEquivalentAmount(harness, transactionId, data.getDebtorAcc(),
                data.getCreditorAcc(), data.getAmount() + " EUR", data.getEquivalentAmount() + " CZK");
    }
}
