package cz.moneta.test.regression.channels.digital.ib.oldib.account.saving;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.util.task.ib.IbTasks;
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.data.Browser;
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.Disabled;
import org.junit.jupiter.api.Order;

import java.text.SimpleDateFormat;
import java.util.Date;

import static cz.moneta.test.dsl.ib.welcome.WelcomePage.CURRENT_ACCOUNT_DISPO_KEY;
import static cz.moneta.test.dsl.ib.welcome.WelcomePage.SAVING_ACCOUNT_DISPO_KEY;
import static cz.moneta.test.dsl.util.task.ib.IbTasks.DEFAULT_MOBILE_KEY;

@TestScenarioWithOrder(name = "Transfer of founds between current account and saving account", browsers = Browser.GOOGLE_CHROME)
public class TransferOfFoundsTest {
    private static final String CHANNEL_ID = "15";
    private static final String EXPECTED_STATUS_SAVING_ACCOUNT = "60";
    private static final String OPER_ID_TRANSACTION_353 = "353";
    private static final String OPER_ID_TRANSACTION_354 = "354";
    String clientId;
    Credentials clientCredentials;
    String amountToTransfer = "1,00";

    private double parseDispoFromStoreElementContentIb(Harness harness, String keyStoreElementContentIb) {
        String parsedAvailableBalance = harness.get(keyStoreElementContentIb)
                .toString()
                .replaceAll("\\s", "")
                .replace("Kč", "")
                .replace(",", ".");
        return Double.valueOf(parsedAvailableBalance);
    }

    private double calculateDisponibileBalanceAfterTest(Harness harness, String keyStoreElementContentIb) {
        double dispoValueAfterCalculate = parseDispoFromStoreElementContentIb(harness, keyStoreElementContentIb);
        return dispoValueAfterCalculate + Double.valueOf(amountToTransfer.replace(",", "."));
    }

    private double calculateDisponibileBalanceFromSavingAccountAfterTest(Harness harness, String keyStoreElementContentIb) {
        double dispoValueAfterCalculate = parseDispoFromStoreElementContentIb(harness, keyStoreElementContentIb);
        return dispoValueAfterCalculate - Double.valueOf(amountToTransfer.replace(",", "."));
    }

    private String nowDateTimeWithFormat() {
        Date nowDateTime = new Date();
        SimpleDateFormat createdAccountDateTime = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
        return createdAccountDateTime.format(nowDateTime);
    }

