package cz.moneta.test.regression.channels.digital.ib.newib.savings.fon;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.newib.tasks.NewIbTasks;
import cz.moneta.test.dsl.newib.util.SavingsFormData;
import cz.moneta.test.harness.annotations.*;
import cz.moneta.test.harness.support.auth.Credentials;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Order;

@TestScenarioWithOrder(name = "Create Savings FON test")
public class SavingsFonTest {
    Credentials clientCredentials;
    String clientId;
    SavingsFormData formData;

    @BeforeEach
    public void setup(Harness harness) {
        String clientBirthNumber = harness.getConfig("nib.rc.termDepositFon");
        String accountNumberRtvServicingDeposit = harness.getConfig("nib.accountNumberRtvServicingDepositFon");
        String destinationAccountNumberDeposit = harness.getConfig("nib.destinationAccountNumberDepositFon");
        String accountNumberRtvServicingWithdrawal = harness.getConfig("nib.accountNumberRtvServicingWithdrawalFon");
        String sourceNumberRtvServicingWithdrawal = harness.getConfig("nib.sourceNumberRtvServicingWithdrawalFon");
        String destinationAccountRtvServicingWithdrawal= harness.getConfig("nib.destinationAccountRtvServicingWithdrawalFon");
        String accountNumberRtvServicingCancel = harness.getConfig("nib.accountNumberRtvServicingCancelFon");
        String sourceNumberRtvServicingCancel = harness.getConfig("nib.sourceNumberRtvServicingCancelFon");
        String destinationAccountRtvServicingCancel= harness.getConfig("nib.destinationAccountRtvServicingCancelFon");

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

        clientId = harness.withUdebs()
                .usersTable()
                .getClientId(clientBirthNumber);

        formData = SavingsFormData.builder()
                .accountNumberRtvServicingDeposit(accountNumberRtvServicingDeposit)
                .destinationAccountNumberDeposit(destinationAccountNumberDeposit)
                .accountNumberRtvServicingWithdrawal(accountNumberRtvServicingWithdrawal)
                .sourceNumberRtvServicingWithdrawal(sourceNumberRtvServicingWithdrawal)
                .destinationAccountRtvServicingWithdrawal(destinationAccountRtvServicingWithdrawal)
                .accountNumberRtvServicingCancel(accountNumberRtvServicingCancel)
                .sourceNumberRtvServicingCancel(sourceNumberRtvServicingCancel)
                .destinationAccountRtvServicingCancel(destinationAccountRtvServicingCancel)
                .build();
    }

    private SavingsFormData createFormData(String currency, String amount, String type, String period, String limitAmountMin, String limitAmountMax, String taxResidence1, String taxResidence2) {
        return SavingsFormData.builder()
                .currency(currency)
                .amount(amount)
                .limitAmountMin(limitAmountMin)
                .limitAmountMax(limitAmountMax)
                .termDepositType(type)
                .termDepositPeriod(period)
                .taxResidence1(taxResidence1)
                .taxResidence2(taxResidence2)
                .build();
    }

    @Order(1)
    @TestCase(name = "Create FON JTV + RTV", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9357")
    public void termDepositCreateFon(Harness harness) {
        String accountNumberCzk = harness.getConfig("nib.currentaccount.termDepositCzkFon");
        String accountNumberEur = harness.getConfig("nib.currentaccount.termDepositEurFon");
        double depositAmountCzk = 50000;
        harness.tasks().wso2().client().subsidyCzkAccount(accountNumberCzk, depositAmountCzk);
        double depositAmountEur = 500;
        harness.tasks().wso2().client().subsidyEurAccount(accountNumberEur, depositAmountEur);
        String operIdJtv = "411";
        String operIdRtv = "412";
        String channelId = "15";
        String expectedStatusSavingAccount = "60";

        SavingsFormData formDataJtv = createFormData("CZK", "50000", "JTV", "3 měsíce", "14999", "999999999999", "Slovensko", "Česká republika");
        SavingsFormData formDataRtv = createFormData("EUR", "500", "RTV", "6 měsíců", "499", "999999999999", "Americká Samoa", "Slovensko");

        NewIbTasks newIbTasks = harness.tasks().newIb();
        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.createTermDeposit(formDataJtv))
                .then(newIbTasks.createTermDeposit(formDataRtv));

        String statusCreatedJtv = harness.withUdebs()
                .transactionsClient()
                .getLastTransactionStatus(clientId, operIdJtv, channelId, 60, expectedStatusSavingAccount);

        Assertions.assertEquals(expectedStatusSavingAccount, statusCreatedJtv, "Status of created JTV  is not equals expected status");
        harness.log("Expected status is: " + expectedStatusSavingAccount + " and status in UDEBS is: " + statusCreatedJtv);

        String statusCreatedRtv = harness.withUdebs()
                .transactionsClient()
                .getLastTransactionStatus(clientId, operIdRtv, channelId, 60, expectedStatusSavingAccount);

        Assertions.assertEquals(expectedStatusSavingAccount, statusCreatedRtv, "Status of created RTV is not equals expected status");
        harness.log("Expected status is: " + expectedStatusSavingAccount + " and status in UDEBS is: " + statusCreatedRtv);
    }

