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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.monetaportal.savings.childbuildingsavings.*;
import cz.moneta.test.dsl.ufo.operations.ufoOperations.UKO720_S001_TaskDetail;
import cz.moneta.test.dsl.ufo.operations.ufoOperations.UKO720_S001_TaskList;
import cz.moneta.test.dsl.util.data.client.NonClient;
import cz.moneta.test.dsl.util.task.retail.other.DocumentUploaderTasks;
import cz.moneta.test.dsl.util.task.ufo.operations.ManualFraudCheckTasks;
import cz.moneta.test.harness.endpoints.nasdb.NasDbEndpoint;
import cz.moneta.test.harness.support.auth.Credentials;

import java.util.function.Function;

import static cz.moneta.test.dsl.util.task.retail.loans.constants.DefaultLoansConstants.DEFAULT_FRAUD_MSG;

public class ChildBuildingSavingsTasks {
    private final Harness harness;
    private final ChildBuildingSavingsUtils utils;

    public ChildBuildingSavingsTasks(Harness harness) {
        this.harness = harness;
        this.utils = new ChildBuildingSavingsUtils(harness);
    }

    public Function<ContinueOnMobilePage, PermanentAddressPage> takeIdentity(NonClient childClient, boolean isLessThan15YearsOld, String parent1BirthNumber, String parent2BirthNumber) {
        utils.storeDocumentUploaderCode();
        DocumentUploaderTasks duTasks = harness.tasks().retail().documentUploaderTasks();
        if (isLessThan15YearsOld)
            duTasks.takeIdentityWithOnlyBirthCertificateBE(childClient, parent1BirthNumber, parent2BirthNumber, "15");
        else duTasks.takeIdentityWithIdAndBirthCertificateBE(childClient, parent1BirthNumber, parent2BirthNumber, "15");
        return ContinueOnMobilePage::onPermanentAddress;
    }

    public Function<ConsentsPage, ConsentsPage> logTestData(String clientCif, NonClient child, NonClient father, NonClient mother) {
        utils.storeUserIdAndSessionId(clientCif);
        harness.log("\nChild: " + child.fullName() + ", RČ: " + child.birthNumber() + "\n" +
                "Father: " + father.fullName() + ", RČ: " + father.birthNumber() + "\n" +
                "Mother: " + mother.fullName() + ", RČ: " + mother.birthNumber() + "\n" +
                "userId: " + harness.get("USERID") + "\n" +
                "clientId: " + harness.get("CLIENTID") + "\n" +
                "userLoginId: " + harness.get("USERLOGINID") + "\n" +
                "sessionId: " + harness.get("SESSIONID"));
        return page -> page;
    }

    public Function<CalculatorPage, ConsentsPage> fillCalculator(String payment, String term, boolean stateSupport) {
        return page -> page
                .setPayment(payment)
                .setTerm(term)
                //.setTargetAmount("150000")
                .setStateSupport(stateSupport)
                .clickContinue();
    }

    public Function<ConsentsPage, PersonalDetailPage> fillConsents(boolean isLessThan15YearsOld) {
        return page -> page
                .chooseAge(isLessThan15YearsOld)
                .clickAgreeAndContinue();
    }

    public Function<PersonalDetailPage, PersonalAddressPage> fillPersonalDetail() {
        return page -> page
                .clickPepNo()
                .clickContinue();
    }

    public Function<PersonalAddressPage, ContinueOnMobilePage> fillPersonalAddress() {
        return page -> page
                .setContactSameAsPermanent(true)
                .clickContinue();
    }

    public Function<PermanentAddressPage, AdditionalInformationPage> fillPermanentAddress(boolean isLessThan15YearsOld) {
        if (isLessThan15YearsOld) return page -> page
                //.setChildAddressSameAsParents(true)
                .clickContinue();
        else return PermanentAddressPage::onAdditionalInformation;
    }

    public Function<AdditionalInformationPage, LawFormPage> fillAdditionalInformation(String birthCountry) {
        return page -> page
                .clickPepNo()
                .setBirthCountry(birthCountry)
                .clickContinue();
    }

    public Function<LawFormPage, PreContractInformationPage> fillAml() {
        return page -> page
                .clickAnswers()
                .clickContinue();
    }

    public Function<PreContractInformationPage, ContractPage> fillPreContract() {
        return page -> page
                .waitToLoad()
                .clickAgreeAndContinue();
    }

    public Function<ContractPage, VictoryPage> fillContract() {
        return page -> page
                .scrollDown()
                .goSign()
                .typeSmsKey("12341234");
    }

    public Function<VictoryPage, VictoryPage> processManualFraudCheckTask(NonClient nonClient, boolean isLessThan15YearsOld, Credentials credentials) {
        NasDbEndpoint nasEP = harness.getEndpoint(NasDbEndpoint.class);
        if (!nasEP.executeSql(String.format(ChildBuildingSavingsSQL.SQL_SELECT_WAITING_FOR_DOCUMENT_REQUEST, nonClient.birthNumber())).isEmpty()) {
            ManualFraudCheckTasks mfchTasks = harness.tasks().ufo().operations().manualFraudCheckTasks();
            UKO720_S001_TaskList taskList = harness.withUfo()
                    .openLoginPage()
                    .typeUserNameAndPasswordAndClickLogin(credentials.getUsername(), credentials.getPassword())
                    .onApplicationSelectPage()
                    .selectFirstOmIfPresent()
                    .selectUfoOperationsAndContinueWithoutOmSelect()
                    .then(mfchTasks.findFraudTaskByBirthNumberWithTimeout(nonClient.birthNumber()));
            UKO720_S001_TaskDetail taskDetail;
            if (isLessThan15YearsOld)
                taskDetail = taskList.then(mfchTasks.selectFirstMFCHBirthCertificateOnly());
            else taskDetail = taskList.then(mfchTasks.selectFirstMFCHBirthCertificateAndId(nonClient.idCardCheckDigit()));
            taskDetail.then(mfchTasks.finishFraudTask(DEFAULT_FRAUD_MSG));
        } else harness.log("CIF request has been automatically approved.");
        return page -> page;
    }
}
