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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ib.loans.consolidationloans.YouRequestToMergeLoansPage;
import cz.moneta.test.harness.annotations.Environment;
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.Disabled;

import java.util.Arrays;
import java.util.List;

import static cz.moneta.test.dsl.ib.loans.consolidationloans.ConsolidationCalculatorPage.TOTAL_CONSOLIDATION_PAYMENT_KEY;
import static cz.moneta.test.dsl.ib.loans.consolidationloans.ConsolidationCalculatorPage.TOTAL_MONTHLY_PAYMENT_KEY;

@TestScenario(name = "Loan consolidation for Moneta client in IB")
public class LoanConsolidation {

    private long removeWhitespaceAndParseInLong(String valueInString) {
        return Long.parseLong(valueInString.replaceAll("\\s", ""));
    }

    private long sumOfValuesFromString(String... values) {
        List<String> valuesList = Arrays.asList(values);
        return valuesList.stream()
                .map(s -> Long.parseLong(s))
                .reduce(0L, Long::sum);
    }

    private long calculationTotalConsolidation(String loanRemainingFirst, String loanRemainingSecond, String cardPaymentFirst, String cardPaymentSecond, String overdraftPaymentFirst, String overdraftPaymentSecond) {
        return sumOfValuesFromString(loanRemainingFirst, loanRemainingSecond, cardPaymentFirst, cardPaymentSecond, overdraftPaymentFirst, overdraftPaymentSecond);
    }

    private long calculationMonthltPayment(String loanPaymentFirst, String loanPaymentSecond, String cardPaymentFirst, String cardPaymentSecond, String overdraftPaymentFirst, String overdraftPaymentSecond) {
        long monthlyPayment = sumOfValuesFromString(loanPaymentFirst, loanPaymentSecond);
        long dispozition = sumOfValuesFromString(cardPaymentFirst, cardPaymentSecond, overdraftPaymentFirst, overdraftPaymentSecond);
        return monthlyPayment + (dispozition / 100 * 5);
    }

    @Disabled("Aktualne je proces konsolidace ve starem IB vypnuty")
    @TestCase(name = "Client create request to loan consolidation in IB", environments = {Environment.TST1, Environment.PPE})
    public void clientRequestToLoansConsolidationInIb(Harness harness) {
        String birthNumber = harness.getConfig("IB.rc.loans-consolidation");
        String loanRemainingFirst = "5000";
        String loanRemainingSecond = "6000";
        String loanRemainingThird = "2000";
        String loanPaymentFirst = "200";
        String loanPaymentSecond = "500";
        String loanPaymentThird = "800";
        String cardPaymentFirst = "6000";
        String cardPaymentSecond = "4000";
        String cardPaymentThird = "2000";
        String overdraftPaymentFirst = "1000";
        String overdraftPaymentSecond = "2000";
        String overdraftPaymentThird = "3000";


        Credentials udebsCredentials = harness.tasks()
                .udebs()
                .findCredentialsByRC(birthNumber);

        YouRequestToMergeLoansPage calculationPage = harness.withIB()
                .openLoginPage()
                .then(harness.tasks().ib().loginWithDefaultMobileKeyAnd(udebsCredentials))
                .clickLoans()
                .clickRequirementToConsolidationLoans()
                .twoTimesClickToAddAnotherLoanButton()
                .fillInInputsLoanFields(loanRemainingFirst, loanRemainingSecond, loanRemainingThird, loanPaymentFirst, loanPaymentSecond, loanPaymentThird)
                .selectProvidersInLoanField("Komerční banka, a.s.", "MONETA Money Bank, a.s.", "Provident Financial s.r.o.")
                .clickToRemoveButtonInLoanField("3")
                .threeTimesClickToAddCreditCardButton()
                .fillInInputsCardFields(cardPaymentFirst, cardPaymentSecond, cardPaymentThird)
                .selectProvidersInCardField("Česká spořitelna, a.s.", "Hello bank! By Cetelem", "UniCredit Bank")
                .clickToRemoveButtonInCardField("3")
                .threeTimesClickToAddOverdraftButton()
                .fillInInputsOverdraftFields(overdraftPaymentFirst, overdraftPaymentSecond, overdraftPaymentThird)
                .selectProvidersInOverdraftField("Air Bank, a.s.", "Raiffeisenbank a.s.", "Home Credit a.s.")
                .clickToRemoveButtonInOverdraftField("3")
                .getTotalMonthlyPaymentAndConsolidationPaymentValues()
                .clickToContinueButton();

        long totalConsolidationFromIb = removeWhitespaceAndParseInLong(harness.get(TOTAL_CONSOLIDATION_PAYMENT_KEY).toString());
        long totalMonthlyPaymentFromIb = removeWhitespaceAndParseInLong(harness.get(TOTAL_MONTHLY_PAYMENT_KEY).toString());
        long calculateVauleForTotalConsolidation = calculationTotalConsolidation(loanRemainingFirst, loanRemainingSecond, cardPaymentFirst, cardPaymentSecond, overdraftPaymentFirst, overdraftPaymentSecond);
        long calculateVauleForMonthlyPayment = calculationMonthltPayment(loanPaymentFirst, loanPaymentSecond, cardPaymentFirst, cardPaymentSecond, overdraftPaymentFirst, overdraftPaymentSecond);
        Assertions.assertEquals(calculateVauleForTotalConsolidation, totalConsolidationFromIb);
        Assertions.assertEquals(calculateVauleForMonthlyPayment, totalMonthlyPaymentFromIb);

        calculationPage.moveSlidersAndCheckValues()
                .clickToInsuranceRadioButton()
                .checkLablesInResultTable("Nová výše splátky", "Nová výše úvěru", "Měsíčně ušetříte na splátce")
                .fillInLoanAndTermInput("10000", "30")
                .clickToRequirementOnlineButton()
                .clickContinueToOverviewLoansButton();
    }
}
