package cz.moneta.test.dsl.util.task.ufo.operations;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage;
import cz.moneta.test.dsl.ufo.banka.tasks.BankaTasks;
import cz.moneta.test.dsl.ufo.operations.main.codelists.HCisBLDuvodVlozeni;
import cz.moneta.test.dsl.ufo.operations.main.codelists.HCisBLTypAkce;
import cz.moneta.test.dsl.ufo.operations.search.BLK701_S001_BlacklistWhitelist;
import cz.moneta.test.dsl.ufo.operations.search.OPS001_S001_ClientSearchPageUfoOperations;
import cz.moneta.test.harness.support.auth.Credentials;

import java.util.function.Function;

public class BlacklistWhitelistTasks {
    private final Harness harness;

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

    public void openUfoOpsAndAddToRcBlacklist(String birthNumber, Credentials credentials) {
        harness.withUfoSecondInstance()
                .openLoginPage()
                .then(harness.tasks().ufo().operations().login().withCredentials(credentials))
                .then(addClientRcToBlacklist(birthNumber, HCisBLDuvodVlozeni.BILY_KUN, HCisBLTypAkce.OVEROVAT, "Test"));
    }

    public Function<OPS001_S001_ClientSearchPageUfoOperations, OPS001_S001_ClientSearchPageUfoOperations> addClientRcToBlacklist(String birthNumber, HCisBLDuvodVlozeni reason, HCisBLTypAkce actionType, String note) {
        return start -> start
                .clickBlacklistWhitelist()
                .then(addToRcBlacklist(birthNumber, reason, actionType, note))
                .clickHomePage();
    }

    public Function<BLK701_S001_BlacklistWhitelist, BLK701_S001_BlacklistWhitelist> addToRcBlacklist(String birthNumber, HCisBLDuvodVlozeni reason, HCisBLTypAkce actionType, String note) {
        return start -> start.selectBlacklistType("BlackList RČ")
                .fillRCSearchInput(birthNumber)
                .clickCheck()
                .fillNote(note)
                .selectReason(reason.getValue())
                .selectActionType(actionType.getValue())
                .clickInsert();
    }

    public Function<HKO101_S001_MainClientPage, HKO101_S001_MainClientPage> addToRcBlacklistFromUfoBankaAndSwitchBackToClient(String birthNumber) {
        BankaTasks bankaTasks = harness.tasks().ufo().banka();

        return start -> start
                .clickSearch()
                .clickLoginMenu()
                .selectFirstOmIfPresent()
                .selectUfoOperationsAndContinueWithoutOmSelect()
                .then(addClientRcToBlacklist(birthNumber, HCisBLDuvodVlozeni.BILY_KUN, HCisBLTypAkce.OVEROVAT, "Test"))
                .clickChangeApplication()
                .clickUfoBanka()
                .then(bankaTasks.login().turnSignpadOff())
                .then(bankaTasks.search().searchByRc(birthNumber));
    }

}