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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.cards.credit.HPO039_S001_MbcDetailPageTransitions;
import cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage;
import cz.moneta.test.dsl.ufo.banka.pages.products.sme.KUL001_S001_SelectProductsPage;
import cz.moneta.test.dsl.ufo.banka.pages.products.sme.KUL003_S001_FlexiBusinessProductDetail;
import cz.moneta.test.dsl.ufo.banka.pages.products.sme.KUL066_S001_ProcessedRequestsPage;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.Lookup;

import java.util.function.Function;

public class ClipDeclipPoTasks {

    private final NewSmeAutomatSubTasks smeAutomatSubTasks;

    public ClipDeclipPoTasks(Harness harness) {
        this.smeAutomatSubTasks = harness.tasks().ufo().banka().newSmeAutomatSubTasks();
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> declipFlexi(Harness harness, String decreaseAmount) {
        return start -> start
                .clickKontokorentDetail()
                .mouseOverFrameChangeAndDecrease()
                .then(skipProcessedRequestsIfOpenedDeclipFlexi(harness))
                .fillRequiredLoanAmount(decreaseAmount)
                .clickLoadTurnovers()
                .clickContinue()
                .onEditBusinessInfoPage()
                .then(smeAutomatSubTasks.fillBusinessInformation("1"))
                .clickCompanyHeadquarterRadio()
                .clickContinue()
                .onListOfPersonsOnRequestPage()
                .then(smeAutomatSubTasks.selectStatutoryAuthority("200000,00"))
                .clickPermanentResidencyFopRadio()
                .clickContinue()
                .onListOfPersonsOnRequestPage()
                .clickContinue()
                .onRequestSummaryPage()
                .clickContinue()
                .onMainClientPage();
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> clipFlexi(Harness harness, String increaseAmount, String lastYear, String yearBeforeLast) {
        return start -> start
                .clickKontokorentDetail()
                .mouseOverFrameChangeAndIncrease()
                .then(skipProcessedRequestsIfOpenedClipFlexi(harness))
                .selectFlexiBusiness("Flexi Business")
                .fillRequiredLoanAmount(increaseAmount)
                .clickLoadTurnovers()
                .clickContinue()
                .onSelectProductsPage()
                .clickContinue()
                .then(smeAutomatSubTasks.sendDocumentationIfPageShownOtherwiseSkipPo(harness))
                .then(smeAutomatSubTasks.fillBusinessInformation("1"))
                .then(smeAutomatSubTasks.selectCompanyHeadquarter())
                .then(smeAutomatSubTasks.assignDocuments("Doklady PO", "Výpis z OR"))
                .clickContinue()
                .onFinancialSignpostPage()
                .then(smeAutomatSubTasks.fillFinancialSignpostDp(lastYear, yearBeforeLast))
                .continueToListOfPersonsOnRequestPage()
                .then(smeAutomatSubTasks.selectStatutoryAuthority("200000,00"))
                .then(smeAutomatSubTasks.selectPermanentResidencyForFop())
                .then(smeAutomatSubTasks.finalizeSmeForPo(harness))
                .then(smeAutomatSubTasks.uploadScannedDocuments(harness))
                .onMainClientPage();
    }

    /***
     * Stranka s rozpracovanymi ziadostami sa preskakuje v pripade, ze klient nejake rozpracovane ziadosti uz ma.
     */
    public Function<HPO039_S001_MbcDetailPageTransitions, KUL003_S001_FlexiBusinessProductDetail> skipProcessedRequestsIfOpenedDeclipFlexi(Harness harness) {
        if (harness.getEndpoint(UfoEndpoint.class).isElementVisible(2, KUL066_S001_ProcessedRequestsPage.KUL066_S001_PAGE_LABEL_XPATH, Lookup.XPATH)) {
            return start -> start
                    .onProcessedRequestsPage()
                    .clickContinue()
                    .onFlexiBusinessProductDetail();
        } else {
            return start -> start.onFlexiBusinessProductDetail();
        }
    }

    /***
     * Stranka s rozpracovanymi ziadostami sa preskakuje v pripade, ze klient nejake rozpracovane ziadosti uz ma.
     */
    public Function<HPO039_S001_MbcDetailPageTransitions, KUL001_S001_SelectProductsPage> skipProcessedRequestsIfOpenedClipFlexi(Harness harness) {
        if (harness.getEndpoint(UfoEndpoint.class).isElementVisible(2, KUL066_S001_ProcessedRequestsPage.KUL066_S001_PAGE_LABEL_XPATH, Lookup.XPATH)) {
            return start -> start
                    .onProcessedRequestsPage()
                    .clickContinue()
                    .onSelectProductsPage();
        } else {
            return start -> start.onSelectProductsPage();
        }
    }
}
