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

import static cz.moneta.test.harness.support.data.GeneratorType.ACCOUNT_NUMBER;

@TestScenarioWithOrder(name = "Direct debit tests")
public class DirectDebitTest {
    Credentials clientCredentials;
    PaymentFormData formData;

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

    private String getDate() {
        return DateUtils.getFormattedTodayDateTime("MMddHHmmss");
    }

    @BeforeAll
    public void setup(Harness harness) {
        String clientBirthNumber = harness.getConfig("nib.rc.direct.debit-payment");
        String fromAccountNumber = harness.getConfig("nib.from.account.direct.debit");
        String directDebitname = ("NameDD" + getDate());
        String directDebitAcc = harness.generate(ACCOUNT_NUMBER);

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

        formData = PaymentFormData.builder()
                .fromAccountNumber(fromAccountNumber)
                .recipientAccountNumberPrefix("123")
                .recipientAccountNumber(directDebitAcc)
                .bankCode("0100")
                .dateOfFirstPayment(DateUtils.getNextWorkday().format(DateTimeFormatter.ofPattern("d. M. yyyy")))
                .directDebitName(directDebitname)
                .directDebitLimit(generateRandomPayment())
                .dateOfLastPayment("1. 1. 2040")
                .frequencyOfPayments("Týdně")
                .build();
    }

    @JiraTestCase(id = "SB-T9218")
    @TestCase(name = "Direct debit - happy path", environments = {Environment.TST1, Environment.PPE})
    public void directDebitFon(Harness harness) {
        NewIbTasks newIbTasks = harness.tasks().newIb();

        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.setDirectDebitForm(formData))
                .then(newIbTasks.checkSummaryDebitForm(formData))
                .then(newIbTasks.checkVictoryPageOfDirectDebit(formData))
                .clickOnDeleteDirectDebit(formData.getDirectDebitName())
                .then(newIbTasks.deleteDirectDebitFromSettings(formData))
                .checkDirectDebitIsDeletedAndRefreshIfNeeded(formData.getDirectDebitName());
    }
}
