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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.fon.CIF111_S016_CreateNewCifPage;
import cz.moneta.test.dsl.ufo.banka.pages.fon.TIS050_S001_SignerSelectionPage;
import cz.moneta.test.dsl.ufo.banka.pages.fon.newCif.CIF111_S001_NewCifDataNavigation;
import cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage;
import cz.moneta.test.dsl.ufo.banka.pages.search.CIF013_S002_ClientNotFoundPage;
import cz.moneta.test.dsl.util.data.client.NonClient;

import java.util.function.Function;

public class CreateStatutoryAuthority {

    private final Harness harness;

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

    public Function<CIF013_S002_ClientNotFoundPage, HKO101_S001_MainClientPage> createStatutoryAuthority(NonClient nonPoClient) {
        return start -> start
                .clickCreateNewCustomer()
                .then(harness.tasks().ufo().banka().newClient().selectDocumentIdCard("OP se strojově čitelnými údaji"))
                .onCreateNewCifpage()
                .then(fillFonBasicData(nonPoClient))
                .then(fillStatutoryAuthority(nonPoClient))
                .clickSignedAndContinueToPrintPage()
                .saveDocumentUrl()
                .closePrintWindow()
                .onPrintInstructionPage()
                .clickRightButton()
                .onDocumentScanPage()
                .clickSkipButton()
                .onClientContractSignPage()
                .clickLeftButton()
                .onClientContractSignPage()
                .clickCenterButton()
                .onMainClientPage();
    }

    public Function<CIF111_S016_CreateNewCifPage, CIF111_S001_NewCifDataNavigation> fillFonBasicData(NonClient nonPoClient) {
        return start -> start
                .fillFirstAndLastNames(nonPoClient.firstName(), nonPoClient.lastName())
                .selectIdType("Občanský průkaz")
                .fillIdCardNumber(nonPoClient.idNumber())
                .clickContinue()
                .then(harness.tasks().ufo().banka().newClient().skipNotRegisteredWarningIfVisible())
                .tryCloseIdentificationPopup();
    }

    public Function<CIF111_S001_NewCifDataNavigation, TIS050_S001_SignerSelectionPage> fillStatutoryAuthorityData(NonClient nonPoClient) {
        return start -> start
                .then(fillPersonalData(nonPoClient))
                .then(fillResidencyInfo(nonPoClient))
                .then(fillClientContacts(nonPoClient))
                .then(fillIdentificationDocuments(nonPoClient))
                .then(fillEmployerDataWithClientData(nonPoClient))
                .then(fillAdditionalInformation(nonPoClient))
                .clickConsents()
                .clickContinue()
                .onAmlSurveyPage()
                .then(harness.tasks().ufo().banka().newClient().fillAmlSurveyFon())
                .then(harness.tasks().ufo().banka().newClient().finishAmlSurveyFon())
                .onAmlSurveyPage()
                .then(harness.tasks().ufo().banka().newClient().fillBankersAmlSurveyFon())
                .onSignerSelectionPage();
    }

    public Function<CIF111_S001_NewCifDataNavigation, TIS050_S001_SignerSelectionPage> fillStatutoryAuthority(NonClient nonPoClient) {
        return start -> start
                .then(fillPersonalData(nonPoClient))
                .then(fillResidencyInfo(nonPoClient))
                .then(fillClientContacts(nonPoClient))
                .then(fillIdentificationDocuments(nonPoClient))
                .then(fillAdditionalInformation(nonPoClient))
                .clickConsents()
                .clickContinue()
                .onAmlSurveyPage()
                .then(harness.tasks().ufo().banka().newClient().fillAmlSurveyFon())
                .then(harness.tasks().ufo().banka().newClient().finishAmlSurveyFon())
                .onAmlSurveyPage()
                .then(harness.tasks().ufo().banka().newClient().fillBankersAmlSurveyFon())
                .onSignerSelectionPage();
    }

    public Function<CIF111_S001_NewCifDataNavigation, CIF111_S001_NewCifDataNavigation> fillPersonalData(NonClient nonPoClient) {
        return start -> start
                .tryCloseIdentificationPopup()
                .fillPersonalIdentificationData()
                .fillPlaceOfBirth(nonPoClient.residencyAddress().getCity())
                .clickBornInCzechButton()
                .selectToEducationLevel(nonPoClient.educationLevel().getUfoValue());
    }

    public Function<CIF111_S001_NewCifDataNavigation, CIF111_S001_NewCifDataNavigation> fillResidencyInfo(NonClient nonPoClient) {
        return start -> start
                .fillResidency()
                .fillStreet(nonPoClient.residencyAddress().getStreet())
                .fillStreetNumber(nonPoClient.residencyAddress().getDescriptiveNumber())
                .clickMagnifyGlassButton()
                .clickFirstFoundAddress()
                .clickResidencyInCzechRepublic();
    }

    public Function<CIF111_S001_NewCifDataNavigation, CIF111_S001_NewCifDataNavigation> fillClientContacts(NonClient nonPoClient) {
        return start -> start
                .fillContacts()
                .fillEmail(nonPoClient.email())
                .fillMobilePhone(nonPoClient.mobileNumber())
                .fillMobilePhonePrefix("420");
    }

    public Function<CIF111_S001_NewCifDataNavigation, CIF111_S001_NewCifDataNavigation> fillIdentificationDocuments(NonClient nonPoClient) {
        return start -> start
                .fillIdentificationDocuments()
                .fillFirstIdValidity(nonPoClient.idNumberExpiration())
                .fillControlNumber(nonPoClient.idNumberCheckDigit())
                .clickAddSecondId()
                .selectSecondId("Řidičský průkaz")
                .fillSecondIdNumber(nonPoClient.driverLicenceId())
                .fillSecondIdValidity(nonPoClient.drivingLicenceExpiration());
    }

    public Function<CIF111_S001_NewCifDataNavigation, CIF111_S001_NewCifDataNavigation> fillEmployerDataWithClientData(NonClient nonClient) {
        return start -> start
                .fillEmployment()
                .selectIncomeType(nonClient.incomeType().getUfoValue())
                .selectIncomeSubtype(nonClient.incomeSubtype().getUfoValue())
                .selectEmployment(nonClient.occupationType().getUfoValue());
    }

    public Function<CIF111_S001_NewCifDataNavigation, CIF111_S001_NewCifDataNavigation> fillAdditionalInformation(NonClient nonPoClient) {
        return start -> start
                .fillAdditionalInformation()
                .selectMaritalOfStatus(nonPoClient.maritalStatus().getUfoValue())
                .selectAccommodationType(nonPoClient.accommodationType().getUfoValue())
                .fillResidencySince(nonPoClient.currentAddressSinceYear().toString())
                .fillDependants(String.valueOf(nonPoClient.dependants()));
    }
}
