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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.cards.debit.OBR003_S001_ClientContractSignPageTransitions;
import cz.moneta.test.dsl.ufo.banka.pages.common.pages.AML002_S001_AmlSurveyPage;
import cz.moneta.test.dsl.ufo.banka.pages.common.pages.AML002_S001_AmlSurveyPageTransitions;
import cz.moneta.test.dsl.ufo.banka.pages.fon.DMS070_S001_ScanDocumentsPage;
import cz.moneta.test.dsl.ufo.banka.pages.fon.GDP001_S001_ClientConsentsPage;
import cz.moneta.test.dsl.ufo.banka.pages.fop.CIF001_S003_CreateNewCompanyCifPage;
import cz.moneta.test.dsl.ufo.banka.pages.fop.CIF009_S001_CreateNewFoCifAsFopPartPage;
import cz.moneta.test.dsl.ufo.banka.pages.fop.CIF034_S001_CompanyAntiLaunderingStatement;
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.DateUtils;
import cz.moneta.test.dsl.util.data.client.NonClient;

import java.text.SimpleDateFormat;
import java.util.function.Function;

public final class NewFopClientTasks {

    private static final String dateFormat = "dd.MM.yyyy";
    private final Harness harness;

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

    public Function<CIF013_S002_ClientNotFoundPage, HKO101_S001_MainClientPage> createFopCif(NonClient nonClient) {
        return start -> start
                .clickCreateNewCompany()
                .then(fillNewFop(nonClient))
                .uploadAndAssignFakeId()
                .saveUploadedDocument()
                .clickContinue()
                .onPersonIdentificationInFundamentaRegisters()
                .then(harness.tasks().ufo().banka().newClient().fillFundamentalRegistersInfo(nonClient))
                .clickContinue()
                .clickLeftButton()
                .onPersonalDataFON()
                .then(harness.tasks().ufo().banka().newClient().fillPersonalDataFON(nonClient))
                .then(harness.tasks().ufo().banka().newClient().fillAdditionalIdentificationDocument(nonClient))
                .then(harness.tasks().ufo().banka().newClient().fillContactInformation(nonClient))
                .then(harness.tasks().ufo().banka().newClient().fillAdditionalInformationForScoring(nonClient))
                .clickContinue()
                .then(fillAmlSurvey())
                .then(finishAmlSurvey())
                .onAmlSurveyPage()
                .then(fillBankersAmlSurvey())
                .onScanDocumentsPage()
                .uploadAndAssignFakeZL()
                .clickContinue()
                .onClientConsentPage()
                .then(finishRestOfCif());
    }

    /**
     * BEWARE: next page could be either CIF011 (FO CIF exists) or CIF009 (otherwise)
     */
    public Function<CIF001_S003_CreateNewCompanyCifPage, DMS070_S001_ScanDocumentsPage> fillNewFop(NonClient nonClient) {
        return start -> start
                //TODO keep prefilled data
                .fillLegalFormCode("101")
                .selectLegalForm("FOP_Živnostník - fyzická osoba - tuzemec - nezapsaná v OR")
                //TODO keep prefilled data
                .fillOkecCode("239100")
                .selectOkecTitle("Výroba chladící vody")
                //TODO keep prefilled data
                .then(fillPersonalInformation(nonClient))
                .fillStartDate(DateUtils.getDateWithFormat(nonClient.occupationEmploymentFromRaw(), dateFormat))
                .fillEmail(nonClient.email())
                .fillMobilePhone(nonClient.mobileNumber())
                .selectEmployeeCount("žádný")
                .selectEconomySectorFop(25)
                .clickSubmit()
                .onIDDocumentsCopy()
                .then(harness.tasks().ufo().banka().newClient().selectDocumentIdCard("OP se strojově čitelnými údaji"))
                .onScanDocumentsPage();
    }

    public Function<CIF001_S003_CreateNewCompanyCifPage, CIF001_S003_CreateNewCompanyCifPage> fillPersonalInformation(NonClient nonClient) {
        return start -> start
                .fillCompanyName(nonClient.fullName())
                .fillAbbreviatedName(nonClient.fullName())
                .clickAddressLookupMagnifyGlass().fillAddressLookup("vyskocilova 1b, praha").clickDoAddressSearch()
                .clickFirstFoundAddress()
                .clickChooseAdress()
                .clickCzechCountryOfFoundation()
                .clickCzechCitizenship();
    }

