package cz.moneta.test.dsl.util.task.monetaportal;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.finanso.FinansoIncomePage;
import cz.moneta.test.dsl.monetaportal.mortgages.MortgagePage;
import org.junit.jupiter.api.Assertions;

import java.util.function.Function;

public class MortgageTasks {
    private static final String CLIENT_INCOME = "50000";
    private static final String CLIENT_INSTALMENT = "1000";
    private static final String MORTGAGE_FIXATION = "5";
    private static final String MAX_MORTGAGE_RESULT_STORE_KEY = "MAX_MORTGAGE_RESULT";
    private static final String MORTGAGE_AMOUNT_VALUE_STORE_KEY = "MORTGAGE_AMOUNT_VALUE";
    private static final String MORTGAGE_REALITY_AMOUNT_VALUE_STORE_KEY = "MORTGAGE_REALITY_AMOUNT_VALUE";
    private static final String MORTGAGE_MATURITY_VALUE_STORE_KEY = "MORTGAGE_MATURITY_VALUE";
    private static final String FINANSO_INCOME_VALUE_STORE_KEY = "FINANSO_INCOME_VALUE";
    private static final String FINANSO_INSTALLMENT_VALUE_STORE_KEY = "FINANSO_INSTALLMENT_VALUE";
    private static final String FINANSO_AMOUNT_STORE_KEY = "FINANSO_AMOUNT";
    private static final String FINANSO_REALITY_AMOUNT_STORE_KEY = "FINANSO_REALITY_AMOUNT";
    private static final String FINANSO_MATURITY_STORE_KEY = "FINANSO_MATURITY";
    private static final String FINANSO_FIXATION_STORE_KEY = "FINANSO_FIXATION";
    private final Harness harness;

    public MortgageTasks(Harness harness) {
        this.harness = harness;
    }

    public Function<MortgagePage, MortgagePage> fillAndStoreFindMaxCalculator() {
        return start -> start
                .clickFindMaxMortgageTabButton()
                .fillIncome(CLIENT_INCOME)
                .fillFee(CLIENT_INSTALMENT)
                .clickIsApplicantFalse()
                .storeMaxMortgageResult(MAX_MORTGAGE_RESULT_STORE_KEY)
                .clickTransferMaxMortgageResult()
                .storeMortgageAmountValue(MORTGAGE_AMOUNT_VALUE_STORE_KEY);
    }

    public Function<MortgagePage, MortgagePage> checkTransferredResult() {
        return start -> {
            Assertions.assertEquals(harness.get(MAX_MORTGAGE_RESULT_STORE_KEY).toString(), harness.get(MORTGAGE_AMOUNT_VALUE_STORE_KEY).toString());
            return start;
        };
    }


    public Function<MortgagePage, FinansoIncomePage> fillAndStoreMortgageCalculator() {
        return start -> start
                .storeMortgageRealityAmountValue(MORTGAGE_REALITY_AMOUNT_VALUE_STORE_KEY)
                .storeMortgageMaturityValue(MORTGAGE_MATURITY_VALUE_STORE_KEY)
                .clickFixation(MORTGAGE_FIXATION)
                .clickIWantLoan();
    }

    public Function<FinansoIncomePage, FinansoIncomePage> storeFinansoPreFilledData() {
        return start -> start.storeIncomeValue(FINANSO_INCOME_VALUE_STORE_KEY)
                .storeInstallmentValue(FINANSO_INSTALLMENT_VALUE_STORE_KEY)
                .goBack()
                .storeFinansoAmount(FINANSO_AMOUNT_STORE_KEY)
                .storeFinansoMaturity(FINANSO_MATURITY_STORE_KEY)
                .storeFinansoFixation(FINANSO_FIXATION_STORE_KEY)
                .storeFinansoRealityAmount(FINANSO_REALITY_AMOUNT_STORE_KEY);
    }

    public Function<FinansoIncomePage, FinansoIncomePage> checkTransferredDataToFinanso() {
        return start -> {
            Assertions.assertEquals(harness.get(MORTGAGE_AMOUNT_VALUE_STORE_KEY).toString(), harness.get(FINANSO_AMOUNT_STORE_KEY));
            Assertions.assertEquals(harness.get(MORTGAGE_REALITY_AMOUNT_VALUE_STORE_KEY).toString(), harness.get(FINANSO_REALITY_AMOUNT_STORE_KEY));
            Assertions.assertEquals(harness.get(MORTGAGE_MATURITY_VALUE_STORE_KEY).toString(), harness.get(FINANSO_MATURITY_STORE_KEY));
            Assertions.assertEquals(MORTGAGE_FIXATION, harness.get(FINANSO_FIXATION_STORE_KEY));
            Assertions.assertEquals(CLIENT_INCOME, harness.get(FINANSO_INCOME_VALUE_STORE_KEY));
            Assertions.assertEquals(CLIENT_INSTALMENT, harness.get(FINANSO_INSTALLMENT_VALUE_STORE_KEY));
            return start;
        };
    }
}
