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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.brokerportal.contract.VictoryScreenPage;
import cz.moneta.test.dsl.brokerportal.contract.shortapplication.ShortApplicationPage;
import cz.moneta.test.dsl.brokerportal.contract.shortapplication.ShortApplicationSignPage;
import cz.moneta.test.dsl.brokerportal.contract.shortapplication.ShortApplicationSummaryPage;
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 ShortApplicationTasks {

    private final Harness harness;
    private final CommonTasks commonTasks;

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

    public Function<ShortApplicationPage, ShortApplicationSummaryPage> fillAndConfirmShortApplication() {
        return start -> start
                .then(fillShortApplication(FIRST_CLIENT, FIRST_HOUSEHOLD))
                .clickApplicationConfirmButton();
    }

    public Function<ShortApplicationPage, ShortApplicationPage> fillShortApplication(Client client, Household household) {
        return start -> start
                .then(fillShortApplicationWithoutEmail(client, household))
                .fillEmail("karel.hruza1@moneta.cz", client, household);
    }

    public Function<ShortApplicationPage, ShortApplicationPage> fillShortApplicationWithoutEmail(Client client, Household household) {
        return start -> start
                .fillPersonalId(harness.get(commonTasks.birthNumberForClient(client)), client, household)
                .fillFirstIdentityValue(commonTasks.getIdentityCard(client.getValue()), client, household)
                .fillFirstIdentityExpirationDate("2.5.2030", client, household)
                .fillPhone(commonTasks.getMobilePhoneNumber(), client, household)
                .then(checkIdentity(client, household));
    }

    public Function <ShortApplicationPage, ShortApplicationPage> checkIdentity(Client client, Household household) {
        return start -> start
                .clickCheckIdentityCheckbox(client, household)
                .clickCheckIdentityButton(client, household);
    }

    public Function<ShortApplicationSummaryPage, ShortApplicationSummaryPage> checkShortApplicationSummary() {
        return start -> start
                .clickSummaryApplicantsConfirmButton()
                .then(confirmApplicantAgreements(FIRST_CLIENT))
                .clickApplicantsAgreementsConfirmButton()
                .then(confirmSummaryAgreements());
    }

    public Function<ShortApplicationSummaryPage, ShortApplicationSummaryPage> confirmApplicantAgreements(Client client) {
        return start -> start
                .clickAgreementCommunicationCheckbox(client)
                .clickAgreementMarketingCheckbox(client);
    }

    public Function<ShortApplicationSummaryPage, ShortApplicationSummaryPage> confirmSummaryAgreements() {
        return start -> start
                .clickAgreementTruDataCheckbox()
                .clickAgreementRegisterCheckCheckbox();
    }

    public Function<ShortApplicationSignPage, VictoryScreenPage> signShortApplication() {
        return start -> start
                .clickContinueToSignButton()
                .clickSendSmsButton()
                .clickSendNewSmsButton()
                .fillSmsCode(commonTasks.getSmsCode())
                .clickSignShortApplicationButton();
    }
}