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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.search.HLO001_S001_ClientSearchPage;
import cz.moneta.test.dsl.ufo.common.authentication.UfoLoginPage;
import cz.moneta.test.dsl.ufo.common.landing.ApplicationSelectPage;
import cz.moneta.test.harness.support.auth.AuthSupport;
import cz.moneta.test.harness.support.auth.Credentials;

import java.util.function.Function;

import static cz.moneta.test.dsl.util.credentials.Keys.ufo.banka.banker;

public final class LoginTasks {

    private final Harness harness;

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

    public Function<UfoLoginPage, HLO001_S001_ClientSearchPage> asBanker() {
        Credentials credentials = AuthSupport.getCredentials(banker, harness);
        return this.withCredentials(credentials);
    }

    public Function<UfoLoginPage, HLO001_S001_ClientSearchPage> withCredentialsAndTurnOffSignpad(Credentials credentials) {
        return start -> start
                .then(withCredentials(credentials))
                .then(turnSignpadOff());
    }

    public Function<UfoLoginPage, HLO001_S001_ClientSearchPage> withCredentials(Credentials credentials) {
        return start -> start.then(loginWithCredentials(credentials))
                .selectUfoBankaAndContinue();
    }

    public Function<UfoLoginPage, HLO001_S001_ClientSearchPage> withCredentialsAndSelectFirstOM(Credentials credentials) {
        return start -> start.then(loginWithCredentials(credentials))
                .clickFirstOM()
                .selectUfoBankaAndContinue();
    }

    public Function<UfoLoginPage, HLO001_S001_ClientSearchPage> withCredentialsSecondBanker(Credentials credentials) {
        return start -> start.then(loginWithCredentials(credentials))
                .clickContinue();
    }

    public Function<UfoLoginPage, HLO001_S001_ClientSearchPage> withCredentialsSecondBankerAndSelectFirstOM(Credentials credentials) {
        return start -> start.then(loginWithCredentials(credentials))
                .clickFirstOM()
                .selectUfoBankaAndContinue();
    }

    public Function<HLO001_S001_ClientSearchPage, HLO001_S001_ClientSearchPage> turnSignpadOff() {
        return start -> start.closeDispositionWindowIfVisible()
                .clickSignatureTemplates()
                .turnSignpadOffWithReason("Turning signpad off from automated test");
    }

    public Function<UfoLoginPage, HLO001_S001_ClientSearchPage> withCredentialsAndSelectOmByOrderAndTurnOffSignpad(Credentials credentials, int omOrder) {
        return start -> start.then(loginWithCredentials(credentials))
                .selectOm(omOrder)
                .selectUfoBankaAndContinue()
                .then(turnSignpadOff());
    }

    public Function<UfoLoginPage, HLO001_S001_ClientSearchPage> withCredentialsAndSelectFirstOmAndTurnOffSignpad(Credentials credentials) {
        return start -> start.then(loginWithCredentials(credentials))
                .selectFirstOmIfPresent()
                .selectUfoBankaAndContinue()
                .then(turnSignpadOff());
    }

    private Function<UfoLoginPage, ApplicationSelectPage> loginWithCredentials(Credentials credentials) {
        return start -> start
                .typeUserNameAndPasswordAndClickLogin(credentials.getUsername(), credentials.getPassword())
                .onApplicationSelectPage();
    }
}
