package cz.moneta.test.regression.channels.digital.ib.newib.payment.fon;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.newib.tasks.NewIbTasks;
import cz.moneta.test.dsl.newib.util.PaymentFormData;
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 org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;

import java.util.concurrent.ThreadLocalRandom;

@TestScenarioWithOrder(name = "Bulk payment tests")
public class BulkPaymentTest {

    Credentials clientCredentials;
    PaymentFormData formData;
    int generatedPayment1;
    int generatedPayment2;
    String totalFormatted;

    private int generateRandomPayment() {
        return ThreadLocalRandom.current().nextInt(1, 9);
    }

    @BeforeAll
    public void setup(Harness harness) {
        generatedPayment1 = generateRandomPayment();
        harness.log("Generated payment 1 is " + generatedPayment1);
        generatedPayment2 = generateRandomPayment();
        harness.log("Generated payment 2 is " + generatedPayment2);

        int total = generatedPayment1 + generatedPayment2;
        totalFormatted = String.valueOf(total);

        harness.log("Total generated payment: " + total);

        String clientBirthNumber = harness.getConfig("nib.rc.bulk-payment");
        String fromAccountNumber = harness.getConfig("nib.fromaccount.bulk-payment");
        String recipientAccountNumber = harness.getConfig("nib.recipientaccount.czk-currency");
        String recipientAccountNumberOtherBank = harness.getConfig("nib.recipientaccount.outside-moneta.czk-currency");

        clientCredentials = harness.tasks()
                .udebs()
                .findCredentialsByRC(clientBirthNumber);

        formData = PaymentFormData.builder()
                .fromAccountNumber(fromAccountNumber)
                .recipientAccountNumber(recipientAccountNumber)
                .bankCode("0600")
                .recipientAccountNumberOtherBank(recipientAccountNumberOtherBank)
                .bankCodeOtherBank("0100")
                .amount(String.valueOf(generatedPayment1))
                .amountForBulkPayment(String.valueOf(generatedPayment2))
                .amountOverLimit("30000")
                .variableSymbol("666")
                .constantSymbol("777")
                .specificSymbol("888")
                .recipientMessage("this is recipient message from automation test")
                .myNote("this is my note from automation test")
                .overLimitPaymentWarningText("Zadaná částka překračuje Váš denní limit pro platby. Navýšit ho můžete v Nastavení.")
                .build();

        harness.tasks().wso2().client().subsidyCzkAccount(fromAccountNumber, total);
    }

    @Order(1)
    @TestCase(name = "Bulk payment - happy path", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9219")
    public void bulkPayment(Harness harness) {
        NewIbTasks newIbTasks = harness.tasks().newIb();
        formData.setNumberOfPayments("2");
        formData.setNumberOfBulkPaymentVictoryText("2 platby");
        formData.setTotalAmountBulkPayment(totalFormatted);

        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.setBulkPaymentForm(formData))
                .then(newIbTasks.checkSummaryBulkPaymentPageAndConfirmWithMobileKey(formData));

        double amount1FromUdebs = harness.withUdebs()
                .accountMovements()
                .getAmountLastAccountMovement(180, formData.getFromAccountNumber(), formData.getRecipientAccountNumber(), String.valueOf(generatedPayment1));

        Assertions.assertEquals(Double.valueOf(generatedPayment1), amount1FromUdebs, "Payment entered in IB was " + generatedPayment1 + " but last payment in udebs is " + amount1FromUdebs);
        harness.log("Generated payment 1 is:" + generatedPayment1 + " and amount in UDEBS is: " + amount1FromUdebs);

        double amount2FromUdebs = harness.withUdebs()
                .accountMovements()
                .getAmountLastAccountMovement(180, formData.getFromAccountNumber(), formData.getRecipientAccountNumberOtherBank(), String.valueOf(generatedPayment2));

        Assertions.assertEquals(Double.valueOf(generatedPayment2), amount2FromUdebs, "Payment entered in IB was " + generatedPayment2 + " but last payment in udebs is " + amount2FromUdebs);
        harness.log("Generated payment 2 is:" + generatedPayment2 + " and amount in UDEBS is: " + amount2FromUdebs);
    }
}
