package cz.moneta.test.regression.transactional.payments.cashless.cashman;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.cashman.tasks.CashmanTasks;
import cz.moneta.test.dsl.cashman.utils.CashmanUtils;
import cz.moneta.test.dsl.util.DateUtils;
import cz.moneta.test.dsl.util.data.payment.PaymentData;
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 org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Order;

import java.util.concurrent.ThreadLocalRandom;

import static cz.moneta.test.dsl.cashman.enums.CashmanOrderStatus.APPROVED;
import static cz.moneta.test.dsl.cashman.pages.menu_OM.CashmanOMNewOrderPage.ORDER_ID_KEY;

@TestScenarioWithOrder(name = "Create New Order")
public class CreateNewOrderTests {
    private PaymentData data;
    private CashmanTasks tasks;

    @BeforeAll
    void setUpTestData() {
        data = PaymentData.builder()
                .todayDate(DateUtils.getFormattedTodayDate("dd.MM.yyyy"))
                .build();
    }

    @BeforeEach
    void setUpUniqueTestData(Harness harness) {
        data.setBanknotesAmount(String.valueOf(ThreadLocalRandom.current().nextInt(1, 5)));
        data.setCoinsAmount(String.valueOf(ThreadLocalRandom.current().nextInt(1, 5)));
        data.setCoinsAmount(String.valueOf(ThreadLocalRandom.current().nextInt(1, 5)));
        tasks = new CashmanTasks(harness);
    }

    @Order(1)
    @JiraTestCase(id = "PAY-T4315")
    @TestCase(name = "Create New Order And Check In OM And Deposit", environments = {Environment.TST1, Environment.DEV})
    void createNewOrderAndCheckInOMAndDeposit(Harness harness) {
        data.setBanknotesSummary(String.valueOf(100 * Integer.valueOf(data.getBanknotesAmount())));
        data.setCoinsSummary(String.valueOf(10 * Integer.valueOf(data.getCoinsAmount())));
        data.setSummary(String.valueOf(Integer.valueOf(data.getBanknotesSummary()) + Integer.valueOf(data.getCoinsSummary())));

        tasks.loginTasks().openCashmanHomePage(harness)
                .then(tasks.createNewOrder(data))
                .then(tasks.checkOrderSummaries(data))
                .then(tasks.submitNewOrder(data));
        harness.withCashman().closeCashman();

        data.setOrderId(CashmanUtils.getOrderId(harness));

        tasks.loginTasks().openCashmanHomePage(harness)
                .then(tasks.checkOrderPresence(data, APPROVED))
                .then(tasks.checkOrderPresenceInDeposit(data));
        harness.withCashman().closeCashman();

        harness.store("reports.tmfj.execution.comment", harness.get(ORDER_ID_KEY));
    }
}
