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

import cz.moneta.test.dsl.Harness;
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.CRS001_S001_CommonReportingStatusPage;
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.CIF034_S001_CompanyAntiLaunderingStatement;
import cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.client.settings.ODS005_S001_FoEditingPage;
import cz.moneta.test.dsl.ufo.banka.pages.po.*;
import cz.moneta.test.dsl.ufo.banka.pages.search.CIF013_S002_ClientNotFoundPage;
import cz.moneta.test.dsl.util.data.client.NonClient;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.Lookup;

import java.util.function.Function;

public final class NewPoClientTasks {

    private final Harness harness;

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

    public Function<CIF013_S002_ClientNotFoundPage, HKO101_S001_MainClientPage> createPoCif(NonClient nonPoClient) {
        return start -> start
                .clickCreateNewCompany()
                .then(fillNewPoFop(nonPoClient))
                .then(createNewStatutoryAuthority(nonPoClient))
                .then(editFoForPo(nonPoClient))
                .clickContinue()
                .onCompaniesOwnedByClientPage()
                .then(addCompanyOwnedByClient(nonPoClient))
                .clickContinue()
                .then(addOwner(nonPoClient))
                .clickDataSameAsEsmYes()
                .clickContinue()
                .clickContinue()
                .then(fillAmlSurvey())
                .then(finishAmlSurvey())
                .onScanDocumentsPage()
                .clickSkipButton()
                .onClientConsentPage()
                .then(selectGdpConsentsAndPrint());
    }

    /***
     * Pri tvorbe PO sa test dostane na výpis skutečných majitelů CIF043_S001 na prostredí PPE.
     * Na prostredí TST1 sa táto stránka nezobrazuje.
     */
    public Function<ZAS001_S001_StatutoryAuthorityPageTransitions, CIF034_S001_CompanyAntiLaunderingStatement> fillActualOwnerIfPageShown(NonClient nonPoClient) {
        if (harness.getEndpoint(UfoEndpoint.class).isElementVisible(3, CIF034_S001_CompanyAntiLaunderingStatement.CIF034_S001_PAGE_LABEL_XPATH, Lookup.XPATH)) {
            return start -> start
                    .onAntiLaunderingStatementPage();
        } else {
            return start -> start
                    .onActualOwnerPage()
                    .then(fillActualOwnerInfo(nonPoClient));
        }
    }

    public Function<CIF001_S003_CreateNewCompanyCifPage, CIF040_S001_StatutoryAuthorityPage> fillNewPoFop(NonClient nonPoClient) {
        return start -> start
                .fillCompanyName(nonPoClient.employer().getName())
                .fillAbbreviatedName("Ministerstvo autom")
                .fillStartDate("12.10.2015")
                .clickAddressLookupMagnifyGlass()
                .fillAddressLookup(nonPoClient.residencyAddress().getStreet() + " " + nonPoClient.residencyAddress().getDescriptiveNumber())
                .clickDoAddressSearch()
                .clickFirstFoundAddress()
                .clickChooseAdress()
                .selectCourtSeat("MS v Praze")
                .fillCourtDepartment("C")
                .fillCourtInsert("19010")
                .clickCzechCountryOfEstablishment()
                .clickCzechCountryOfFoundation()
                .fillEmail(nonPoClient.email())
                .fillMobilePhone(nonPoClient.mobileNumber())
                .selectLegalForm("PO_Společnost s ručením omezeným")
                .selectOkecTitle("Maloobchod v nespecializovaných prodejnách")
                .selectEconomySectorPo("Nefinanční podniky soukromé národní ")
                .selectEmployeeCount("1 až 10")
                .clickSubmit()
                .onStatutoryAuthorityPage();
    }

    public Function<CIF040_S001_StatutoryAuthorityPage, ODS005_S001_FoEditingPage> createNewStatutoryAuthority(NonClient nonPoClient) {
        return start -> start
                .addStatutoryAuthority()
                .selectTypeOfNewAuthority("je jednatelem")
                .fillFirstName(nonPoClient.firstName())
                .fillLastName(nonPoClient.lastName())
                .fillBirthDate(nonPoClient.birthDate())
                .selectCzechResidencyOfNewAuthority()
                .clickSaveNewAuthority()
                .continueToSelectStatutoryAuthority()
                .selectFirstStatutoryAuthority()
                .continueToFoEditingPage();
    }

