package cz.moneta.test.regression.transactional.payments.cashless.ufo.banka.analytical;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.payment_engine.enums.TransactionReasonPAE;
import cz.moneta.test.dsl.payment_engine.tasks.PaymentEngineTasks;
import cz.moneta.test.dsl.payment_engine.utils.PaymentEngineUtils;
import cz.moneta.test.dsl.ufo.banka.tasks.BankaTasks;
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.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
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.TransactionReasonPAE.*;
import static cz.moneta.test.dsl.ufo.banka.pages.common.pages.OBR001_S001_PrintInstructionPage.ROOT_ID_KEY;
import static cz.moneta.test.harness.annotations.Environment.PPE;
import static cz.moneta.test.harness.annotations.Environment.TST1;

@TestScenario(name = "Create Standard Payment UFO Banka")
public class StandardPaymentUfoBanka {
    private BankaTasks bankaTasks;
    private PaymentEngineTasks paymentEngineTasks;
    private PaymentData data;
    private String TOKEN_CBL;
    private String transactionId;

    @BeforeAll
    void setUpTestData(Harness harness) {
        data = PaymentData.builder()
                .todayDate(DateUtils.convertRawDateForWso2(DateUtils.getTodayRawDate()))
                .debtorAcc(harness.getConfig("payments_cashless.domesticPayment.debtorAccUFO"))
                .bankName("MONETA Money Bank, a.s.")
                .payScheme("CZ CT Non-priority")
                .nextDayDist("Ne")
                .build();
        TOKEN_CBL = harness.tasks().wso2().authorization().getToken("DigitalchannelsSB_CBL");
    }

    @BeforeEach
    void setUpUniqueData(Harness harness) {
        data.setAmount(String.valueOf(ThreadLocalRandom.current().nextInt(1, 299)));
        bankaTasks = harness.tasks().ufo().banka();
        paymentEngineTasks = harness.tasks().paymentEngineTasks();
    }

    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 createStandardPaymentMaxData(Harness harness, Credentials ufoCredentials) {
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentials(ufoCredentials))
                .then(bankaTasks.payment().createStandardPaymentMaxData(data))
                .then(bankaTasks.payment().savePaymentOrder());
        harness.withUfo().closeApplication();
        transactionId = harness.get(ROOT_ID_KEY);
    }

    private void createStandardPayment(Harness harness, Credentials ufoCredentials) {
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentials(ufoCredentials))
                .then(bankaTasks.payment().createStandardPayment(data))
                .then(bankaTasks.payment().savePaymentOrder());
        harness.withUfo().closeApplication();
        transactionId = harness.get(ROOT_ID_KEY);
    }

    private void searchTransactionAndWaitForStatus(Harness harness, Credentials paeCredentials, TransactionReasonPAE status) {
        paymentEngineTasks.loginTasks().logInPae(harness, paeCredentials)
                .then(paymentEngineTasks.searchTransactionAndWaitForStatus(status, transactionId));
        harness.withPaymentEngine().closePaymentEngine();
    }

    @Order(1)
    @JiraTestCase(id = "PAY-T3544")
    @TestCase(name = "Create Outgoing Standard Payment in UFO Banka and check in PAE and ICBS - Max Data", environments = {TST1, PPE})
    void createOutgoingStandardPaymentInUfoAndCheckInPaeAndIcbsMaxData(Harness harness, @Key(Keys.ufo.banka.banker) Credentials ufoCredentials,
                                                                       @Key(Keys.paymentEngine.firstUser) Credentials paeCredentials) {
        data.setCreditorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAcc"));
        data.setBankCode(harness.getConfig("payments_cashless.domesticPayment.bankCodeCr"));

        createStandardPaymentMaxData(harness, ufoCredentials);

        paymentEngineTasks.loginTasks().logInPae(harness, paeCredentials)
                .then(paymentEngineTasks.searchTransactionAndWaitForStatus(RELEASE, transactionId))
                .closeTab()
                .then(paymentEngineTasks.runReleaseAfterDispoCheck(DISTRIBUTION, transactionId))
                .closeTab()
                .then(paymentEngineTasks.sendAndRunCZCTDDistribution(data, ARCHIV, transactionId));
        harness.withPaymentEngine().closePaymentEngine();

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

        PaymentEngineUtils.logResultUfoWithPaeAndICBS(harness, transactionId, data);
    }

    @Order(2)
    @JiraTestCase(id = "PAY-T3545")
    @TestCase(name = "Create Inhouse Standard Payment in UFO Banka and check in PAE and ICBS - Max Data", environments = {TST1, PPE})
    void createInhouseStandardPaymentInUfoAndCheckInPaeAndIcbsMaxData(Harness harness, @Key(Keys.ufo.banka.banker) Credentials ufoCredentials,
                                                                      @Key(Keys.paymentEngine.firstUser) Credentials paeCredentials) {
        data.setCreditorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAccInh"));
        data.setBankCode("0600");

        createStandardPaymentMaxData(harness, ufoCredentials);
        searchTransactionAndWaitForStatus(harness, paeCredentials, ARCHIV);

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

        PaymentEngineUtils.logResultUfoWithPaeAndICBS(harness, transactionId, data);
    }

    @Order(3)
    @JiraTestCase(id = "PAY-T3546")
    @TestCase(name = "Create Inhouse Standard Payment in UFO Banka and check in PAE and ICBS", environments = {TST1, PPE})
    void createInhouseStandardPaymentInUfoAndCheckInPaeAndIcbs(Harness harness, @Key(Keys.ufo.banka.banker) Credentials ufoCredentials,
                                                               @Key(Keys.paymentEngine.firstUser) Credentials paeCredentials) {
        data.setCreditorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAccInh"));
        data.setBankCode("0600");

        createStandardPayment(harness, ufoCredentials);
        searchTransactionAndWaitForStatus(harness, paeCredentials, ARCHIV);

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

        PaymentEngineUtils.logResultUfoWithPaeAndICBS(harness, transactionId, data);
    }
}
