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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.cards.debit.OBR003_S001_ClientContractSignPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage;
import cz.moneta.test.dsl.ufo.banka.pages.search.CIF013_OBR001_EmployeeClientPage;
import cz.moneta.test.dsl.ufo.banka.pages.search.CIF013_S002_ClientNotFoundPage;
import cz.moneta.test.dsl.ufo.banka.pages.search.HLO001_S001_ClientSearchPage;
import cz.moneta.test.dsl.ufo.banka.pages.search.HLO001_S001_ClientSearchPageTransition;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.Lookup;

import java.util.function.Function;

public class SearchTasks {

    private final Harness harness;

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

    public Function<HLO001_S001_ClientSearchPage, HKO101_S001_MainClientPage> findByIco(String ico) {
        return start -> start
                .closeSearchedHistoryIfVisible()
                .fillSearchTerm(ico)
                .searchByIco()
                .onClientDetailPage();
    }

    public Function<HLO001_S001_ClientSearchPage, HKO101_S001_MainClientPage> searchByRc(String rc) {
        return start -> start
                .closeSearchedHistoryIfVisible()
                .fillSearchTerm(rc)
                .clickSearchByRc()
                .onClientDetailPage();
    }

    public Function<HLO001_S001_ClientSearchPage, HKO101_S001_MainClientPage> findByStoredCif() {
        return start -> start.fillSearchTerm(harness.get(HKO101_S001_MainClientPage.CLIENT_CIF_STORE_KEY))
                .clickSearchByCif()
                .onClientDetailPage();
    }

    public Function<HLO001_S001_ClientSearchPage, HKO101_S001_MainClientPage> findByCif(String cif) {
        return start -> start
                .closeSearchedHistoryIfVisible()
                .fillSearchTerm(cif)
                .clickSearchByCif()
                .then(skipCifEditationIfVisible());
    }

    public Function<HLO001_S001_ClientSearchPageTransition, HKO101_S001_MainClientPage> skipCifEditationIfVisible() {
        if (harness.getEndpoint(UfoEndpoint.class).isElementVisible(OBR003_S001_ClientContractSignPage.PAGE_LABEL_XPATH, Lookup.XPATH)) {
            return start -> start
                    .onOpFoundPage()
                    .clickLeftButton()
                    .onMainClientPage();
        } else {
            return start -> start.onClientDetailPage();
        }
    }

    public Function<HLO001_S001_ClientSearchPage, HKO101_S001_MainClientPage> searchByCif(String cif) {
        return start -> start
                .closeSearchedHistoryIfVisible()
                .fillSearchTerm(cif)
                .clickSearchByCif()
                .onClientDetailPage();
    }

    public Function<HLO001_S001_ClientSearchPage, CIF013_OBR001_EmployeeClientPage> findEmployeeByRC(String rc) {
        return start -> start.fillSearchTerm(rc)
                .clickSearchByRc()
                .onEmployeeClientPage();
    }

    public Function<HLO001_S001_ClientSearchPage, HKO101_S001_MainClientPage> findByRC(String rc) {
        return start -> start
                .closeSearchedHistoryIfVisible()
                .fillSearchTerm(rc)
                .clickSearchByRc()
                .onClientDetailPage();
    }

    public Function<HLO001_S001_ClientSearchPage, HKO101_S001_MainClientPage> findChildByRC(String rc) {
        return start -> start
                .closeSearchedHistoryIfVisible()
                .fillSearchTerm(rc)
                .clickSearchByRc()
                .onChooseLegalRepresentative()
                .clickFirstRepresentative()
                .clickContinueToMainClientPageAndCloseAlertIfVisible()
                .onMainClientPage();
    }

    public Function<HLO001_S001_ClientSearchPage, HKO101_S001_MainClientPage> findByIcoWithAlert(String ico) {
        return start -> start
                .closeSearchedHistoryIfVisible()
                .fillSearchTerm(ico)
                .searchByIco()
                .onClientDetailPageCloseAlertIfVisible();
    }

    public Function<HLO001_S001_ClientSearchPage, HKO101_S001_MainClientPage> findByRcWithAlert(String rc) {
        return start -> start
                .closeSearchedHistoryIfVisible()
                .fillSearchTerm(rc)
                .clickSearchByRc()
                .onClientDetailPageCloseAlertIfVisible();
    }

    public Function<HLO001_S001_ClientSearchPage, HKO101_S001_MainClientPage> findByIcoPo(String ico) {
        return start -> start
                .closeSearchedHistoryIfVisible()
                .fillSearchTerm(ico)
                .searchByIco()
                .onChooseLegalRepresentative()
                .clickFirstRepresentative()
                .clickContinueToMainClientPageAndCloseAlertIfVisible()
                .onMainClientPage();
    }

    public Function<HLO001_S001_ClientSearchPage, HKO101_S001_MainClientPage> findLegalRepresentativeByIco(String ico) {
        return start -> start
                .closeSearchedHistoryIfVisible()
                .fillSearchTerm(ico)
                .clickIcoRadioAndContinueToStatutoryAuthorityEvidencePage()
                .clickFirstStatutoryRepresentativeAndContinueToMainPage();
    }

    public Function<HLO001_S001_ClientSearchPage, HKO101_S001_MainClientPage> findTwoLegalRepresentativesByIco(String ico) {
        return start -> start
                .closeSearchedHistoryIfVisible()
                .fillSearchTerm(ico)
                .clickIcoRadioAndContinueToStatutoryAuthorityEvidencePage()
                .clickSecondStatutoryRepresentative()
                .clickFirstStatutoryRepresentativeAndContinueToMainPage();
    }

    public Function<HLO001_S001_ClientSearchPage, CIF013_S002_ClientNotFoundPage> findByNewIco(String ico) {
        return start -> start
                .closeSearchedHistoryIfVisible()
                .fillSearchTerm(ico)
                .searchByIco()
                .onClientNotFound();
    }

    public Function<HLO001_S001_ClientSearchPage, CIF013_S002_ClientNotFoundPage> findByNewRc(String rc) {
        return start -> start
                .closeSearchedHistoryIfVisible()
                .fillSearchTerm(rc)
                .clickSearchByRc()
                .onClientNotFound();
    }

    public Function<HLO001_S001_ClientSearchPage, HLO001_S001_ClientSearchPageTransition> findByCardNumber(String cardNumber) {
        return start -> start
                .closeSearchedHistoryIfVisible()
                .fillSearchTerm(cardNumber)
                .searchByCard();
    }
}
