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.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.*;

import java.text.DecimalFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ThreadLocalRandom;

@TestScenarioWithOrder(name = "Domestic payment order")
public class DomesticPaymentOrderFopTest {
    Credentials clientCredentials;
    PaymentFormData formData;
    double generatedPayment;
    String generatedPaymentString;

    private double generateRandomPayment() {
        String random = new DecimalFormat("#.00").format(ThreadLocalRandom.current().nextDouble(1.00, 2.00));
        generatedPaymentString = random.replace(".", ",");
        return Double.parseDouble(random.replace(",", "."));
    }

    @BeforeEach
    public void generatePayment(Harness harness) {
        generatedPayment = generateRandomPayment();
        harness.log("Generated payment is " + generatedPayment);
    }

    @BeforeAll
    public void setup(Harness harness) {
        String clientBirthNumber = harness.getConfig("nib.rc.priority-payment-fop");
        String fromAccountNumber = harness.getConfig("nib.fromaccount.priority-payment-fop");
        String recipientAccountNumber = harness.getConfig("nib.recipientaccount.czk-currency");
        String recipientAccountNumberOtherBank = harness.getConfig("nib.recipientaccount.outside-moneta.czk-currency");

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

        formData = PaymentFormData.builder()
                .fromAccountType("Běžný účet CZK")
                .fromAccountNumber(fromAccountNumber)
                .recipientAccountNumber(recipientAccountNumber)
                .recipientAccountNumberOtherBank(recipientAccountNumberOtherBank)
                .category("Bankomat")
                .bankCode("0600")
                .bankCodeOtherBank("0100")
                .variableSymbol("111")
                .constantSymbol("2222")
                .specificSymbol("33333")
                .recipientMessage("Toto je zpráva pro příjemce")
                .myNote("Toto je moje poznamka")
                .build();

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

    private void assertAmountsEqual(double expected, double actual) {
        Assertions.assertEquals(expected, actual, "Expected payment: " + expected + ", but actual payment in udebs: " + actual);
    }

    @Order(1)
    @TestCase(name = "Domestic payment order – Priority - all steps", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9264")
    public void domesticPaymentOrderPriorityAllStepsFop(Harness harness) {
        NewIbTasks newIbTasks = harness.tasks().newIb();
        formData.setAmount(generatedPaymentString);
        formData.setDateOfPayment(DateUtils.getFormattedTodayDate("d. M. yyyy"));
        formData.setTypeOfPayment("Prioritní");

        Assumptions.assumeTrue(LocalTime.now().isBefore(LocalTime.of(14, 0)),
                "Skipped: Priority payments are processed only until 14:00.");

        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.setDomesticPriorityPaymentFirstStep(formData))
                .then(newIbTasks.setDomesticPriorityPaymentSecondStep(formData))
                .then(newIbTasks.setDomesticPaymentThirdStep(formData));

        double amount = harness.withUdebs()
                .accountMovements()
                .getAmountLastAccountMovement(180, formData.getFromAccountNumber(), formData.getRecipientAccountNumberOtherBank(), String.valueOf(generatedPayment));

        assertAmountsEqual(generatedPayment, amount);
        harness.log("Generated payment is:" + generatedPayment + " and amount in UDEBS is: " + amount);
    }

    @Order(2)
    @TestCase(name = "Domestic payment order – Instant - all steps", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9265")
    public void domesticPaymentOrderInstantAllStepsFop(Harness harness) {
        NewIbTasks newIbTasks = harness.tasks().newIb();
        formData.setAmount(generatedPaymentString);
        formData.setTypeOfPayment("Okamžitá");
        formData.setDateOfPayment("Okamžitě");

        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.setDomesticPaymentFirstStep(formData))
                .clickContinue()
                .then(newIbTasks.checkSummaryPaymentPageAndConfirmWithMobileKey(formData))
                .then(newIbTasks.setDomesticPaymentThirdStep(formData));

        double amount = harness.withUdebs()
                .accountMovements()
                .getAmountLastAccountMovement(180, formData.getFromAccountNumber(), formData.getRecipientAccountNumber(), String.valueOf(generatedPayment));

        assertAmountsEqual(generatedPayment, amount);
        harness.log("Generated payment is:" + generatedPayment + " and amount in UDEBS is: " + amount);
    }

    @Order(3)
    @TestCase(name = "Domestic payment order – standard", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9266")
    public void domesticPaymentOrderStandardAllStepsFop(Harness harness) {
        NewIbTasks newIbTasks = harness.tasks().newIb();
        formData.setAmount(generatedPaymentString);
        formData.setTypeOfPayment("Standardní");
        formData.setDateOfPayment(DateUtils.getFormattedTodayDate("d. M. yyyy"));

        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.setDomesticPaymentFirstStep(formData))
                .clickContinue()
                .then(newIbTasks.checkSummaryPaymentPageAndConfirmWithMobileKey(formData))
                .then(newIbTasks.setDomesticPaymentThirdStep(formData));

        double amount = harness.withUdebs()
                .accountMovements()
                .getAmountLastAccountMovement(180, formData.getFromAccountNumber(), formData.getRecipientAccountNumber(), String.valueOf(generatedPayment));

        assertAmountsEqual(generatedPayment, amount);
        harness.log("Generated payment is:" + generatedPayment + " and amount in UDEBS is: " + amount);
    }

    @Order(4)
    @TestCase(name = "Domestic payment order – to own accounts", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9727")
    public void domesticPaymentOrderToOwnAccountFop(Harness harness) {
        NewIbTasks newIbTasks = harness.tasks().newIb();
        formData.setAmount(generatedPaymentString);
        formData.setRecipientAccountNumber(formData.getRecipientAccountNumber());

        String savingsAccount = harness.getConfig("nib.savings.account.fop");

        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.setDomesticPaymentToOwnAccount(formData, savingsAccount))
                .clickContinue()
                .then(newIbTasks.checkSummaryPaymentToOwnAccountPageAndConfirm(formData, savingsAccount))
                .then(newIbTasks.setDomesticPaymentThirdStep(formData));

        double amount = harness.withUdebs()
                .accountMovements()
                .getAmountLastAccountMovement(180, formData.getFromAccountNumber(), savingsAccount, String.valueOf(generatedPayment));

        assertAmountsEqual(generatedPayment, amount);
        harness.log("Generated payment is:" + generatedPayment + " and amount in UDEBS is: " + amount);
    }
}
