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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.brokerportal.contract.fullapplication.FullApplicationSignPage;
import cz.moneta.test.dsl.brokerportal.contract.fullapplication.FullApplicationPage;
import cz.moneta.test.dsl.brokerportal.contract.fullapplication.FullApplicationVictoryModal;
import cz.moneta.test.dsl.brokerportal.enums.AddressType;
import cz.moneta.test.dsl.brokerportal.enums.Client;
import cz.moneta.test.dsl.brokerportal.enums.Household;
import java.util.function.Function;

import static cz.moneta.test.dsl.brokerportal.enums.Client.FIRST_CLIENT;
import static cz.moneta.test.dsl.brokerportal.enums.Household.FIRST_HOUSEHOLD;

public class FullApplicationTasks {

    private final Harness harness;
    private final CommonTasks commonTasks;

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

    public Function<FullApplicationPage, FullApplicationSignPage> fillAndSaveFullApplication() {
        return start -> start
                .then(fillFullApplication())
                .clickGenerateFullApplication();
    }

    public Function<FullApplicationPage, FullApplicationPage> fillFullApplication() {
        return start -> start
                .then(fillApplicantData(FIRST_CLIENT, FIRST_HOUSEHOLD))
                .then(fillLoanAdditionalInfo());
    }

    public Function<FullApplicationPage, FullApplicationPage> fillApplicantData(Client client, Household household) {
        return start -> start
                .then(fillApplicantAdditionalInfo(client, household))
                .clickAddressSameCheckbox(client, household, AddressType.CONTACT_ADDRESS)
                .clickAddressSameCheckbox(client, household, AddressType.REAL_ADDRESS)
                .then(fillApplicantEmploymentInfo(client, household));
    }

    public Function<FullApplicationPage, FullApplicationPage> fillLoanAdditionalInfo() {
        return start -> start
                .selectDrawingAccount("Nový účet")
                .clickSmsNotifyWhenPaymentChangesCheckbox()
                .clickEmailNotifyWhenPaymentChangesCheckbox()
                .selectRealtyPurchaseReason("Vlastn")
                .selectLoanObject("Byt");
    }

    public Function<FullApplicationSignPage, FullApplicationVictoryModal> signFullApplication() {
        return start -> start
                .clickContinueToSignButton()
                .clickSendSmsButton()
                .fillSmsCode(commonTasks.getSmsCode())
                .clickSignFullApplicationButton();
    }

    private Function<FullApplicationPage, FullApplicationPage> fillApplicantAdditionalInfo(Client client, Household household) {
        if(client == FIRST_CLIENT && household == FIRST_HOUSEHOLD){
            return start -> start
                    .then(fillApplicantBasicAdditionalInfo(client, household));
        } else {
            return start -> start
                    .then(fillApplicantBasicAdditionalInfo(client, household))
                    .selectMainClientRelation("Manžel", client, household);
        }
    }

    private Function<FullApplicationPage, FullApplicationPage> fillApplicantBasicAdditionalInfo(Client client, Household household) {
        String passportCard = commonTasks.getPassportNumber();
        return start -> start
                .selectSecondIdentityType("Pas", client, household)
                .fillSecondIdentityValue(passportCard, client, household)
                .fillSecondIdentityExpirationDate("1.5.2030", client, household)
                .selectEducation("Vysoko", client, household)
                .clickPep(client, household)
                .selectCurrentLivingType("Vlastní byt", client, household);
    }

    private Function<FullApplicationPage, FullApplicationPage> fillApplicantEmploymentInfo(Client client, Household household) {
        return start -> start
                .searchEmployerManual(client, household)
                .fillEmployerName("MONETA Money Bank, a.s.", client, household)
                .fillEmployerIco("25672720", client, household)
                .fillEmployerStreet("Vyskočilova", client, household)
                .fillEmployerStreetNumber("1442", client, household)
                .fillEmployerNumberOrientational("1b", client, household)
                .fillEmployerZipCode("14000", client, household)
                .fillEmployerCity("Praha", client, household)
                .fillEmployerPhone("+420 224 446 646", client, household)
                .selectEmploymentType("Na dobu u", client, household)
                .fillEmploymentStartDate("5.8.2000", client, household)
                .fillEmploymentFinishDate("4.8.2030", client, household)
                .clickEmploymentIsFirst(client, household)
                .clickEmploymentSpecialRelation(client, household)
                .fillProfession("Tester", client, household)
                .clickEmploymentAbroad(client, household)
                .clickEmploymentAgency(client, household)
                .clickEmploymentPaidToMmb(client, household)
                .clickEmploymentCashSalary(client, household);
    }
}