package cz.moneta.test.regression.backoffice.ufooperations.regressionsbbo;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.util.credentials.Keys;
import cz.moneta.test.dsl.util.task.ufo.operations.OperationsTasks;
import cz.moneta.test.harness.annotations.*;
import cz.moneta.test.harness.support.auth.Credentials;
import cz.moneta.test.harness.support.auth.Key;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;

import static cz.moneta.test.dsl.ufo.operations.ufoOperations.EXE705_S001_PaymentRecordsToApprovePage.*;
import static cz.moneta.test.dsl.ufo.operations.ufoOperations.TRA740_S001_CreateNewTransferPaymentOrderPage.ID_PAYMENT_ORDER_KEY;
import static cz.moneta.test.dsl.util.task.ufo.cb.CommercialTasks.JIRA_COMMENT_STORE_KEY;
import static cz.moneta.test.harness.annotations.Environment.PPE;

@TestScenarioWithOrder(name = "Payment Order Verification")
public class PaymentOrderVerification {

    private OperationsTasks operationsTasksTasks;
    private String accountNumber;
    private String recipientAccountNumber;
    private String amount;

    @BeforeAll
    public void setUp(Harness harness) {
        accountNumber = harness.getConfig("ufo.operations.account-number-for-payment-order-and-verification");
        recipientAccountNumber = harness.getConfig("ufo.operations.account-number-recipient");
        amount = "5";
    }

    @Order(1)
    @TestCase(name = "Payment Order Verification", environments = {PPE})
    @JiraTestCase(id = "BOC-T8591")
    public void repaymentFee(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials, @Key(Keys.ufo.databoxes.user) Credentials credentialsSecondBanker) {
        operationsTasksTasks = harness.tasks().ufo().operations();

        String orderType = "Poplatek za splacení";

        harness.store("ORDER_TYPE", orderType);
        harness.withUfo()
                .openLoginPage()
                .then(operationsTasksTasks.login().withCredentialsAndSelectFirstOM(credentials))
                .then(operationsTasksTasks.paymentOrderTasks().createTransferOrderForClient(accountNumber))
                .selectOrderType(orderType)
                .fillAmount(amount)
                .clickSend()
                .saveTaskIDPaymentOrder()
                .checkTransactionSuccessfullyEstablished("Transakce byla úspěšně založena pod ID:");

        harness.withUfoSecondInstance()
                .openLoginPage()
                .then(operationsTasksTasks.login().withSecondBankerCredentials(credentialsSecondBanker))
                .clickTransferPaymentOrderVerification()
                .fillTransactionId(harness.get(ID_PAYMENT_ORDER_KEY))
                .clickOk()
                .clickFirstTransactionDetail()
                .then(operationsTasksTasks.paymentOrderTasks().saveTransferOrderInformation())
                .saveConstantSymbol()
                .clickCloseDetail()
                .clickFirstTransactionVerification()
                .then(operationsTasksTasks.paymentOrderTasks().fillPaymentVerificationForm(amount))
                .fillConstantSymbol(harness.get(CONSTANT_SYMBOL_KEY))
                .clickSaveWithConstantSymbol();
    }

    @Order(2)
    @TestCase(name = "Payment Order Verification", environments = {PPE})
    @JiraTestCase(id = "BOC-T8591")
    public void fees(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials, @Key(Keys.ufo.databoxes.user) Credentials credentialsSecondBanker) {
        operationsTasksTasks = harness.tasks().ufo().operations();

        String orderType = "Poplatky";

        harness.store("ORDER_TYPE", orderType);
        harness.withUfo()
                .openLoginPage()
                .then(operationsTasksTasks.login().withCredentialsAndSelectFirstOM(credentials))
                .then(operationsTasksTasks.paymentOrderTasks().createTransferOrderForClient(accountNumber))
                .selectOrderType(orderType)
                .fillAmount(amount)
                .clickSend()
                .saveTaskIDPaymentOrder()
                .checkTransactionSuccessfullyEstablished("Transakce byla úspěšně založena pod ID:");

        harness.withUfoSecondInstance()
                .openLoginPage()
                .then(operationsTasksTasks.login().withSecondBankerCredentials(credentialsSecondBanker))
                .clickTransferPaymentOrderVerification()
                .fillTransactionId(harness.get(ID_PAYMENT_ORDER_KEY))
                .clickOk()
                .clickFirstTransactionDetail()
                .then(operationsTasksTasks.paymentOrderTasks().saveTransferOrderInformation())
                .saveVariableSymbol()
                .saveConstantSymbol()
                .clickCloseDetail()
                .clickFirstTransactionVerification()
                .then(operationsTasksTasks.paymentOrderTasks().fillPaymentVerificationForm(amount))
                .fillVariableSymbol(harness.get(VARIABLE_SYMBOL_KEY))
                .fillConstantSymbol(harness.get(CONSTANT_SYMBOL_KEY))
                .clickSaveWithOptionalSymbols();
    }

