package cz.moneta.test.regression.channels.digital.ib.newib.settings;

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.dsl.util.DateUtils;
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.BeforeAll;
import org.junit.jupiter.api.Order;

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

@TestScenarioWithOrder(name = "Settings - Standing order creation test")
public class StandingOrdersSettingsTest {
    Credentials clientCredentials;
    PaymentFormData formData;

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

    private String getDate() {
        return DateUtils.getFormattedTodayDateTime("MM/dd-HH:mm:ss");
    }

    @BeforeAll
    public void getCredentials(Harness harness) {
        String clientBirthNumber = harness.getConfig("nib.rc.standing-order");
        String fromAccountNumber = harness.getConfig("nib.fromaccount.standing-order");

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

        formData = PaymentFormData.builder()
                .prefixAccountNumber("19")
                .fromAccountNumber(fromAccountNumber)
                .recipientAccountNumber("123457")
                .bankCode("0710")
                .standingOrderName(getDate())
                .lastDateOfPayment("6. 6. 2038")
                .paymentFrequency("Ročně")
                .recipientMessage(getDate())
                .build();
    }

    @Order(1)
    @TestCase(name = "Standing order - Settings", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9345")
    public void standingOrderSettings(Harness harness) {
        String initialAmount = generateRandomPayment();
        String updatedAmount = generateRandomPayment();
        harness.log("Generated initial amount for standing order: " + initialAmount + " and updated amount for change test: " + updatedAmount);
        formData.setAmount(initialAmount);
        formData.setAmount2(updatedAmount);
        NewIbTasks newIbTasks = harness.tasks().newIb();

        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.createStandingOrderViaSettings(formData));
    }
}
