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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.brokerportal.contract.CalculatorPage;
import cz.moneta.test.dsl.brokerportal.enums.Client;
import cz.moneta.test.dsl.util.TestUtils;
import cz.moneta.test.harness.support.data.GeneratorType;
import java.util.function.Function;

import static cz.moneta.test.dsl.brokerportal.enums.Client.FIRST_CLIENT;
import static cz.moneta.test.dsl.brokerportal.enums.Row.*;

public class CalculatorTasks {

    private final Harness harness;
    private final CommonTasks commonTasks;

    public CalculatorTasks(Harness harness) {
        this.harness = harness;
        this.commonTasks = harness.tasks().brokerPortal().commonTasks();
    }

    public Function<CalculatorPage, CalculatorPage> fillCalculator(String loanAmount) {
        return start -> start
                .then(fillClient(FIRST_CLIENT))
                .then(fillLoan(loanAmount));
    }

    public Function<CalculatorPage, CalculatorPage> calculateCalculator() {
        return start -> start
                .clickCalculateButton()
                .checkCalculationSuccessfulAlert();
    }

    public Function<CalculatorPage, CalculatorPage> calculateAndSaveCalculator() {
        return start -> start
                .then(calculateCalculator())
                .clickSaveCalculationButton()
                .getContractId(harness);
    }

    public Function<CalculatorPage, CalculatorPage> fillFullCalculator(String loanAmount) {
        return start -> start
                //Client 1 - Section basic information
                .then(fillBasicInformation(FIRST_CLIENT))
                .fillContactCountry(FIRST_CLIENT.getValue(),"Kuba")
                .fillNationality(FIRST_CLIENT.getValue(),"Kuba")
                .clickMateCheckbox(FIRST_CLIENT)

                //Client 1 - Section incomes from employment
                .then(fillIncomesFromEmployment(FIRST_CLIENT))

                //Client 1 - Section incomes from business
                .clickBusinessIncome()
                .selectYearLatest(FIRST_CLIENT.getValue(),commonTasks.getCurrentYearIncrementedByYears(-1))
                .fillTurnoverLatest(FIRST_CLIENT.getValue(),"900000")
                .fillTaxBaseLatest(FIRST_CLIENT.getValue(),"12000")
                .fillTaxAmountLatest(FIRST_CLIENT.getValue(),"45000")
                .selectCostAccountingType(FIRST_CLIENT.getValue(),"Výdaje procentem z příjmu")
                .selectCostPercentageRate(FIRST_CLIENT.getValue(),"40%")
                .selectNaceCode(FIRST_CLIENT.getValue(),"20149")
                .clickDapCheckbox(FIRST_CLIENT)
                .selectYearPenultimate(FIRST_CLIENT.getValue(),commonTasks.getCurrentYearIncrementedByYears(-2))
                .fillTurnoverPenultimate(FIRST_CLIENT.getValue(),"134975")
                .fillTaxBasePenultimate(FIRST_CLIENT.getValue(),"13475")

                //Client 1 - Section incomes other
                .clickOtherIncome()
                .selectOtherIncome(FIRST_CLIENT.getValue(), FIRST_ROW.getValue(),"výživné")
                .fillOtherIncomeAmount(FIRST_CLIENT.getValue(), FIRST_ROW.getValue(),"20000")
                .clickAddIncomeButton(FIRST_CLIENT)
                .selectOtherIncome(FIRST_CLIENT.getValue(), SECOND_ROW.getValue(),"ostatní příjem")
                .fillOtherIncomeAmount(FIRST_CLIENT.getValue(), SECOND_ROW.getValue(),"10000")

                //Client 1 - Section expenditures
                .then(fillExpenditures(FIRST_CLIENT))
                //Add Credit Card
                .clickAddExpenditureButton(FIRST_CLIENT)
                .selectExpenditureType(FIRST_CLIENT.getValue(), SECOND_ROW.getValue(),"Kreditní karta")
                .fillExpenditureCreditRestriction(FIRST_CLIENT.getValue(), SECOND_ROW.getValue(),"1000")
                .selectExpenditureCorrection(FIRST_CLIENT.getValue(), SECOND_ROW.getValue(),"refinancování úvěru")
                //Add Leasing
                .clickAddExpenditureButton(FIRST_CLIENT)
                .selectExpenditureType(FIRST_CLIENT.getValue(), THIRD_ROW.getValue(),"Leasing")
                .fillExpenditureInstalmentAmount(FIRST_CLIENT.getValue(), THIRD_ROW.getValue(),"10000")
                .fillExpenditureDebt(FIRST_CLIENT.getValue(), THIRD_ROW.getValue(),"1000")
                .selectExpenditureCorrection(FIRST_CLIENT.getValue(), THIRD_ROW.getValue(),"doložené ke schval.")

                //Section loan
                .then(fillLoan(loanAmount))
                .selectProduct("Pružná")
                .fillMaturity("20")
                .selectFixation("7 let")
                .selectService("Flexibilita")
                .selectInsuranceType("Bez pojištění")
                .clickInsuranceCheckbox(FIRST_CLIENT)
                .clickInsuranceCheckbox(FIRST_CLIENT)
                //Add property
                .clickAddPropertyButton()
                .selectLoanSecurityEstateType(SECOND_ROW.getValue(),"Rodinný dům")
                .fillLoanSecurityAmount(SECOND_ROW.getValue(),"10000000")
                .selectLoanSecurityContractRelation(SECOND_ROW.getValue(),"Dozajištění");
    }

    public Function<CalculatorPage, CalculatorPage> fillClient(Client client) {
        return start -> start
                .then(fillBasicInformation(client))
                .then(fillIncomesFromEmployment(client))
                .then(fillExpenditures(client));
    }

    public Function<CalculatorPage, CalculatorPage> addAndFillClient(Client client) {
        return start -> start
                .clickAddApplicantButton()
                .then(fillClient(client));
    }

    public Function<CalculatorPage, CalculatorPage> fillLoan(String loanAmount) {
        return start -> start
                //Section loan
                .fillLoanAmount(loanAmount)
                .selectLoanPurpose("Koupě")
                .fillLoanSecurityAmount(FIRST_ROW.getValue(),"5000000")
                .clickLoanOwnerCheckbox();
    }

    public Function<CalculatorPage, CalculatorPage> changeHousehold(Client client) {
        return start -> start
                .clickClientTab(client)
                .clickHouseholdCheckbox(client);
    }

    private Function<CalculatorPage, CalculatorPage> fillBasicInformation(Client client) {
        String personalId = harness.tasks().common().getRcMinMaxAge(20, 30);
        harness.store(commonTasks.birthNumberForClient(client), personalId);
        return start -> start
                //Client 1 - Section basic information
                .selectSex("Pan", client)
                .fillFirstName(client.getValue(), harness.generate(GeneratorType.FIRST_NAME))
                .fillLastName(client.getValue(), harness.generate(GeneratorType.LAST_NAME))
                .fillBirthDate(client.getValue(),TestUtils.parseBirthDateAsString(personalId))
                .selectMaritalStatus(client.getValue(),"Svobodný(á)");
    }

    private Function<CalculatorPage, CalculatorPage> fillIncomesFromEmployment(Client client) {
        return start -> start
                //Client 1 - Section incomes from employment
                .fillGrossIncome(client.getValue(),"120000")
                .fillNetIncome(client.getValue(),"100000");
    }

    private Function<CalculatorPage, CalculatorPage> fillExpenditures(Client client) {
        return start -> start
                //Client 1 - Section expenditures
                .fillExpenditureInstalmentAmount(client.getValue(), FIRST_ROW.getValue(),"1000");
    }
}