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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.operations.search.OPS001_S001_ClientSearchPageUfoOperations;
import cz.moneta.test.dsl.ufo.operations.ufoOperations.ACQ702_S010_Fraud;
import cz.moneta.test.dsl.ufo.operations.ufoOperations.UKO720_S001_TaskDetail;
import cz.moneta.test.dsl.ufo.operations.ufoOperations.UKO720_S001_TaskList;

import java.util.function.Function;

public class ManualFraudCheckTasks {

    private final Harness harness;

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

    public Function<OPS001_S001_ClientSearchPageUfoOperations, UKO720_S001_TaskList> findFraudTaskByBirthNumber(String birthday) {
        return page -> page.clickTasks()
                .fillBirthNumber(birthday)
                .clickDepartments()
                .clickFind();
    }

    public Function<OPS001_S001_ClientSearchPageUfoOperations, UKO720_S001_TaskList> findFraudTaskByBirthNumberWithTimeout(String birthday) {
        return page -> page.clickTasks()
                .fillBirthNumber(birthday)
                .clickDepartments()
                .waitUntilFoundMFCHTask();
    }

    public Function<UKO720_S001_TaskList, UKO720_S001_TaskDetail> selectFirstMFCHIdOnly(String controlNumber) {
        return page -> page
                .then(selectMFCHTask())
                .then(confirmTaskIdOnly(controlNumber));
    }

    public Function<UKO720_S001_TaskList, UKO720_S001_TaskDetail> selectFirstMFCHBirthCertificateOnly() {
        return page -> page
                .then(selectMFCHTask())
                .then(confirmFraudControlsTask())
                .clickFinish()
                .closeWindow();
    }

    public Function<UKO720_S001_TaskList, UKO720_S001_TaskDetail> selectFirstMFCHBirthCertificateAndId(String controlNumber) {
        return page -> page
                .then(selectMFCHTask())
                .then(confirmFraudControlsTask())
                .then(fillIdFraud(controlNumber))
                .clickFinish()
                .closeWindow();
    }

    public Function<UKO720_S001_TaskList, UKO720_S001_TaskDetail> selectFirstMFCH(String controlNumber) {
        return page -> page
                .then(selectMFCHTask())
                .then(confirmTask(controlNumber));
    }

    public Function<UKO720_S001_TaskList, ACQ702_S010_Fraud> selectMFCHTask() {
        return page -> page
                .clickMFCHDetail()
                .clickProcessDocuments()
                .onFraudRisk()
                .clickCloseRisks();
    }

    public Function<ACQ702_S010_Fraud, UKO720_S001_TaskDetail> confirmTask(String controlNumber) {
        return page -> page
                .then(fillIdFraud(controlNumber))
                .clickFraudControl()
                .clickSetAll()
                .clickSend()
                .clickAgreeSend()
                .clickSetAll()
                .clickSend()
                .clickAgreeSend()
                .clickFinish()
                .closeWindow();
    }

    public Function<ACQ702_S010_Fraud, UKO720_S001_TaskDetail> confirmTaskIdOnly(String controlNumber) {
        return page -> page
                .then(fillIdFraud(controlNumber))
                .clickSetAll()
                .clickSend()
                .clickAgreeSend()
                .clickFinish()
                .closeWindow();
    }

    public Function<UKO720_S001_TaskDetail, UKO720_S001_TaskList> finishFraudTask(String comment) {
        return page -> page
                .selectSolver(2)
                .clickFinishProcess()
                .fillCommentAndClickAgreeFinish(comment);
    }

    private Function<ACQ702_S010_Fraud, ACQ702_S010_Fraud> fillIdFraud(String controlNumber) {
        harness.log(String.format("ID Control Number: %s", controlNumber));
        return page -> page
                .fillCheckingNumber(controlNumber)
                .clickFraudControl()
                .clickSetAll()
                .clickSend()
                .clickAgreeSend();
    }

    public Function<ACQ702_S010_Fraud, ACQ702_S010_Fraud> confirmFraudControlsTask() {
        return page -> page
                .clickFraudControl()
                .clickSetAll()
                .clickSend()
                .clickAgreeSend();
    }
}
