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.DateUtils;
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.time.LocalDate;
import java.time.format.DateTimeFormatter;
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 domestic standing order - UFO Banka")
public class StandingOrderUfoBankaTest {
    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"))
                .firstPaymentDate(DateUtils.getNextWorkday().format(DateTimeFormatter.ofPattern("dd.MM.yyyy")))
                .lastPaymentDate(LocalDate.now().plusYears(1).format(DateTimeFormatter.ofPattern("dd.MM.yyyy")))
                .bankCode("0600")
                .build();
    }

    @Order(1)
    @JiraTestCase(id = "PAY-T3540")
    @TestCase(name = "Create/edit/delete standing order CZ in UFO and check in PAE", environments = {Environment.TST1, Environment.PPE})
    void createEditDeleteStandingOrderInUfoBankaAndCheckInPAE(Harness harness, @Key(Keys.ufo.banka.banker) Credentials ufoCredentials,
                                                              @Key(Keys.paymentEngine.firstUser) Credentials paeCredentials1) {
        BankaTasks bankaTasks = harness.tasks().ufo().banka();
        PaymentEngineTasks paymentEngineTasks = harness.tasks().paymentEngineTasks();

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

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

        harness.store("reports.tmfj.execution.comment", "Standing Order " + 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().chooseStandingOrderFromList(rootId1))
                .then(bankaTasks.payment().changeStandingOrderFromList(data.getEquivalentAmount()))
                .then(bankaTasks.payment().clickSignedAndStoreEditedRootId());
        harness.withUfo().closeApplication();

        String rootId2 = harness.get(ROOT_ID_KEY);
        paymentEngineTasks.loginTasks().logInPae(harness, paeCredentials1)
                .then(paymentEngineTasks.checkStandingOrderStatus(statusArchiv, rootId1))
                .closeTab()
                .then(paymentEngineTasks.checkStandingOrderStatus(statusValid, rootId2));
        harness.withPaymentEngine().closePaymentEngine();

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

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

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

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

        harness.log("Standing Order " + rootId1 + " was successfully created, edited and deleted in UFO and checked in PAE.");
        harness.store("reports.tmfj.execution.comment", "Standing Order " + rootId1 + " was successfully created in UFO and checked in PAE."
                + "\n" +
                "Standing Order " + rootId2 + " was successfully edited in UFO and checked in PAE."
                + "\n" +
                "Standing Order " + 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");
    }
}