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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ib.settings.ib.PasswordAndChannelManagementPage;
import cz.moneta.test.dsl.ib.utils.IbUtils;
import cz.moneta.test.dsl.util.task.ib.IbTasks;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import cz.moneta.test.harness.support.auth.Credentials;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;

import java.time.LocalDateTime;

@TestScenario(name = "Change limit in Internet banking")
public class ChangeIbLimitTest {
    private Credentials credentials;
    private String clientId;

    @BeforeAll
    public void clientData(Harness harness) {
        String clientBirthNumber = harness.getConfig("IB.rc.change.limit");

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

        credentials = harness.withUdebs()
                .ib()
                .usernameAndPassword()
                .getUsernameAndPasswordByRc(clientBirthNumber);
    }

    @Disabled("Due to rewriting all IB regression tests")
    @TestCase(name = "Change limit in Internet banking")
    public void changeLimitInIB(Harness harness) {
        IbTasks ibTasks = harness.tasks().ib();
        String pattern = "Kč| ";
        LocalDateTime localDateTime = LocalDateTime.now();

        PasswordAndChannelManagementPage passwordAndChannelManagementPage =
                harness.withIB()
                        .openLoginPage()
                        .then(ibTasks.loginWithDefaultMobileKeyAnd(credentials))
                        .goToSettings()
                        .passwordAndChannelManagement();

        String actualLimit = passwordAndChannelManagementPage.getActualLimit().replaceAll(pattern, "");
        int limit = Integer.parseInt(actualLimit) - 1;

        passwordAndChannelManagementPage.changeLimit()
                .setDailyLimit(limit)
                .continueToConfirmPage()
                .compareLimit(limit)
                .setMobileKeyAndSign(IbTasks.DEFAULT_MOBILE_KEY)
                .clickOnchannelManagement();

        IbUtils.verifyTransactionStatus(harness, localDateTime, clientId, "781", 3);

        int changedLimit = Integer.parseInt(passwordAndChannelManagementPage.getActualLimit().replaceAll(pattern, ""));

        Assertions.assertEquals(limit, changedLimit, "Previous limit is not equal to limit value");

    }
}
