// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"

#include "Engine/GameEngine.h"
#include "PortaFrameGrabber.generated.h"

DECLARE_DYNAMIC_DELEGATE_OneParam(FScreenshotDelegate, FString, imageData);

struct FPortaScreenshotRequest
{
public:
	FString BatchRequest;
	TArray<FString> Levels;
	TArray<FString> Sequences;
	FScreenshotDelegate Callback;
};

UCLASS()
class PORTAINTERFACE_API UPortaFrameGrabber : public UWorldSubsystem, public FTickableGameObject
{
	GENERATED_BODY()

public:
	virtual void Initialize(FSubsystemCollectionBase& Collection) override;

	virtual void Tick(float DeltaTime) override;

	virtual bool IsTickable() const override;

	virtual TStatId GetStatId() const override;

	UFUNCTION(BlueprintCallable, Category = "Porta")
	static void RequestScreenshotStatic(const FString& BatchRequest, const TArray<FString>& Levels, const TArray<FString>& Sequences, FScreenshotDelegate Callback);

	UFUNCTION()
	void OnLevelLoaded();
protected:

	void RunBatchAndSequences();
	void RequestScreenshot(const FString& BatchRequest, const TArray<FString>& Levels, const TArray<FString>& Sequences, FScreenshotDelegate Callback);

	TQueue<FPortaScreenshotRequest*> screenshotQueue;

	// For testing purposes, the queue will only start processing images when it reaches a certain size.
	// To disable this, set this to 1.
	const int QUEUE_THRESHOLD = 1;

	int screenshotQueueSize;
	bool shouldProccessQueue;
	FPortaScreenshotRequest* currentReq = nullptr;
	bool WaitingOnLevel = false;

	float ScreenshotDelay = 0.f;

	void AcceptScreenshot(int32 InSizeX, int32 InSizeY, const TArray<FColor>& InImageData);
	void ProcessQueue();
};