// Oleg Petruny proprietary.

#pragma once

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

#include "ContentLoader.generated.h"

UENUM(BlueprintType)
enum class EContentDownloadMethod : uint8
{
    ClearRandom = 0,  //!< Always random from 0 to n
    NonRepeatRandom,  //!< Download the one that isn't downloaded in current runtime (Reset on full set)
    RatingOftenRandom //!< NonRepeatRandom combined with asset rating [WIP]
};

DECLARE_DYNAMIC_DELEGATE_OneParam(FContentDownloadedCallback, FString, pakFilePath);

/*
 * High language wrapper for Paks(Assets) download and loading.
 */
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;

    /** Sends http get request */
    void HttpGet(const FString& url, FHttpRequestCompleteDelegate requestCompleteCallback);
    /** Parses assets html/json index */
    TArray<FString> ParseDirectoryIndex(const TArray<uint8>& content, const FString& contentType);
    /** Selects item by desired method */
    FString SelectContentByMethod(const TArray<FString>& content, const EContentDownloadMethod method);

    /** Reads Pak content */
    FString GetPakMountContent(const FString& pakFilePath, TArray<FString>* content = nullptr);
    /** Returns mount path to desired content item */
    UClass* GetPakClass(const FString& pakContentPath);

    /** Cache of already downloaded content at runtime (used by EContentDownloadMethod::NonRepeatRandom)*/
    TArray<FString> downloadedContent;
};
