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

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

@TestScenarioWithOrder(name = "Payments tests - Mobile operators")
public class PaymentMobileOperatorsTest {
    Credentials clientCredentials;
    PaymentFormData formData;
    String generatedPaymentAmount;

    private String generateRandomPayment() {
        double doubleNumber = ThreadLocalRandom.current().nextInt(300, 400);
        return new DecimalFormat("0").format(doubleNumber).replace(".", ",");
    }

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

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

        formData = PaymentFormData.builder()
                .fromAccountNumber(fromAccountNumber)
                .phoneNumber("606 777 888")
                .mobileOperator("O2")
                .category("Auto")
                .recipientMessage("Test message for recipient.")
                .confirmationPhoneNumber("777 111 888")
                .build();
    }

    @Order(1)
    @TestCase(name = "Test - credit payment", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9212")
    public void creditPaymentFon(Harness harness) {
        generatedPaymentAmount = generateRandomPayment();
        harness.log("Generated payment for credit payment is: " + generatedPaymentAmount);
        NewIbTasks newIbTasks = harness.tasks().newIb();
        formData.setAmount(generatedPaymentAmount);

        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.setCreditPaymentForm(formData))
                .then(newIbTasks.checkSummaryCreditPaymentPageAndConfirmWithMobileKey(formData))
                .clickOnTransactionOverview()
                .onHistoryTabs()
                .checkCreditPaymentTransaction("+420" + formData.getPhoneNumber().replaceAll("\\s+", ""), "-" + formData.getAmount());

        String totalAmount = harness.withUdebs()
                .paymentOrder()
                .getAmountOfLastPaymentOrderDomestic(15, formData.getFromAccountNumber());

        harness.log("Generated payment is:" + generatedPaymentAmount + " and amount in UDEBS is: " + totalAmount);

        Assertions.assertEquals(generatedPaymentAmount, totalAmount, "Credit-Payment entered in IB was " + generatedPaymentAmount + " but last amount in udebs is " + totalAmount);
    }

    @Order(2)
    @TestCase(name = "Test - vodafone invoice payment", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9213")
    public void vodafoneInvoicePaymentFon(Harness harness) {
        generatedPaymentAmount = generateRandomPayment();
        harness.log("Generated payment for vodafone invoice is: " + generatedPaymentAmount);
        NewIbTasks newIbTasks = harness.tasks().newIb();
        formData.setAmount(generatedPaymentAmount);
        formData.setPhoneNumber("777999333");

        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.setVodafoneInvoicePaymentForm(formData))
                .checkVictoryPageVodafoneOff(formData.getPhoneNumber());

        //TODO vodafone backend is temporarily off
        //.then(newIbTasks.checkSummaryVodafoneInvoicePaymentPageAndConfirmWithMobileKey(formData))
        //.clickOnTransactionOverview()
        //.onHistoryTabs()
        //.checkCreditPaymentTransaction("+420" + formData.getPhoneNumber().replaceAll("\\s+", ""), "-" + formData.getAmount());

        //String totalAmount = harness.withUdebs()
        //.paymentOrder()
        //.getAmountOfLastPaymentOrderDomestic(15, formData.getFromAccountNumber());

        //Assertions.assertEquals(generatedPaymentAmount, totalAmount, "Invoice amount entered in IB was " + generatedPaymentAmount + " but last amount in udebs is " + totalAmount);
    }
}
