#pragma once
#include "Engine/LatentActionManager.h"
#include "LatentActions.h"
#include "LatentActions.h"
#include "Async/TaskGraphInterfaces.h"

class EMOSHAPESUBSYSTEM_API FEmoShapeLatentAction : public FPendingLatentAction
{
public:
	TFunction<void()> OnCancelNotification = nullptr;

	/** Note that you need to use the resulting call to cleanly exit */
	static FEmoShapeLatentAction* CreateLatentAction(struct FLatentActionInfo& LatentInfo, UObject* WorldContext);

	FEmoShapeLatentAction(const FLatentActionInfo& LatentInfo);

	virtual void UpdateOperation(FLatentResponse& Response) override;

	void Call();
	void Cancel();

	virtual void NotifyObjectDestroyed() override;
	virtual void NotifyActionAborted() override;

#if WITH_EDITOR
	virtual FString GetDescription() const override;
#endif

	const FName ExecutionFunction;
	const int32 OutputLink;
	const FWeakObjectPtr CallbackTarget;

private:
	bool Called;
};

/**
*	Convenience wrappers for common thread/task work flow. Run background task on thread, callback via task graph on game thread
*/
class EMOSHAPESUBSYSTEM_API FEmoShapeLambdaRunnable
{
public:

	/**
	*	Runs the passed lambda on the background thread, new thread per call
	*/
	static void RunLambdaOnBackGroundThread(TFunction< void()> InFunction);

	/**
	*	Runs the passed lambda on the background thread pool
	*/
	static void RunLambdaOnBackGroundThreadPool(TFunction< void()> InFunction);

	/**
	*	Runs a short lambda on the game thread via task graph system
	*/
	static FGraphEventRef RunShortLambdaOnGameThread(TFunction< void()> InFunction);

	/**
	*	Runs a short lambda on background thread via task graph system
	*/
	static FGraphEventRef RunShortLambdaOnBackGroundTask(TFunction< void()> InFunction);

	/** 
	*	Runs a thread with idle for duration before calling back on game thread. 
	*	Due to context cost recommended for >0.1sec durations.
	*/
	static void SetTimeout(TFunction<void()>OnDone, float DurationInSec, bool bCallbackOnGameThread = true);
	
};
