package cz.moneta.test.dsl.hypos.tasks;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.hypos.enums.HyposProfile;
import cz.moneta.test.dsl.hypos.pages.HyposLoginPage;
import cz.moneta.test.dsl.hypos.pages.contract.ContractHistoryPage;
import cz.moneta.test.dsl.hypos.pages.contract.hyposutils.TestDrawVerificationPage;
import cz.moneta.test.dsl.util.data.client.HyposClient;
import cz.moneta.test.dsl.util.data.client.NewClient;
import cz.moneta.test.dsl.util.data.client.NewClientWithCurrentAccountInformation;
import cz.moneta.test.dsl.util.data.hypos.HyposContract;

import java.util.function.Function;

import static cz.moneta.test.dsl.hypos.enums.Client.FIRST_CLIENT;
import static cz.moneta.test.dsl.hypos.enums.HyposLogType.TASK;

public class HyposTasks {
    public static final String defaultDateFormat = "d.M.yyyy";

    private final Harness harness;

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

    public HypokalkulackaTasks hypokalkulackaTasks() {
        return new HypokalkulackaTasks(harness);
    }

    public HypokomTasks hypokomTasks() {
        return new HypokomTasks(harness);
    }

    public HyposCommonTasks hyposCommonTasks() {
        return new HyposCommonTasks(harness);
    }

    public HyposWso2Tasks hyposWso2Tasks() {
        return new HyposWso2Tasks(harness);
    }

    public HyposStructureTasks hyposStructureTasks() {
        return new HyposStructureTasks(harness);
    }

    public ContractTasks contractTasks() {
        return new ContractTasks(harness);
    }

    public ValuationTasks valuationTasks() {
        return new ValuationTasks(harness);
    }

    public Function<HyposLoginPage, HyposLoginPage> loginToHyposWithProfile(HyposProfile profile) {
        this.hyposCommonTasks().hyposLog(TASK, new Object(){}.getClass().getEnclosingMethod().getName());
        return start -> start.selectSecondaryProfile(profile.getValue())
                .checkLoginButton();
    }

    public Function<HyposLoginPage, ContractHistoryPage> searchAndOpenContractAndCheckState(String contractId, String contractState) {
        this.hyposCommonTasks().hyposLog(TASK, new Object(){}.getClass().getEnclosingMethod().getName());
        return start -> start.then(searchAndOpenContract(contractId))
                .checkContractState(contractState);
    }

    public Function<HyposLoginPage, ContractHistoryPage> searchAndOpenContract(String contractId) {
        this.hyposCommonTasks().hyposLog(TASK, new Object(){}.getClass().getEnclosingMethod().getName());
        return start -> start.switchToHyposMainPage()
                .clickSearchContract()
                .fillContractId(contractId)
                .clickSearchcontractButton()
                .checkContractId(contractId)
                .clickEnterContractButton()
                .checkContractIdPresent(contractId);
    }

    public Function<TestDrawVerificationPage, TestDrawVerificationPage> searchAndVerifyDraw(String contractId) {
        this.hyposCommonTasks().hyposLog(TASK, new Object(){}.getClass().getEnclosingMethod().getName());
        return start -> start.fillContractToSearch(contractId)
                .clickSearchButton()
                .clickDrawCheckbox()
                .clickVerifyDraw();
    }

    public String createNewCifAndCurrentAccount(String bankerUsername, HyposClient hyposClient) {
        this.hyposCommonTasks().hyposLog(TASK, new Object(){}.getClass().getEnclosingMethod().getName());
        NewClientWithCurrentAccountInformation newFoClientInformation = harness.dataPrepare().createCifWithCurrentAccountApiSequence(bankerUsername, hyposClient.getNonClient());
        return newFoClientInformation.getCurrentAccount();
    }

    public String createCurrentAccountForExistingClient(String bankerUsername, HyposContract hyposContract, String cif) {
        this.hyposCommonTasks().hyposLog(TASK, new Object(){}.getClass().getEnclosingMethod().getName());
        NewClient newCustomer = new NewClient(hyposContract.getClients().get(FIRST_CLIENT.getValue()).getNonClient(), cif);
        return harness.dataPrepare().createCurrentAccountFo(bankerUsername, newCustomer);
    }
}