    @Order(2)
    @TestCase(name = "Servicing RTV FON - Deposit", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9297")
    public void rtvServicingDepositFon(Harness harness) {
        String clientBirthNumber = harness.getConfig("nib.rc.rtvServicingDepositFon");
        String operIdRtv = "413";
        String nextOperIdRtv= "404";
        String channelId = "15";
        String expectedStatusRtv = "60";
        String expectedStatusNextRtvTrn = "55";

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

        clientId = harness.withUdebs()
                .usersTable()
                .getClientId(clientBirthNumber);

        NewIbTasks newIbTasks = harness.tasks().newIb();
        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.rtvServicingDeposit(formData));

        String statusRtv = harness.withUdebs()
                .transactionsClient()
                .getLastTransactionStatus(clientId, operIdRtv, channelId, 60, expectedStatusRtv);

        String statusNextRtvTrn = harness.withUdebs()
                .transactionsClient()
                .getLastTransactionStatus(clientId, nextOperIdRtv, channelId, 60, expectedStatusNextRtvTrn);

        harness.log("Expected status is: " + expectedStatusRtv + " and status in UDEBS is: " + statusRtv);
        Assertions.assertEquals(expectedStatusRtv, statusRtv, "Status of Rtv is not equals expected status");

        harness.log("Expected status is: " + expectedStatusNextRtvTrn + " and status in UDEBS is: " + statusNextRtvTrn);
        Assertions.assertEquals(expectedStatusNextRtvTrn, statusNextRtvTrn, "Status of Rtv is not equals expected status");
    }

    @Order(3)
    @TestCase(name = "Servicing RTV FON - Withdrawal", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9298")
    public void rtvServicingWithdrawalFon(Harness harness) {
        String clientBirthNumber = harness.getConfig("nib.rc.rtvServicingWithdrawalFon");
        String operIdRtv = "413";
        String nextOperIdRtv= "404";
        String channelId = "15";
        String expectedStatusRtv = "60";
        String expectedStatusNextRtvTrn = "55";

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

        clientId = harness.withUdebs()
                .usersTable()
                .getClientId(clientBirthNumber);

        NewIbTasks newIbTasks = harness.tasks().newIb();
        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.rtvServicingWithdrawal(formData));

        String statusRtv = harness.withUdebs()
                .transactionsClient()
                .getLastTransactionStatus(clientId, operIdRtv, channelId, 60, expectedStatusRtv);

        String statusNextRtvTrn = harness.withUdebs()
                .transactionsClient()
                .getLastTransactionStatus(clientId, nextOperIdRtv, channelId, 60, expectedStatusNextRtvTrn);

        harness.log("Expected status is: " + expectedStatusRtv + " and status in UDEBS is: " + statusRtv);
        Assertions.assertEquals(expectedStatusRtv, statusRtv, "Status of Rtv is not equals expected status");

        harness.log("Expected status is: " + expectedStatusNextRtvTrn + " and status in UDEBS is: " + statusNextRtvTrn);
        Assertions.assertEquals(expectedStatusNextRtvTrn, statusNextRtvTrn, "Status of Rtv is not equals expected status");
    }

    @Order(4)
    @TestCase(name = "Servicing RTV FON - Cancel", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9299")
    public void rtvServicingCancelFon(Harness harness) {
        String clientBirthNumber = harness.getConfig("nib.rc.rtvServicingCancelFon");
        String operIdRtv = "413";
        String nextOperIdRtv= "404";
        String channelId = "15";
        String expectedStatusRtv = "60";
        String expectedStatusNextRtvTrn = "55";

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

        clientId = harness.withUdebs()
                .usersTable()
                .getClientId(clientBirthNumber);

        NewIbTasks newIbTasks = harness.tasks().newIb();
        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.rtvServicingCancel(formData));

        String statusRtv = harness.withUdebs()
                .transactionsClient()
                .getLastTransactionStatus(clientId, operIdRtv, channelId, 60, expectedStatusRtv);

        String statusNextRtvTrn = harness.withUdebs()
                .transactionsClient()
                .getLastTransactionStatus(clientId, nextOperIdRtv, channelId, 60, expectedStatusNextRtvTrn);

        harness.log("Expected status is: " + expectedStatusRtv + " and status in UDEBS is: " + statusRtv);
        Assertions.assertEquals(expectedStatusRtv, statusRtv, "Status of Rtv is not equals expected status");

        harness.log("Expected status is: " + expectedStatusNextRtvTrn + " and status in UDEBS is: " + statusNextRtvTrn);
        Assertions.assertEquals(expectedStatusNextRtvTrn, statusNextRtvTrn, "Status of Rtv is not equals expected status");
    }
}