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.EnvelopesFormData;
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 = "Envelopes tests")
public class EnvelopesSettingsTest {
    Credentials clientCredentials;
    EnvelopesFormData formData;

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

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

    @BeforeAll
    public void setup(Harness harness) {
        String clientBirthNumber = harness.getConfig("nib.rc.envelopes");

        clientCredentials = harness.tasks()
                .udebs()
                .findCredentialsByRC(clientBirthNumber);
        formData = EnvelopesFormData.builder()
                .envelopeName(getDate())
                .amount(generateRandomPayment())
                .category("auto")
                .build();
    }

    @Order(1)
    @TestCase(name = "Envelopes - via Settings", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9342")
    public void newEnvelopeCreateEditAndDelete(Harness harness) {
        NewIbTasks newIbTasks = harness.tasks().newIb();

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