    @Order(3)
    @TestCase(name = "Payment Order Verification", environments = {PPE})
    @JiraTestCase(id = "BOC-T8591")
    public void transferOfPositiveBalance(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials, @Key(Keys.ufo.databoxes.user) Credentials credentialsSecondBanker) {
        operationsTasksTasks = harness.tasks().ufo().operations();

        String orderType = "Převod kladného zůstatku";

        harness.store("ORDER_TYPE", orderType);
        harness.withUfo()
                .openLoginPage()
                .then(operationsTasksTasks.login().withCredentialsAndSelectFirstOM(credentials))
                .then(operationsTasksTasks.paymentOrderTasks().createTransferOrderForClient(accountNumber))
                .selectOrderType(orderType)
                .fillRecipientAccountNumber(recipientAccountNumber)
                .fillAmount(amount)
                .clickSend()
                .saveTaskIDPaymentOrder()
                .checkTransactionSuccessfullyEstablished("Transakce byla úspěšně založena pod ID:");

        harness.withUfoSecondInstance()
                .openLoginPage()
                .then(operationsTasksTasks.login().withSecondBankerCredentials(credentialsSecondBanker))
                .clickTransferPaymentOrderVerification()
                .fillTransactionId(harness.get(ID_PAYMENT_ORDER_KEY))
                .clickOk()
                .clickFirstTransactionDetail()
                .then(operationsTasksTasks.paymentOrderTasks().saveTransferOrderInformation())
                .saveVariableSymbol()
                .saveConstantSymbol()
                .saveSpecificSymbol()
                .clickCloseDetail()
                .clickFirstTransactionVerification()
                .then(operationsTasksTasks.paymentOrderTasks().fillPaymentVerificationForm(amount))
                .fillVariableSymbol(harness.get(VARIABLE_SYMBOL_KEY))
                .fillConstantSymbol(harness.get(CONSTANT_SYMBOL_KEY))
                .fillSpecificSymbol(harness.get(SPECIFIC_SYMBOL_KEY))
                .clickSaveWithOptionalSymbols();
    }

    @Order(4)
    @TestCase(name = "Payment Order Verification", environments = {PPE})
    @JiraTestCase(id = "BOC-T8591")
    public void loanRepayment(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials, @Key(Keys.ufo.databoxes.user) Credentials credentialsSecondBanker) {
        operationsTasksTasks = harness.tasks().ufo().operations();

        String orderType = "Splacení úvěru";

        harness.store("ORDER_TYPE", orderType);
        harness.withUfo()
                .openLoginPage()
                .then(operationsTasksTasks.login().withCredentialsAndSelectFirstOM(credentials))
                .then(operationsTasksTasks.paymentOrderTasks().createTransferOrderForClient(accountNumber))
                .selectOrderType(orderType)
                .fillAmount(amount)
                .clickSend()
                .saveTaskIDPaymentOrder()
                .checkTransactionSuccessfullyEstablished("Transakce byla úspěšně založena pod ID:");

        harness.withUfoSecondInstance()
                .openLoginPage()
                .then(operationsTasksTasks.login().withSecondBankerCredentials(credentialsSecondBanker))
                .clickTransferPaymentOrderVerification()
                .fillTransactionId(harness.get(ID_PAYMENT_ORDER_KEY))
                .clickOk()
                .clickFirstTransactionDetail()
                .then(operationsTasksTasks.paymentOrderTasks().saveTransferOrderInformation())
                .saveConstantSymbol()
                .clickCloseDetail()
                .clickFirstTransactionVerification()
                .then(operationsTasksTasks.paymentOrderTasks().fillPaymentVerificationForm(amount))
                .fillConstantSymbol(harness.get(CONSTANT_SYMBOL_KEY))
                .clickSaveWithConstantSymbol();
    }

