// Oleg Petruny proprietary.

#pragma once

#include "Http.h"
#include "UObject/Object.h"

#include "ContentLoader.generated.h"

UENUM(BlueprintType)
enum class EContentDownloadMethod : uint8
{
    ClearRandom = 0,
    NonRepeatRandom,
    RatingOftenRandom
};

DECLARE_DYNAMIC_DELEGATE_OneParam(FContentDownloadedCallback, FString, pakFilePath);

UCLASS(BlueprintType)
class UContentLoader : public UObject
{
    GENERATED_BODY()

public:
    UFUNCTION(BlueprintCallable, Category = "ContentLoader")
    void DownloadPak(FContentDownloadedCallback downloadedCallback, const EContentDownloadMethod method = EContentDownloadMethod::NonRepeatRandom);

    UFUNCTION(BlueprintCallable, Category = "ContentLoader")
    UClass* LoadPak(const FString& pakFilePath);

    UFUNCTION(BlueprintCallable, Category = "ContentLoader")
    bool UnloadPak(const FString& pakFilePath);

protected:
    virtual void BeginDestroy() override;

    void HttpGet(const FString& url, FHttpRequestCompleteDelegate requestCompleteCallback);
    TArray<FString> ParseDirectoryIndex(const TArray<uint8>& content, const FString& contentType);
    FString SelectContentByMethod(const TArray<FString>& content, const EContentDownloadMethod method);

    FString GetPakMountContent(const FString& pakFilePath, TArray<FString>* content = nullptr);
    UClass* GetPakClass(const FString& pakContentPath);

    TArray<FString> downloadedContent;
};
