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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.fon.CIF113_S001_ChangeDataFOTransitions;
import cz.moneta.test.dsl.ufo.banka.pages.loans.flexikredit.HPO003_FlexikreditDetailPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.Lookup;

import java.util.function.Function;

import static cz.moneta.test.dsl.ufo.banka.pages.loans.flexikredit.NFL006_S001_FlexikreditScoringResultPage.APPROVED_TABLE_AMOUNT_KEY;
import static cz.moneta.test.dsl.ufo.banka.pages.loans.flexikredit.NFL006_S001_FlexikreditScoringResultPage.APPROVED_TABLE_INTEREST_KEY;

public class SharedTasks {

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> storeFullNameAndIco() {
        return start -> start
                .storeClientFullName()
                .storeIco();
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> storeFullNameAndBirthNumber() {
        return start -> start
                .storeClientFullName()
                .getBirthNumber();
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> storeInternetBankingID() {
        return start -> start
                .clickActiveProduct("Internet Banka heslo/MK; Aktivní")
                .onDirectBankingServices()
                .clickFirstIB()
                .clickSettingsIB()
                .storeInternetBanking_ID()
                .clickReturn()
                .clickReturnTOHKO();
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> storeClientDetailInfo(Harness harness) {
        return start -> start
                .clickEdit()
                .onChangeDataFO()
                .storeFirstName()
                .storeLastName()
                .storeEducation()
                .storeBirthNumber()
                .storeNationality()
                .storeAddress()
                .storeEmail()
                .storeMobilePhoneIncludedCode()
                .storeLandlineIncludedCode()
                .storeFirstIdCardType()
                .storeFirstIdCardNumber()
                .storeFirstIdCardExpirationDate()
                .storeSecondIdCardType()
                .storeSecondIdCardNumber()
                .storeSecondIdCardExpirationDate()
                .storeMaritalStatus()
                .storeHousingType()
                .storeDependantsNumber()
                .clickBack()
                .then(clickContinueOpFoundPageIfVisible(harness));
    }

    public Function<CIF113_S001_ChangeDataFOTransitions, HKO101_S001_MainClientPage> clickContinueOpFoundPageIfVisible(Harness harness){
        if (harness.getEndpoint(UfoEndpoint.class).isElementVisible("//*[text()='OP nalezen']", Lookup.XPATH)) {
            return start->start
                    .onOpFoundPage()
                    .clickLeftButton()
                    .onMainClientPage();
        }
        else {
            return start-> start
                    .onMainClientPage();
        }
    }

    public Function<HKO101_S001_MainClientPage, HPO003_FlexikreditDetailPage> checkFlexikreditDetail(Harness harness) {
        return start -> start
                .clickFlexikredit()
                .checkAmount(harness.get(APPROVED_TABLE_AMOUNT_KEY))
                .checkInterest(harness.get(APPROVED_TABLE_INTEREST_KEY))
                .checkAccountingChargeNotEmpty()
                .checkPenaltyRateNotEmpty();
    }

}
