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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.payment_engine.tasks.PaymentEngineTasks;
import cz.moneta.test.dsl.ufo.banka.tasks.BankaTasks;
import cz.moneta.test.dsl.util.credentials.Keys;
import cz.moneta.test.dsl.util.data.payment.PaymentData;
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.Order;


import java.util.concurrent.ThreadLocalRandom;

import static cz.moneta.test.dsl.ufo.banka.pages.common.pages.OBR001_S001_PrintInstructionPage.ROOT_ID_KEY;

@TestScenarioWithOrder(name = "Create/edit/delete Authorization For Direct Debit - UFO Banka")
public class AuthorizationForDirectDebitUfoBankaTest {
    private PaymentData data;
    private static final String statusValid = "Platný";
    private static final String statusArchiv = "Archivovaný";

    @BeforeAll
    void setUpTestData(Harness harness) {
        data = PaymentData.builder()
                .amount(String.valueOf(ThreadLocalRandom.current().nextInt(1, 9)))
                .equivalentAmount(String.valueOf(ThreadLocalRandom.current().nextInt(1, 9)))
                .debtorAcc(harness.getConfig("payments_cashless.domesticPayment.debtorAccUFO"))
                .creditorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAccInh"))
                .period("Měsíční")
                .bankCode("0600")
                .build();
    }

    @Order(1)
    @JiraTestCase(id = "PAY-T3541")
    @TestCase(name = "Create/edit/delete Authorization For Direct Debit in UFO and check in PAE", environments = {Environment.TST1, Environment.PPE})
    void createEditDeleteAuthorizationForDirectDebitInUfoBankaAndCheckInPAE(Harness harness, @Key(Keys.ufo.banka.banker) Credentials ufoCredentials,
                                                              @Key(Keys.paymentEngine.firstUser) Credentials paeCredentials1,
                                                              @Key(Keys.paymentEngine.secondUser) Credentials paeCredentials2) {
        BankaTasks bankaTasks = harness.tasks().ufo().banka();
        PaymentEngineTasks paymentEngineTasks = harness.tasks().paymentEngineTasks();
        paymentEngineTasks.assumptionAuthorizationForDirectDebitExistence(data, paeCredentials1, paeCredentials2);

        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentials(ufoCredentials))
                .then(bankaTasks.payment().searchAccountByProduct(data.getDebtorAcc()))
                .then(bankaTasks.payment().createAuthorizationForDirectDebit(data))
                .then(bankaTasks.payment().clickSignedAndStoreRootId());
        harness.withUfo().closeApplication();

        String rootId1 = harness.get(ROOT_ID_KEY);
        paymentEngineTasks.loginTasks().logInPae(harness, paeCredentials1)
                .then(paymentEngineTasks.checkAuthorizationForDirectDebitStatus(statusValid, rootId1));
        harness.withPaymentEngine().closePaymentEngine();

        harness.store("reports.tmfj.execution.comment", "Authorization For Direct Debit " + rootId1 + " was successfully created in UFO and checked in PAE.");

        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentials(ufoCredentials))
                .then(bankaTasks.payment().searchAccountByProduct(data.getDebtorAcc()))
                .then(bankaTasks.payment().chooseAuthorizationForDirectDebitList(rootId1))
                .then(bankaTasks.payment().changeAuthorizationForDirectDebitFromList(data.getEquivalentAmount()))
                .then(bankaTasks.payment().clickSignedAndStoreRootId());
        harness.withUfo().closeApplication();

        String rootId2 = String.valueOf(Integer.valueOf(rootId1) + 1);
        String rootId3 = String.valueOf(Integer.valueOf(rootId2) + 1);

        paymentEngineTasks.loginTasks().logInPae(harness, paeCredentials1)
                .then(paymentEngineTasks.checkAuthorizationForDirectDebitStatus(statusArchiv, rootId1))
                .closeTab()
                .then(paymentEngineTasks.checkAuthorizationForDirectDebitStatus(statusValid, rootId2));
        harness.withPaymentEngine().closePaymentEngine();

        harness.store("reports.tmfj.execution.comment", "Authorization For Direct Debit " + rootId1 + " was successfully edited in UFO and checked in PAE."
                + "\n" + "Authorization For Direct Debit " + rootId2 + " was successfully archived in UFO and checked in PAE.");

        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentials(ufoCredentials))
                .then(bankaTasks.payment().searchAccountByProduct(data.getDebtorAcc()))
                .then(bankaTasks.payment().chooseAuthorizationForDirectDebitList(rootId2))
                .then(bankaTasks.payment().deleteAuthorizationForDirectDebitAndCheckStatus(rootId3));
        harness.withUfo().closeApplication();

        paymentEngineTasks.loginTasks().logInPae(harness, paeCredentials1)
                .then(paymentEngineTasks.checkAuthorizationForDirectDebitStatus(statusArchiv, rootId1))
                .closeTab()
                .then(paymentEngineTasks.checkAuthorizationForDirectDebitStatus(statusArchiv, rootId2))
                .closeTab()
                .then(paymentEngineTasks.checkAuthorizationForDirectDebitStatus(statusArchiv, rootId3));
        harness.withPaymentEngine().closePaymentEngine();

        harness.log("Authorization For Direct Debit " + rootId1 + " was successfully created, edited and deleted in UFO and checked in PAE.");
        harness.store("reports.tmfj.execution.comment", "Authorization For Direct Debit " + rootId1 + " was successfully created in UFO and checked in PAE."
                + "\n" +
                "Authorization For Direct Debit " + rootId2 + " was successfully edited in UFO and checked in PAE."
                + "\n" +
                "Authorization For Direct Debit " + rootId3 + " was successfully deleted in UFO and checked in PAE."
                + "\n" +
                "Debtor account:  " + data.getDebtorAcc()
                + "\n" +
                "Creditor account:  " + data.getCreditorAcc()
                + "\n" +
                "Amount:  " + data.getAmount() + " CZK"
                + "\n" +
                "Changed amount:  " + data.getAmount() + data.getEquivalentAmount() + " CZK");
    }
}
