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

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.BeforeEach;
import org.junit.jupiter.api.Order;

import java.text.DecimalFormat;
import java.util.concurrent.ThreadLocalRandom;

@TestScenarioWithOrder(name = "Foreign payment tests")
public class ForeignPaymentFopTest {

    Credentials clientCredentials;
    PaymentFormData formData;
    String generatedPaymentAmount;

    private String generateRandomPayment() {
        double doubleNumber = ThreadLocalRandom.current().nextDouble(1.00, 2.00);
        return new DecimalFormat("0.00").format(doubleNumber).replace(".", ",");
    }

    private String formatAccountNumber(String accountNumber) {
        return accountNumber.length() == 9 ? "0" + accountNumber : accountNumber;
    }

    @BeforeEach
    public void setup(Harness harness) {
        String clientBirthNumber = harness.getConfig("nib.rc.priority-payment-fop");
        String fromAccountNumber = harness.getConfig("nib.fromaccount.priority-payment-fop");
        String recipientAccountNumberEur = harness.getConfig("nib.recipientaccount.eur-currency");

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

        formData = PaymentFormData.builder()
                .fromAccountNumber(fromAccountNumber)
                .recipientAccountNumber("BE62 5100 0754 7061")
                .currency("EUR")
                .typeOfCharge("SHA")
                .recipientName("Romelu Lukaku")
                .recipientAddressStreet("Greewek 1")
                .recipientAddressCity("Brusel")
                .recipientAddressCountry("BE - Belgie")
                .recipientMessage("Test message for recipient - foreign payment")
                .confirmationEmail("tomas.habinak@moneta.cz")
                .bankCountry("BE - Belgie")
                .bankCodeBic("VAPEBE22XXX")
                .bankNameAddress("VAN DE PUT AND CO PRIVATE BANKERS")
                .fromAccountType("Běžný účet CZK")
                .recipientAccountNumberEur(recipientAccountNumberEur)
                .bankCode("0600")
                .variableSymbol("111")
                .constantSymbol("2222")
                .specificSymbol("33333")
                .recipientBankAddressStreet("BANIYAS ROAD")
                .recipientBankCity("DUBAI")
                .validationCurrency("Zvolenou měnu banka nepřijímá, vyberte prosím jinou.")
                .alertCzIban("Zadali jste platbu v CZK na český IBAN. Proveďte platbu raději jako tuzemskou.")
                .errorIban("Toto pole je povinné.")
                .typeOfPayment("Prioritní ")
                .feeForeignPriorityPayment("500")
                .feeForeignPriorityPaymentSummary("500,00")
                .chargeBearerValue("SHA")
                .feeType("SHA")
                .estimatedAmountCurrency("GBP")
                .submitSourceCurrency("CZK")
                .conversionFeeCurrency("CZK")
                .paymentFeeCurrency("CZK")
                .changedRecipientAccountNumber("AT48 3200 0000 1234 58")
                .fees("BEN")
                .build();

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

    @Order(1)
    @TestCase(name = "Foreign payment - happy path", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9263")
    public void foreignPaymentFop(Harness harness) {
        generatedPaymentAmount = generateRandomPayment();
        harness.log("Generated amount for foreign payment is: " + generatedPaymentAmount);
        NewIbTasks newIbTasks = harness.tasks().newIb();
        formData.setAmount(generatedPaymentAmount);

        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.setForeignPaymentForm(formData))
                .then(newIbTasks.checkSummaryForeignPaymentPageAndConfirmWithMobileKey(formData))
                .clickOnTransactionOverview()
                .onHistoryTabs()
                .checkTransaction(formData.getRecipientAccountNumber(), "-" + generatedPaymentAmount);

        double actual = harness.withUdebs()
                .paymentOrder()
                .getAmountOfLastForeignPaymentOrder(5, formatAccountNumber(formData.getFromAccountNumber()));

        double expected = Double.parseDouble(generatedPaymentAmount.replace(",", "."));

        Assertions.assertEquals(expected, actual, "Foreign payment amount entered in IB was " + generatedPaymentAmount + " but last amount in udebs is " + actual);
        harness.log("Generated payment is:" + generatedPaymentAmount + " and amount in UDEBS is: " + actual);
    }
}