    @Order(5)
    @TestCase(name = "Payment Order Verification", environments = {PPE})
    @JiraTestCase(id = "BOC-T8591")
    public void interest(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials, @Key(Keys.ufo.databoxes.user) Credentials credentialsSecondBanker) {
        operationsTasksTasks = harness.tasks().ufo().operations();

        String orderType = "Úroky";

        harness.store("ORDER_TYPE", orderType);
        harness.withUfo()
                .openLoginPage()
                .then(operationsTasksTasks.login().withCredentialsAndSelectFirstOM(credentials))
                .then(operationsTasksTasks.paymentOrderTasks().createTransferOrderForClient(accountNumber))
                .selectOrderType(orderType)
                .fillAmount(amount)
                .clickSend()
                .saveTaskIDPaymentOrder()
                .checkTransactionSuccessfullyEstablished("Transakce byla úspěšně založena pod ID:");

        harness.withUfoSecondInstance()
                .openLoginPage()
                .then(operationsTasksTasks.login().withSecondBankerCredentials(credentialsSecondBanker))
                .clickTransferPaymentOrderVerification()
                .fillTransactionId(harness.get(ID_PAYMENT_ORDER_KEY))
                .clickOk()
                .clickFirstTransactionDetail()
                .then(operationsTasksTasks.paymentOrderTasks().saveTransferOrderInformation())
                .saveVariableSymbol()
                .saveConstantSymbol()
                .clickCloseDetail()
                .clickFirstTransactionVerification()
                .then(operationsTasksTasks.paymentOrderTasks().fillPaymentVerificationForm(amount))
                .fillVariableSymbol(harness.get(VARIABLE_SYMBOL_KEY))
                .fillConstantSymbol(harness.get(CONSTANT_SYMBOL_KEY))
                .clickSaveWithOptionalSymbols();
    }

    @Order(6)
    @TestCase(name = "Payment Order Verification", environments = {PPE})
    @JiraTestCase(id = "BOC-T8591")
    public void refundOfCanceledPayments(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials, @Key(Keys.ufo.databoxes.user) Credentials credentialsSecondBanker) {
        operationsTasksTasks = harness.tasks().ufo().operations();

        String orderType = "Vratka storno splátek";

        harness.store("ORDER_TYPE", orderType);
        harness.withUfo()
                .openLoginPage()
                .then(operationsTasksTasks.login().withCredentialsAndSelectFirstOM(credentials))
                .then(operationsTasksTasks.paymentOrderTasks().createTransferOrderForClient(accountNumber))
                .selectOrderType(orderType)
                .fillAmount(amount)
                .clickSend()
                .saveTaskIDPaymentOrder()
                .checkTransactionSuccessfullyEstablished("Transakce byla úspěšně založena pod ID:");

        harness.withUfoSecondInstance()
                .openLoginPage()
                .then(operationsTasksTasks.login().withSecondBankerCredentials(credentialsSecondBanker))
                .clickTransferPaymentOrderVerification()
                .fillTransactionId(harness.get(ID_PAYMENT_ORDER_KEY))
                .clickOk()
                .clickFirstTransactionDetail()
                .then(operationsTasksTasks.paymentOrderTasks().saveTransferOrderInformation())
                .clickCloseDetail()
                .clickFirstTransactionVerification()
                .then(operationsTasksTasks.paymentOrderTasks().fillPaymentVerificationForm(amount))
                .clickSaveWithOptionalSymbols();
    }

    @AfterEach
    protected void teardown(Harness harness) {
        harness.store(JIRA_COMMENT_STORE_KEY,
                "\nUFO Session ID: " + harness.get("reports.tmfj.execution.commentufosessionid", true) +
                        "\nTyp příkazu: " + harness.get("ORDER_TYPE") +
                        "\nID Transakce: " + harness.get(ID_PAYMENT_ORDER_KEY, true) +
                        "\nČíslo účtu: " + harness.getConfig("ufo.operations.account-number-for-payment-order-and-verification"));
    }
}