    public Function<CIF009_S001_CreateNewFoCifAsFopPartPage, CIF034_S001_CompanyAntiLaunderingStatement> fillNewFo(NonClient nonClient) {
        return start -> start
                .fillPlaceOfBirth(nonClient.placeOfBirth())
                .clickBornInCzechRepublic()
                .clickCompanySeatSameAsResidency()
                .fillEmail(nonClient.email())
                .selectOccupation(nonClient.occupationType().getUfoValue())
                .then(fillFirstOtherId(nonClient))
                .then(fillPrivateInformation(nonClient))
                .submitCif();
    }

    public Function<CIF009_S001_CreateNewFoCifAsFopPartPage, CIF009_S001_CreateNewFoCifAsFopPartPage> fillFirstOtherId(NonClient nonClient) {
        return start -> start
                .selectFirstOtherId("ŘP")
                .fillFirstOtherIdNumber(nonClient.driverLicenceId())
                .fillFirstOtherIdValidity(new SimpleDateFormat(dateFormat).format(nonClient.driveLicenceExpiryDateRaw()))
                .clickFirstOtherIdIssuedByCzechAuthorities();
    }

    public Function<CIF009_S001_CreateNewFoCifAsFopPartPage, CIF009_S001_CreateNewFoCifAsFopPartPage> fillPrivateInformation(NonClient nonClient) {
        return start -> start
                .selectEducationLevel(nonClient.educationLevel().getUfoValue())
                .selectMaritalStatus(nonClient.maritalStatus().getUfoValue())
                .selectResidencyType(nonClient.accommodationType().getUfoValue())
                .fillCurrentResidencySince("1.1.2005");
    }

    public Function<GDP001_S001_ClientConsentsPage, HKO101_S001_MainClientPage> finishRestOfCif() {
        return start -> start
                .clickContinueToSignerSelectionPage()
                .clickSignedPersonallyAndContinueToPrint()
                .saveDocumentUrl()
                .closePrintWindow()
                .onCompanySignatureCifConfirmationPage()
                .clickContinue();
    }

    public Function<CIF034_S001_CompanyAntiLaunderingStatement, CIF034_S001_CompanyAntiLaunderingStatement> fillAntiLaunderingStatements() {
        return start -> start
                .chooseExpectedCashless()
                .selectMajorityCurrency("CZK")
                .chooseNoSendingAbroad()
                .chooseNoForeignReceive()
                .chooseLoansNotExpected()
                .chooseMmbMinority()
                .selectLastTurnover("1 - 199 999 Kč")
                .selectMmbTurnover("1 - 199 999 Kč");
    }

    public Function<AML002_S001_AmlSurveyPage, AML002_S001_AmlSurveyPageTransitions> fillAmlSurvey() {
        return start -> start
                .clickCzechRepublicTaxesResidency()
                .clickContinue()
                .onAmlSurveyPage()
                .clickNotAnotherTaxesResidency()
                .clickContinue()
                .onAmlSurveyPage()
                .clickIncomeFromBusiness()
                .clickContinue()
                .onAmlSurveyPage()
                .clickNotInSuspiciousMarkets()
                .clickContinue()
                .onAmlSurveyPage()
                .clickResidencyPlaceOfBusiness()
                .clickContinue()
                .onAmlSurveyPage()
                .clickNotPoliticallyExposedPerson()
                .clickContinue()
                .onAmlSurveyPage()
                .clickAccountInDifferentBankNo()
                .clickContinue()
                .onAmlSurveyPage()
                .clickCashPaymentPreference()
                .clickContinue()
                .onAmlSurveyPage()
                .clickMaxExpectedRevenue200K()
                .clickContinue()
                .onAmlSurveyPage()
                .clickDomesticPaymentsPresumption()
                .clickContinue();
    }

    public Function<AML002_S001_AmlSurveyPage, AML002_S001_AmlSurveyPageTransitions> fillBankersAmlSurvey() {
        return start -> start
                .clickPotentialClientNotWithThirdPerson()
                .clickContinue();
    }

    public Function<AML002_S001_AmlSurveyPageTransitions, OBR003_S001_ClientContractSignPageTransitions> finishAmlSurvey() {
        return start -> start
                .onPrintPage()
                .saveDocumentUrl()
                .closePrintWindow()
                .onClientContractSignPage()
                .clickCenterButton()
                .onClientContractSignPage()
                .clickLeftButton();
    }
}
