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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.cards.debit.*;
import cz.moneta.test.dsl.ufo.banka.pages.fon.DMS070_S001_ScanDocumentsPage;
import cz.moneta.test.dsl.ufo.banka.pages.fon.ICO001_S001_BusinessControlPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPageTransitions;
import cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.PSP004_S001_PrintPageTransitions;
import cz.moneta.test.dsl.ufo.banka.pages.main.client.settings.ADR004_S001_EnterContactAddressPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisProductClass;
import cz.moneta.test.dsl.util.data.client.EducationLevel;
import cz.moneta.test.dsl.util.data.client.MaritalStatus;
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 class RetailHunterTasks {

    private Harness harness;

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

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> createRetailHunter(NonClient nonClient, String businessIco) {
        return start -> start
                .createNewProduct(HCisProductClass.MONETA_BUSINESS_CARD)
                .then(fillContactAddressOtherwiseSkip())
                .then(fillBusinessInformation(businessIco))
                .clickAgreeToSettingMbcPage()
                .then(setMbc(nonClient))
                .then(fillComplementaryBusinessData())
                .then(scanDocuments())
                .then(fillDataFromIdCard(nonClient))
                .then(fillPersonalContactData(nonClient))
                .clickPrintRequest()
                .saveDocumentUrl()
                .closePrintWindow()
                .onContractSignPage()
                .clickRightButton()
                .onWaitingForModulationPage()
                .waitForModulation(180)
                .onMbcSummaryPage()
                .clickContinueToModulation()
                .waitForModulation(260)
                .onSignerSelectionPage()
                .chooseCitizen()
                .clickContinue()
                .onPrintPage()
                .saveDocumentUrl()
                .closePrintWindow()
                .then(printDocumentation())
                .onContractSignPage()
                .clickRightButton()
                .onWaitingForModulationPage()
                .waitForModulation(100)
                .onClientContractSignPage()
                .clickLeftButton()
                .onMainClientPage();
    }

    public Function<HKO101_S001_MainClientPageTransitions, OBR003_S001_ClientContractSignPage> fillContactAddressOtherwiseSkip() {
        if (harness.getEndpoint(UfoEndpoint.class).isElementVisible(3, ADR004_S001_EnterContactAddressPage.ADR004_S001_PAGE_LABEL_XPATH, Lookup.XPATH)) {
            return start -> start
                    .onEnterContactAddressPageAndDismissAlertIfVisible()
                    .clickUseResidencyAsContactAndConfirm()
                    .clickSave()
                    .onClientContractSignPage();
        } else {
            return start -> start
                    .onClientContractSignPage();
        }
    }

    public Function<PSP004_S001_PrintPageTransitions, PSP004_S001_PrintPageTransitions> printDocumentation() {
        return start -> start
                .then(printing())
                .then(printing())
                .then(printing())
                .then(printing());
    }

    public Function<PSP004_S001_PrintPageTransitions, PSP004_S001_PrintPageTransitions> printing() {
        return start -> start
                .onPrintPage()
                .saveDocumentUrl()
                .closePrintWindow();
    }

    public Function<OBR003_S001_ClientContractSignPage, ICO001_S001_BusinessControlPage> fillBusinessInformation(String businessIco) {
        return start -> start
                .fillIco(businessIco)
                .clickCenterButton()
                .onBusinessControlPage();
    }

    public Function<MBC002_S001_SettingMbcPage, MBC004_S001_ComplementaryBusinessDataPage> setMbc(NonClient nonClient) {
        return start -> start
                .fillCreditLimitAmount("50000")
                .selectRepaymentType("Inkasem z účtu")
                .checkAccountProBusiness()
                .clickContinueToBasicBusinessDataPage()
                .fillBusinessName(nonClient.fullName())
                .fillDic("")
                .then(fillCityIntoStreetIfInputIsEmpty())
                .checkConsents()
                .clickContinueToComplementaryBusinessDataPage();
    }

    public Function<MBC003_S001_BasicBusinessDataPage, MBC003_S001_BasicBusinessDataPage> fillCityIntoStreetIfInputIsEmpty() {
        if (harness.getEndpoint(UfoEndpoint.class).getAttribute(MBC003_S001_BasicBusinessDataPage.STREET_INPUT_XPATH, Lookup.XPATH, "value").equals("")) {
            String businessCity = harness.getEndpoint(UfoEndpoint.class).getAttribute(MBC003_S001_BasicBusinessDataPage.CITY_INPUT_XPATH, Lookup.XPATH, "value");
            return start -> start
                    .fillBusinessStreet(businessCity);
        } else {
            return null;
        }
    }

    public Function<MBC004_S001_ComplementaryBusinessDataPage, DMS070_S001_ScanDocumentsPage> fillComplementaryBusinessData() {
        return start -> start
                .clickCzechTaxResidencyOnly()
                .clickNoEstablishmentAbroad()
                .clickNoSilentCompanion()
                .fillApproximateTurnover("500000")
                .fillNettoIncome("400000")
                .fillHouseholdNettoIncome("50000")
                .fillNumberOfEmployees("0")
                .clickCashlessDomesticTransactions()
                .clickNoPep()
                .clickContinueToScanDocumentsPage();
    }

    public Function<DMS070_S001_ScanDocumentsPage, MBC005_S001_IdCardDataPage> scanDocuments() {
        return start -> start
                .uploadAndAssignFakeId()
                .saveUploadedDocument()
                .clickContinue()
                .onIdCardDataPage();
    }

    public Function<MBC005_S001_IdCardDataPage, MBC006_S001_PersonalContactDataPage> fillDataFromIdCard(NonClient nonClient) {
        return start -> start
                .fillFirstName(nonClient.firstName())
                .fillLastName(nonClient.lastName())
                .fillBirthDate(nonClient.birthDate())
                .fillBirthPlace(nonClient.placeOfBirth())
                .clickCzechBirthCountry()
                .fillIdValidTill(nonClient.idNumberExpiration())
                .fillIdNumber(nonClient.idNumber())
                .fillBirthNumber(nonClient.birthNumber())
                .fillIdControlNumber(nonClient.idNumberCheckDigit())
                .clickMagnifyingGlass()
                .typeAddress(nonClient.residencyAddress().getStreet() + " " + nonClient.residencyAddress().getDescriptiveNumber())
                .clickSearchForAddress()
                .clickFirstFoundAddressAndConfirm()
                .clickCzechFlagPermanentResidency()
                .clickMaleGender()
                .continueToPersonalAndContactDataPage();
    }

    public Function<MBC006_S001_PersonalContactDataPage, MBC007_S001_PersonalDataSummaryPage> fillPersonalContactData(NonClient nonClient) {
        return start -> start
                .clickMagnifyingGlass()
                .typeAddress(nonClient.residencyAddress().getStreet() + " " + nonClient.residencyAddress().getDescriptiveNumber())
                .clickSearchForAddress()
                .clickFirstFoundAddressAndConfirm()
                .clickCzechResidency()
                .fillEmail(nonClient.email())
                .fillPhone("420" + nonClient.mobileNumber())
                .selectEducationLevel(EducationLevel.HIGHER.getUfoValue())
                .selectMaritalStatus(MaritalStatus.SINGLE.getUfoValue())
                .checkConsents()
                .clickContinueToMbcSummaryPage();
    }
}