    @BeforeEach
    public void getCredentialsFromUdebs(Harness harness) {
        String clientBirthNumber = harness.getConfig("IB.rc.transfer-founds");

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

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

    @Disabled
    @Order(1)
    @TestCase(name = "Transfer of founds from current account to saving account in IB", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id="IB-T8530")
    public void transferOfFoundsFromCurrentAccountToSavingAccount(Harness harness) {
        IbTasks ibTasks = harness.tasks()
                .ib();

        harness.withIB()
                .openLoginPage()
                .then(ibTasks.loginWithDefaultMobileKeyAnd(clientCredentials))
                .then(ibTasks.transferOfFoundsSavingAccount())
                .inputAmountToTransfer(amountToTransfer)
                .checkElementsTransferToSavingAccount()
                .clickToContinueButton()
                .checkValuesToTransactionToSavingAccount(amountToTransfer)
                .clickToWithoutSignButton()
                .clickToOverviewSavingAccounts()
                .clickLogOff();

        String dateTimeCreatedSavingAccount = nowDateTimeWithFormat();
        harness.log("Create transaction: " + dateTimeCreatedSavingAccount);

        String statusTransaction = harness.withUdebs()
                .transactionsClient()
                .getLastTransactionStatus(clientId, OPER_ID_TRANSACTION_353, CHANNEL_ID, 60, dateTimeCreatedSavingAccount, EXPECTED_STATUS_SAVING_ACCOUNT);
        Assertions.assertEquals(EXPECTED_STATUS_SAVING_ACCOUNT, statusTransaction, "Status of created transaction is not equals expected status");

        double expectedDispoSavingAccount = calculateDisponibileBalanceAfterTest(harness, SAVING_ACCOUNT_DISPO_KEY);
        harness.log("Dispo saving account before test " + expectedDispoSavingAccount);

        harness.withIB()
                .openLoginPage()
                .then(ibTasks.loginWithDefaultMobileKeyAnd(clientCredentials))
                .getDispoSavingAccount()
                .clickLogOff();

        double dispoSavingAccountAfterTest = parseDispoFromStoreElementContentIb(harness, SAVING_ACCOUNT_DISPO_KEY);
        harness.log("Dispo saving account after test " + dispoSavingAccountAfterTest);
        Assertions.assertEquals(expectedDispoSavingAccount, dispoSavingAccountAfterTest, 0, "The disposition balance does not equal the expected after test in IB");
    }

    @Disabled
    @Order(2)
    @TestCase(name = "Transfer of founds from saving account to current account in IB", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id="IB-T8531")
    public void transferOfFoundsFromSavingAccountToCurrentAccount(Harness harness) {
        IbTasks ibTasks = harness.tasks()
                .ib();

        harness.withIB()
                .openLoginPage()
                .then(ibTasks.loginWithDefaultMobileKeyAnd(clientCredentials))
                .then(ibTasks.transferOfFoundsSavingAccount())
                .chooseTransferFromSavingAccount()
                .inputAmountToTransfer(amountToTransfer)
                .checkElementsTransferFromSavingAccount()
                .clickToContinueButton()
                .checkValuesToTransactionFromSavingAccount(amountToTransfer)
                .clickToWithoutSignButton()
                .clickToOverviewSavingAccounts()
                .clickLogOff();

        String dateTimeCreatedSavingAccount = nowDateTimeWithFormat();
        harness.log("Create transaction: " + dateTimeCreatedSavingAccount);

        String statusTransaction = harness.withUdebs()
                .transactionsClient()
                .getLastTransactionStatus(clientId, OPER_ID_TRANSACTION_353, CHANNEL_ID, 60, dateTimeCreatedSavingAccount, EXPECTED_STATUS_SAVING_ACCOUNT);
        Assertions.assertEquals(EXPECTED_STATUS_SAVING_ACCOUNT, statusTransaction, "Status of created transaction is not equals expected status");

        double expectedDispoSavingAccount = calculateDisponibileBalanceFromSavingAccountAfterTest(harness, SAVING_ACCOUNT_DISPO_KEY);
        harness.log("Dispo saving account before test " + expectedDispoSavingAccount);

        harness.withIB()
                .openLoginPage()
                .then(ibTasks.loginWithDefaultMobileKeyAnd(clientCredentials))
                .getDispoSavingAccount()
                .clickLogOff();

        double dispoSavingAccountAfterTest = parseDispoFromStoreElementContentIb(harness, SAVING_ACCOUNT_DISPO_KEY);
        harness.log("Dispo saving account after test " + dispoSavingAccountAfterTest);
        Assertions.assertEquals(expectedDispoSavingAccount, dispoSavingAccountAfterTest, 0, "The disposition balance does not equal the expected after test in IB");

    }

    @Disabled
    @Order(3)
    @TestCase(name = "Transfer of founds from saving account to current account in IB", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id="IB-T8532")
    public void transferOfFoundsSaveToSign(Harness harness) {
        IbTasks ibTasks = harness.tasks()
                .ib();

        harness.withIB()
                .openLoginPage()
                .then(ibTasks.loginWithDefaultMobileKeyAnd(clientCredentials))
                .then(ibTasks.transferOfFoundsSavingAccount())
                .inputAmountToTransfer(amountToTransfer)
                .checkElementsTransferToSavingAccount()
                .clickToContinueButton()
                .checkValuesToTransactionToSavingAccount(amountToTransfer)
                .clickSaveToSignButton()
                .clickToWelcomePageButton()
                .then(ibTasks.transferOfFoundsSavingAccount())
                .chooseTransferFromSavingAccount()
                .checkElementsTransferFromSavingAccount()
                .inputAmountToTransfer(amountToTransfer)
                .clickToContinueButton()
                .clickSaveToSignButton()
                .clickPaymentsToSingButton()
                .clicSelectAll()
                .clicSignSelected()
                .clickSign()
                .typeSmsCode(DEFAULT_MOBILE_KEY)
                .clickSignSms()
                .clickLogOff();

        String dateTimeCreatedSavingAccount = nowDateTimeWithFormat();
        harness.log("Create transaction: " + dateTimeCreatedSavingAccount);

        double dispoSavingAccountBeforeTest = parseDispoFromStoreElementContentIb(harness, SAVING_ACCOUNT_DISPO_KEY);
        double dispoCurrentAccountBeforeTest = parseDispoFromStoreElementContentIb(harness, CURRENT_ACCOUNT_DISPO_KEY);

        String statusTransaction353 = harness.withUdebs()
                .transactionsClient()
                .getLastTransactionStatus(clientId, OPER_ID_TRANSACTION_353, CHANNEL_ID, 60, dateTimeCreatedSavingAccount, EXPECTED_STATUS_SAVING_ACCOUNT);
        Assertions.assertEquals(EXPECTED_STATUS_SAVING_ACCOUNT, statusTransaction353, "Status of created transaction 353 is not equals expected status 60");

        String statusTransaction354 = harness.withUdebs()
                .transactionsClient()
                .getLastTransactionStatus(clientId, OPER_ID_TRANSACTION_354, CHANNEL_ID, 0, dateTimeCreatedSavingAccount, EXPECTED_STATUS_SAVING_ACCOUNT);
        Assertions.assertEquals(EXPECTED_STATUS_SAVING_ACCOUNT, statusTransaction354, "Status of created transaction 354 is not equals expected status 60");

        harness.withIB()
                .openLoginPage()
                .then(ibTasks.loginWithDefaultMobileKeyAnd(clientCredentials))
                .getDispoSavingAccount()
                .getDispoCurrentAccount()
                .clickLogOff();

        double dispoSavingAccountAfterTest = parseDispoFromStoreElementContentIb(harness, SAVING_ACCOUNT_DISPO_KEY);
        double dispoCurrentAccountAfterTest = parseDispoFromStoreElementContentIb(harness, CURRENT_ACCOUNT_DISPO_KEY);
        Assertions.assertEquals(dispoSavingAccountBeforeTest, dispoSavingAccountAfterTest, 0, "The disposition balance on saving account does not equal the expected after test in IB");
        Assertions.assertEquals(dispoCurrentAccountBeforeTest, dispoCurrentAccountAfterTest, 0, "The disposition balance on current account does not equal the expected after test in IB");
    }
}