    public Function<ODS005_S001_FoEditingPage, ZAS001_S001_StatutoryAuthorityPage> editFoForPo(NonClient nonPoClient) {
        return start -> start
                .fillBirthNumber(nonPoClient.birthNumber())
                .clickGenderMaleRadio()
                .fillPlaceOfBirth(nonPoClient.placeOfBirth())
                .clickCzechCountryOfBirthButton()
                .clickCzechResidencyButton()
                .fillStreet(nonPoClient.residencyAddress().getStreet())
                .fillDescriptiveNumber(nonPoClient.residencyAddress().getDescriptiveNumber())
                .clickCzechCountryAddress()
                .clickAddressLookupMagnifyGlass()
                .clickDoAddressSearch()
                .clickFirstFoundAddress()
                .fillMobilePhoneCode("420")
                .fillMobilePhone(nonPoClient.mobileNumber())
                .selectIdType("Občanský průkaz")
                .fillIdNumber(nonPoClient.idNumber())
                .fillIdValidTill(nonPoClient.idNumberExpiration())
                .clickCzechCountryIdReleaseButton()
                .clickPersonPhysicallyOnsite("Ano")
                .chooseNotPoliticallyExposedPerson()
                .clickContinue()
                .then(harness.tasks().ufo().banka().newClient().selectDocumentIdCard("OP se strojově čitelnými údaji"))
                .onScanDocumentsPage()
                .uploadAndAssignFakeId()
                .saveUploadedDocument()
                .clickContinue()
                .onStatutoryAuthorityPage();
    }

    public Function<CIF043_S001_ActualOwnerPage, CIF034_S001_CompanyAntiLaunderingStatement> fillActualOwnerInfo(NonClient nonPoClient) {
        return start -> start
                .clickToAddNewActualOwner()
                .fillFirstName(nonPoClient.firstName())
                .fillLastName(nonPoClient.lastName())
                .selectCzechResidency()
                .selectCzechPermanentResidency()
                .fillBirthNumber(nonPoClient.birthDate())
                .fillConcurrentShare("100")
                .fillStreet(nonPoClient.residencyAddress().getStreet())
                .fillDescriptiveNumber(nonPoClient.residencyAddress().getDescriptiveNumber())
                .fillCity(nonPoClient.residencyAddress().getCity())
                .fillZipCode(nonPoClient.residencyAddress().getZip())
                .saveNewActualOwner()
                .scrollIntoContinueButton()
                .continueToRealOwners()
                .clickToAddNewActualOwner()
                .fillFirstName(nonPoClient.firstName())
                .fillLastName(nonPoClient.lastName())
                .selectCzechResidency()
                .selectCzechPermanentResidency()
                .fillBirthNumber(nonPoClient.birthDate())
                .fillConcurrentShare("100")
                .saveNewActualOwner()
                .scrollIntoContinueButton()
                .continueToScoringPage()
                .waitForLoader()
                .onAntiLaunderingStatementPage();
    }

    public Function<CIF034_S001_CompanyAntiLaunderingStatement, CRS001_S001_CommonReportingStatusPage> selectAntiLaunderingStatements() {
        return start -> start
                .chooseExpectedCashless()
                .selectMajorityCurrency("CZK")
                .chooseNoSendingAbroad()
                .chooseNoForeignReceive()
                .chooseLoansExpected()
                .chooseMmbMajority()
                .selectLastTurnover("1 000 000 - 2 999 999 Kč")
                .selectMmbTurnover("1 000 000 - 2 999 999 Kč")
                .submitStatement();
    }

    public Function<GDP001_S001_ClientConsentsPage, HKO101_S001_MainClientPage> selectGdpConsentsAndPrint() {
        return start -> start
                .clickOnPoConsentsAndContinueToSignerSelectionPoPage()
                .clickSignedByPoAndClickOk()
                .clickFirstStatutoryRepresentativeAndContinue()
                .saveDocumentUrl()
                .closePrintWindow()
                .onCompanySignatureCifConfirmationPage()
                .clickContinue();
    }

    public Function<CIF081_S001_CompaniesOwnedByClientPage, CIF081_S001_CompaniesOwnedByClientPage> addCompanyOwnedByClient(NonClient nonPoClient) {
        return start -> start
                .clickAddPo()
                .fillPoTitle(nonPoClient.employer().getName())
                .fillPoIco(nonPoClient.ico())
                .clickPoCountryCz()
                .clickPoSave();
    }

    public Function<CIF080_S001_OwnershipStructurePage, CIF080_S001_OwnershipStructurePage> addOwner(NonClient nonPoClient) {
        return start -> start
                .clickAddOwner()
                .fillFirstName(nonPoClient.firstName())
                .fillLastName(nonPoClient.lastName())
                .fillOwnerDateOfBirth(nonPoClient.birthDate())
                .clickOwnerNationalityCz()
                .clickOwnerPermanentCz()
                .selectOwnerType("SM dle § 4 ZESM (materiální)")
                .fillOwnerShare("100")
                .clickOwnerSave();
    }

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

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

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