package cz.moneta.test.dsl.ufo.banka.tasks.insurance;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.fon.TIS050_S001_SignerSelectionPage;
import cz.moneta.test.dsl.ufo.banka.pages.insurance.IDD001_S001_IndividualInsuranceFormPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisProductClass;
import cz.moneta.test.dsl.ufo.banka.pages.search.HLO001_S001_ClientSearchPage;
import cz.moneta.test.dsl.util.DateUtils;
import cz.moneta.test.dsl.util.TestUtils;
import cz.moneta.test.dsl.util.data.client.NewClientWithCurrentAccountInformation;
import cz.moneta.test.dsl.util.data.client.NonClient;

import java.util.function.Function;

import static cz.moneta.test.dsl.util.credentials.Keys.ufo.banka.banker;
import static cz.moneta.test.dsl.util.task.retail.loans.constants.DefaultLoansConstants.*;
import static cz.moneta.test.harness.support.auth.AuthSupport.getCredentials;

public class InsuranceIndividualTasks {
    private final Harness harness;

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

    public NonClient createNonClient() {
        return harness.data().newNonClient()
                .withIdCard(DEFAULT_ID_CARD)
                .withBirthNumber(harness.tasks().common().getRcMinMaxAge(20, 50))
                .withResidencyAddress(DEFAULT_CLIENT_ADDRESS)
                .withEmail(DEFAULT_EMAIL)
                .withFixPhoneNumberWork(DEFAULT_MOBILE_NUMBER)
                .withFixPhoneNumberHome(DEFAULT_MOBILE_NUMBER)
                .withMobileNumber(DEFAULT_MOBILE_NUMBER)
                .withNetMonthlyIncome(DEFAULT_INCOME)
                .withHomeIncome(DEFAULT_INCOME);
    }

    public NonClient prepareClientViaWSO() {
        NonClient nonClient = createNonClient();
        String ufoUserName = getCredentials(banker, harness).getUsername();
        NewClientWithCurrentAccountInformation info = harness.dataPrepare().createNewFoCustomerWithCurrentAccountAndIbViaWso2(nonClient, ufoUserName);
        return info.getClientData();
    }

    public Function<HLO001_S001_ClientSearchPage, IDD001_S001_IndividualInsuranceFormPage> searchClientAndOpenIndividualInsuranceForm(NonClient client) {
        return page -> page
                .fillSearchTerm(client.birthNumber())
                .clickSearchByRc()
                .onClientDetailPage()
                .createNewProduct(HCisProductClass.INSURANCE)
                .onInsuranceSelectPage()
                .clickIndividualInsuranceButton();
    }

    public Function<IDD001_S001_IndividualInsuranceFormPage, TIS050_S001_SignerSelectionPage> fillInsuranceForm() {
        return page -> page
                .clickNewContract()
                .clickLifeInsurance()
                .clickMeAsInsured()
                .clickMeAsBeneficiary()
                .clickMaritalStatusSingle()
                .selectOccupation("programátor/-ka | Informační technologie")
                .selectActivity("Běh, překážky a nestandardní přírodní podmínky | registrovaný nebo neregistrovaný sportovec")
                .fillIncome("200000")
                .fillExpenses("2000")
                .clickSecuringLoansNo()
                .clickFinancialReserveSixMonths()
                .clickBetterTreatment()
                .clickNotMonthlyAnnuity()
                .clickDailyAnnuityNo()
                .clickPersonalDataAgreementYes()
                .clickNoRemoteBanker()
                .clickElectronicForm()
                .clickCalculateInsuranceRisk()
                .clickSaveInsuranceRisk()
                .clickContinueToSign();
    }

    public Function<TIS050_S001_SignerSelectionPage, IDD001_S001_IndividualInsuranceFormPage> signDocuments() {
        return page -> page.clickSignedPersonallyWithPossiblePhysicalSignature()
                .onPrintPage()
                .closePrintWindow()
                .onSignerPage()
                .clickRightButton()
                .onPrintPage()
                .closePrintWindow()
                .onSignerPage()
                .clickRightButton()
                .onInsurancePage();
    }

    public String formatBirthDate(String date) {
        return DateUtils.getDateWithFormat(TestUtils.dateFromString(date, "d.M.yyyy"), "dd.MM.yyyy");
    }
}
