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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.util.credentials.Keys;
import cz.moneta.test.dsl.util.data.client.NewClientWithCurrentAccountInformation;
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 cz.moneta.test.harness.support.auth.Key;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Order;

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

@TestScenarioWithOrder(name = "Opening and cancel a savings account in Internet Bank")
public class EstablishmentOfSavingsAccount {
    private static final String ACCOUNT_NAME = "TestSavingAccount";
    private static final String SMS_KEY = "12341234";
    private static final String CHANNEL_ID = "15";
    private static final String EXPECTED_STATUS_SAVING_ACCOUNT = "60";
    private Credentials credentials;
    private String clientId;

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

    @BeforeAll
    public void createClientWithBuAndIb(Harness harness, @Key(Keys.ufo.banka.banker) Credentials banker) {
        NewClientWithCurrentAccountInformation newCustomerWithCurrentAccountAndIb = harness.dataPrepare()
                .createNewFoCustomerWithCurrentAccountAndIbViaWso2(banker.getUsername());

        String clientBirthNumber = newCustomerWithCurrentAccountAndIb.getClientData().birthNumber();

        credentials = harness.tasks().wso2().idm().getIbCredentialsByRC(clientBirthNumber);

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

    @Disabled
    @Order(1)
    @TestCase(name = "Establishment saving account for new client in Internet Bank", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id="IB-T8528")
    public void establishmentOfSavingAccountForNewClient(Harness harness) {
        String operIdCreatedSavingAccount = "332";
        String operIdDocuments = "334";
        int numberOfExpectedDocument = 6;

        harness.log("ClientID is: " + clientId);

        harness.withIB()
                .openLoginPage()
                .then(harness.tasks().ib().loginWithDefaultMobileKeyAnd(credentials))
                .checkAndConfirmCookies()
                .clickSavings()
                .clickButtonEstablishmentOnlineSavingAccount()
                .fillInAccountName(ACCOUNT_NAME)
                .selectTaxJurisdiction("Česká republika")
                .confirmCheckPrecontractualInformation()
                .confirmCheckProductionConditions()
                .clickToContinueButton()
                .fillInSmsKey(SMS_KEY)
                .clickToConfirmationButton()
                .clickLogOff();

        String dateTimeCreatedSavingAccount = nowDateTimeWithFromat();
        harness.log("Create saving account: " + dateTimeCreatedSavingAccount);

        String statusCreatedSavingAccount = harness.withUdebs()
                .transactionsClient()
                .getLastTransactionStatus(clientId, operIdCreatedSavingAccount, CHANNEL_ID, 180, EXPECTED_STATUS_SAVING_ACCOUNT);
        Assertions.assertEquals(EXPECTED_STATUS_SAVING_ACCOUNT, statusCreatedSavingAccount, "Status of created saving account is not equals expected status");

        int numberOfCreatedDocuments = harness.withUdebs()
                .transactionsClient()
                .delayAndGetNumberOfTransactionsCreatedDocuments(clientId, operIdDocuments, CHANNEL_ID, dateTimeCreatedSavingAccount, 330);
        harness.log("Number of drocuments transactions in UDEBS: " + numberOfCreatedDocuments);
        Assertions.assertEquals(numberOfExpectedDocument, numberOfCreatedDocuments, "Number of created documents transactions is not equale number of expected transactions");

        harness.withIB()
                .openLoginPage()
                .then(harness.tasks().ib().loginWithDefaultMobileKeyAnd(credentials))
                .clickToMyDocuments()
                .checkDocumentName("Předsmluvní informace spořicího účtu")
                .checkDocumentName("Parametry spořicího účtu")
                .checkDocumentName("Úrokový lístek")
                .checkDocumentName("Sazebník poplatků za produkty a služby")
                .checkDocumentName("Ostatní produktové podmínky")
                .clickSavings()
                .clickToSpecificSavingAccountOnSavingAccountPage(ACCOUNT_NAME);
    }

    @Disabled
    @Order(2)
    @TestCase(name = "Cancel saving account for client in Internet Bank", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id="IB-T8529")
    public void cancelSavingAccountToClient(Harness harness) {
        String operIdCancelingSavingAccount = "352";

        harness.withIB()
                .openLoginPage()
                .then(harness.tasks().ib().loginWithDefaultMobileKeyAnd(credentials))
                .checkAndConfirmCookies()
                .clickSavings()
                .clickToSpecificSavingAccountOnSavingAccountPage(ACCOUNT_NAME)
                .clickToCancel()
                .clickToContinueButton()
                .fillInMobileKey(SMS_KEY)
                .clickSignButton()
                .clickLogOff();

        String statusCreatedSavingAccount = harness.withUdebs()
                .transactionsClient()
                .getLastTransactionStatus(clientId, operIdCancelingSavingAccount, CHANNEL_ID, 120, EXPECTED_STATUS_SAVING_ACCOUNT);
        Assertions.assertEquals(EXPECTED_STATUS_SAVING_ACCOUNT, statusCreatedSavingAccount, "Status of created saving account is not equals expected status");
    }
}