////////////////////////////////////////////////////////////
//
// AssetLookup.h
//
// Provides asset loading by short name. At
// initialization, scans asset directories and builds
// a lookup table mapping (class, short name) pairs
// to full asset paths. Blueprint-callable functions
// let blueprints load assets by short name.
//
////////////////////////////////////////////////////////////

#pragma once

#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "Subsystems/GameInstanceSubsystem.h"
#include "Templates/Tuple.h"
#include "Common.h"
#include "AssetLookup.generated.h"

class AActor;
class UUserWidget;
class UlxLookAtWidget;
class UStaticMesh;
class USkeletalMesh;
class UAnimSequence;

////////////////////////////////////////////////////////////
//
// UlxAssetLookup
//
////////////////////////////////////////////////////////////

UCLASS(MinimalAPI)
class UlxAssetLookup : public UGameInstanceSubsystem
{
	GENERATED_BODY()

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

private:
	TMap<TTuple<FString, FName>, FString> AssetPaths;

private:

	void AddAssets(const TCHAR *Path, UClass *Class, const TCHAR *NamePrefix);

	static void ReportFailedLoad(const FString &ClassName, const FString &Name, const FString &Reason);

	static UObject *LoadAsset(const UObject *Context, UClass *Class, UClass *ChildOf, const FString &Name);

public:
	void RebuildIndex();

	// Get a static mesh by name.
	//
	UFUNCTION(BlueprintCallable, meta = (WorldContext = "Context", ExpandEnumAsExecs="ReturnValue"), Category = "Luprex|Asset Loading")
	static ElxValidOrNotValid LoadStaticMeshAsset(
		UStaticMesh *&Result, const UObject *Context, const FString &Name);

	// Get a skeletal mesh by name.
	//
	UFUNCTION(BlueprintCallable, meta = (WorldContext = "Context", ExpandEnumAsExecs="ReturnValue"), Category = "Luprex|Asset Loading")
	static ElxValidOrNotValid LoadSkeletalMeshAsset(
		USkeletalMesh *&Result, const UObject *Context, const FString &Name);

	// Get an animation sequence by name.
	//
	UFUNCTION(BlueprintCallable, meta = (WorldContext = "Context", ExpandEnumAsExecs="ReturnValue"), Category = "Luprex|Asset Loading")
	static ElxValidOrNotValid LoadAnimSequenceAsset(
		UAnimSequence *&Result, const UObject *Context, const FString &Name);

	// Get a tangible class by name.
	//
	UFUNCTION(BlueprintCallable, meta = (WorldContext = "Context", ExpandEnumAsExecs="ReturnValue"), Category = "Luprex|Asset Loading")
	static ElxValidOrNotValid LoadTangibleBlueprintAsset(
		TSubclassOf<AActor> &Result, const UObject *Context, const FString &Name);

	// Get a widget blueprint by name.
	//
	UFUNCTION(BlueprintCallable, meta = (WorldContext = "Context", ExpandEnumAsExecs="ReturnValue"), Category = "Luprex|Asset Loading")
	static ElxValidOrNotValid LoadUserWidgetAsset(
		TSubclassOf<UUserWidget> &Result, const UObject *Context, const FString &Name);
};
