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.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.time.format.DateTimeFormatter;
import java.util.concurrent.ThreadLocalRandom;

@TestScenarioWithOrder(name = "Standing order tests")
public class StandingOrderTest {

    Credentials clientCredentials;
    PaymentFormData formData;
    String generatedPaymentAmount;

    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 setup(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()
                .fromAccountNumber(fromAccountNumber)
                .recipientAccountNumber("123457")
                .bankCode("0710")
                .standingOrderName(getDate())
                .lastDateOfPayment("3. 6. 2028")
                .dateOfFirstPayment(DateUtils.getNextWorkday().format(DateTimeFormatter.ofPattern("d. M. yyyy")))
                .paymentFrequency("Týdně")
                .variableSymbol("111111")
                .constantSymbol("222222")
                .specificSymbol("333333")
                .recipientMessage("Standing order msg")
                .build();
    }

    @Order(1)
    @TestCase(name = "Standing order - happy path - via Payment button ", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9201")
    public void standingOrderCreateAndDelete(Harness harness) {
        generatedPaymentAmount = generateRandomPayment();
        harness.log("Generated amount for standing order is: " + generatedPaymentAmount);
        NewIbTasks newIbTasks = harness.tasks().newIb();
        formData.setAmount(generatedPaymentAmount);

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