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.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.TransactionReasonPAE.ARCHIV;
import static cz.moneta.test.dsl.payment_engine.enums.TransactionReasonPAE.RELEASE;
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;

@TestScenarioWithOrder(name = "Create  Bank Book Payment UFO Banka")
public class BankBookPaymentUfoBanka {
    private PaymentData data;
    private String TOKEN_CBL;
    private BankaTasks bankaTasks;
    private PaymentEngineTasks paymentEngineTasks;
    private String transactionId;

    @BeforeAll
    void setUpTestData(Harness harness) {
        data = PaymentData.builder()
                .todayDate(DateUtils.convertRawDateForWso2(DateUtils.getTodayRawDate()))
                .debtorAcc(harness.getConfig("payments_cashless.domesticPayment.debtorAccUFO"))
                .creditorAcc("2298050")
                .bankCode("0600")
                .postingValue("Y")
                .initiatorValue("B")
                .icbsProfile("COU15")
                .build();
        TOKEN_CBL = harness.tasks().wso2().authorization().getToken("DigitalchannelsSB_CBL");
    }

    @BeforeEach
    void setUpUniqueTestData(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 releaseManualOutgoingAndWaitForStatus(Harness harness, Credentials paeCredentials1, TransactionReasonPAE status) {
        paymentEngineTasks.loginTasks().logInPae(harness, paeCredentials1)
                .then(paymentEngineTasks.releaseManualOutgoing(transactionId))
                .closeTab()
                .then(paymentEngineTasks.searchTransactionAndWaitForStatus(status, transactionId));
        harness.withPaymentEngine().closePaymentEngine();
    }

    private void verifyRequestAndWaitForStatusAndData(Harness harness, Credentials paeCredentials2, TransactionReasonPAE status) {
        paymentEngineTasks.loginTasks().logInPae(harness, paeCredentials2)
                .then(paymentEngineTasks.verifyRequestAndWaitForStatus(transactionId, status))
                .then(paymentEngineTasks.checkDebitInitiatorAndIcbsProfilData(data));
        harness.withPaymentEngine().closePaymentEngine();
    }

    @Order(1)
    @JiraTestCase(id = "PAY-T3547")
    @TestCase(name = "Create Bank Book Payment in UFO Banka and check in PAE and ICBS", environments = {TST1, PPE})
    void createBankBookPaymentInUfoAndCheckInPaeAndIcbs(Harness harness, @Key(Keys.ufo.banka.banker) Credentials ufoCredentials,
                                                        @Key(Keys.paymentEngine.firstUser) Credentials paeCredentials1,
                                                        @Key(Keys.paymentEngine.secondUser) Credentials paeCredentials2) {
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentials(ufoCredentials))
                .then(bankaTasks.payment().createBankBookPayment(data));
        harness.withUfo().closeApplication();

        transactionId = harness.get(ROOT_ID_KEY);
        releaseManualOutgoingAndWaitForStatus(harness, paeCredentials1, RELEASE);
        verifyRequestAndWaitForStatusAndData(harness, paeCredentials2, ARCHIV);

        checkTransactionStatusBooked(harness, data.getDebtorAcc(), data.getAmount(), transactionId);
        data.setCreditorAcc("3704-2298050");
        PaymentEngineUtils.logResultUfoWithPaeAndICBS(harness, transactionId, data);
    }

    @Order(2)
    @JiraTestCase(id = "PAY-T3548")
    @TestCase(name = "Create Bank Book Payment in UFO Banka and check in PAE and ICBS - Max Data", environments = {TST1, PPE})
    void createBankBookPaymentInUfoAndCheckInPaeAndIcbsMaxData(Harness harness, @Key(Keys.ufo.banka.banker) Credentials ufoCredentials,
                                                               @Key(Keys.paymentEngine.firstUser) Credentials paeCredentials1,
                                                               @Key(Keys.paymentEngine.secondUser) Credentials paeCredentials2) {
        data.setCreditorAcc("2298050");

        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentials(ufoCredentials))
                .then(bankaTasks.payment().createBankBookPaymentMaxData(data));
        harness.withUfo().closeApplication();

        transactionId = harness.get(ROOT_ID_KEY);
        releaseManualOutgoingAndWaitForStatus(harness, paeCredentials1, RELEASE);
        verifyRequestAndWaitForStatusAndData(harness, paeCredentials2, ARCHIV);

        checkTransactionStatusBooked(harness, data.getDebtorAcc(), data.getAmount(), transactionId);
        data.setCreditorAcc("3704-2298050");
        PaymentEngineUtils.logResultUfoWithPaeAndICBS(harness, transactionId, data);
    }
}
