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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ib.loans.flexicredit.FlexicreditFinalPage;
import cz.moneta.test.dsl.ib.utils.IbUtils;
import cz.moneta.test.dsl.util.credentials.Keys;
import cz.moneta.test.dsl.util.data.client.NewClientWithCurrentAccountInformation;
import cz.moneta.test.dsl.util.task.ib.IbContactDataTasks;
import cz.moneta.test.dsl.util.task.ib.IbTasks;
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.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

@TestScenarioWithOrder(name = "IB sjednání flexikreditu")
public class ArrangementFlexiCreditTest {
    private Credentials credentials;
    private LocalDateTime testExecutionStartTime;
    private NewClientWithCurrentAccountInformation newClientData;
    private IbTasks ibTasks;
    private String clientId;
    private String currentAccount;
    private String udebsBalance;
    private String amount = "2000";
    private String flexiCreditParameters = "Parametry Flexikreditu";
    private String specialOverdraft = "Zvláštní ujednání ke kontokorentnímu úvěru Flexikredit";
    private String standardInformation = "Standardní informace o spotřebitelském úvěru";

    @BeforeAll
    public void createClientWithBuAndIb(Harness harness, @Key(Keys.ufo.banka.banker) Credentials banker) {
        newClientData = harness.tasks()
                .dataPrepareTasks()
                .createNewFoCustomerWithCurrentAccountAndIbViaWso2(banker.getUsername());
        String birthNumber = newClientData.getClientData().birthNumber();

        newClientData.getClientData().lastName();

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

        currentAccount = harness.withUdebs()
                .accountNumbers()
                .getTopCurrentAccount(birthNumber);

        credentials = harness.tasks().wso2().idm().getIbCredentialsByRC(birthNumber);
        
        harness.withIlods().setScore("FXON01S3", birthNumber, null);
        harness.withIlods().setSegment("CUST_INC_VER", "POI_VALID_STRICTLY", birthNumber, null);
    }

    @Disabled
    @Order(1)
    @TestCase(name = "IB sjednání flexikreditu TEA-594")
    public void arrangementFlexiCreditTest(Harness harness) {
        testExecutionStartTime = LocalDateTime.now();
        String date = testExecutionStartTime.format(DateTimeFormatter.ofPattern("d.M.yyyy"));

        ibTasks = harness.tasks().ib();
        FlexicreditFinalPage flexicreditFinalPage =
                harness.withIB()
                        .openLoginPage()
                        .then(ibTasks.loginWithDefaultMobileKeyAnd(credentials))
                        .clickLoans()
                        .flexicreditApplication()
                        .setLoanAmount(amount)
                        .clickContinueButton()
                        .clickContinueButton()
                        .typeMonthlyIncome("50000")
                        .typeMonthlyHouseIncome("80000")
                        .typeOtherMonthlyInstallments("0")
                        .typeOtherLivingCosts("0")
                        .typeDeductionsFromSalary("0")
                        .setResidenceFlag(true)
                        .clickContinueButton()
                        .setSecondDocument("Pas")
                        .agreeAndContinueToOverdraftAmount()
                        .confirmAndContinueToPreContract()
                        .readContract()
                        .readContractAndBack()
                        .onFlexicreditReadContractInformation()
                        .contractIsReadAndAgree()
                        .readContract()
                        .readContractAndBack()
                        .onFlexicreditPreparedToSignPage()
                        .signDocuments()
                        .typeSmsCodeAndSignDocuments("12341234");

        IbUtils.verifyTransactionStatus(harness, testExecutionStartTime, clientId, "65", 3);

        flexicreditFinalPage
                .backToWelcomePage()
                .clickAccountsAndTransactions()
                .verifyFlexiCreditAmount(currentAccount, amount)
                .clickMyDocuments()
                .setDateFrom(date)
                .selectGroup("Půjčky")
                .showDocuments()
                .checkDocumentName(flexiCreditParameters)
                .checkDocumentName(specialOverdraft)
                .checkDocumentName(standardInformation);

        udebsBalance = harness.withUdebs()
                .transactionsClient()
                .getAccountBalance(newClientData.getCurrentAccount()).format().replaceAll("\\D+", "");

        Assertions.assertEquals(amount, udebsBalance, "Balance in UDEBS DB is not equals to set balance ");
    }

    @Disabled
    @Order(2)
    @TestCase(name = "IB - změna kontaktních informací")
    public void changeContactInformation(Harness harness) {
        ibTasks = harness.tasks().ib();
        String newAddress = "Auomation" + new Random().nextInt(999);
        IbContactDataTasks contactDataTasks = harness.tasks().ib().ibContactDataTasks();

        harness.withIB()
                .openLoginPage()
                .then(ibTasks.loginWithDefaultMobileKeyAnd(credentials))
                .goToSettings()
                .contactInformation()
                .then(contactDataTasks.verifyContactData(newClientData))
                .changeContactData()
                .setStreet(newAddress)
                .continueToSignPage()
                .then(contactDataTasks.verifyContactDataOnSignPage(
                        newClientData.getClientData().firstName(),
                        newClientData.getClientData().lastName(),
                        newAddress,
                        newClientData.getClientData().residencyAddress().getDescriptiveNumber(),
                        newClientData.getClientData().residencyAddress().getZip(),
                        newClientData.getClientData().residencyAddress().getCity(),
                        newClientData.getClientData().mobileNumber(),
                        newClientData.getClientData().email()))
                .typeMobileKeyAndSendRequest(IbTasks.DEFAULT_MOBILE_KEY)
                .overviewContactInformation()
                .checkChangedDataText("UPOZORNĚNÍ: Váš požadavek na úpravu kontaktních informací je nyní ve stavu zpracování, další úpravy je možné provádět po jeho dokončení. V případě, že Váš požadavek nebyl zpracován během 24 hodin, kontaktujte náš Zákaznický servis na tel.: 224 443 636.");
    }
}
