////////////////////////////////////////////////////////////
//
// FormatDataLibrary.h
//
// The 'Format Message' and 'Format Log Message' nodes can format
// ints, strings, vectors, and more.  To support a new data type,
// all you need to do is implement a blueprint-callable conversion
// function like this one:
//
//  UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"))
//  static FFormatArgumentData FormatArgumentDataBool(bool AutoConvertedValue, const FString &Name);
//
// This conversion function must have three things:
//
//  * It returns FFormatArgumentData
//  * It accepts 'AutoConvertedValue', the value you want to format,
//  * It accepts 'Name', which must be copied into the FFormatArgumentData.
// 
// The parameter names are required: you must use exactly these parameter
// names if you want this to act as a conversion function.  You can put a
// conversion function like that anywhere in the system, in any UCLASS.
// This module will find and use it.
//
// This file also contains the built-in converters for standard types.
//
////////////////////////////////////////////////////////////

#pragma once

#include "CoreMinimal.h"
#include "InputCoreTypes.h"
#include "HAL/Platform.h"
#include "UObject/ObjectMacros.h"
#include "UObject/UObjectGlobals.h"
#include "EditorSubsystem.h"
#include "EdGraphSchema_K2.h"

#include "FormatDataLibrary.generated.h"

////////////////////////////////////////////////////////////
//
// ElxFormatLogVerbosity
//
// Controls the ELogVerbosity of the UE_LOG directive inside
// FormatLogMessage.  Also controls the throttling of log
// messages.
//
// Fatal is deliberately placed at the end so that the
// editor defaults to Error (value 0) when the dropdown is
// uninitialized. The numeric values don't match
// ELogVerbosity, so a conversion function is needed.
//
////////////////////////////////////////////////////////////

UENUM(BlueprintType)
enum class ElxFormatLogVerbosity : uint8 {

	/** Prints an error to the console and log file. The editor collects and reports errors. */
	Error,

	/** Prints a warning to the console and log file. The editor collects and reports warnings. */
	Warning,

	/** Prints a message to the console and log file. */
	Display,

	/** Prints a message to the log file, but not to the console. */
	Log,

	/** Like Display, but suppresses repeated messages with the same format pattern (at most once per second). */
	ThrottledDisplay,

	/** Like Log, but suppresses repeated messages with the same format pattern (at most once per second). */
	ThrottledLog,

	/** Prints a message to the log file only if Verbose logging is enabled for the given category. */
	Verbose,

	/** Prints a message to the log file only if VeryVerbose logging is enabled. */
	VeryVerbose,

	/** Prints a fatal error to the console and log file, then crashes (this crashes the editor too). */
	Fatal,
};

////////////////////////////////////////////////////////////
//
// UlxFormatDataLibrary
//
//
////////////////////////////////////////////////////////////

UCLASS(MinimalAPI)
class UlxFormatDataLibrary : public UEditorSubsystem
{
	GENERATED_BODY()

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

	// Given a pin type, find a converter function that can turn
	// that type into an FFormatArgumentData.  If AllowWild is true,
	// unconnected wildcard pins will use the Blank converter.
	// Returns nullptr if no converter is found.
	//
	static UFunction* GetConverterForPinType(const UEdGraphSchema_K2 *Schema, const FEdGraphPinType& PinType, bool AllowWild);

	// Format a message using FTextFormatter::Format.
	// Meant to be used internally by the Format Message K2Node,
	// which needs an impure wrapper around formatting.
	//
	UFUNCTION(BlueprintCallable, meta=(BlueprintInternalUseOnly = "true"))
	static FText FormatMessageInternal(const FString &InPattern, TArray<FFormatArgumentData> InArgs);

	// Format a message using FTextFormatter::Format, and send
	// it to UE_LOG.  The Context object's name is used as the
	// log category.  Meant to be used internally by the Format
	// Log Message K2Node.
	//
	UFUNCTION(BlueprintCallable, meta=(WorldContext = "Context", BlueprintInternalUseOnly = "true"))
	static void FormatLogMessageInternal(UObject *Context, ElxFormatLogVerbosity Verbosity, const FString &InPattern, TArray<FFormatArgumentData> InArgs);

private:
	static ELogVerbosity::Type ConvertElxFormatLogVerbosity(ElxFormatLogVerbosity Verbosity);

	// Scan all loaded classes for converter functions.
	//
	void ScanForConverters();

	// Cached list of converter functions.
	//
	TArray<UFunction*> Converters;

public:
	UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"), Category = "Luprex|Utility")
	static FFormatArgumentData FormatArgumentDataBool(bool AutoConvertedValue, const FString &Name);

	UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"), Category = "Luprex|Utility")
	static FFormatArgumentData FormatArgumentDataByte(uint8 AutoConvertedValue, const FString &Name);

	UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"), Category = "Luprex|Utility")
	static FFormatArgumentData FormatArgumentDataInt(int AutoConvertedValue, const FString &Name);

	UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"), Category = "Luprex|Utility")
	static FFormatArgumentData FormatArgumentDataInt64(int64 AutoConvertedValue, const FString &Name);

	UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"), Category = "Luprex|Utility")
	static FFormatArgumentData FormatArgumentDataFloat(float AutoConvertedValue, const FString &Name);

	UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"), Category = "Luprex|Utility")
	static FFormatArgumentData FormatArgumentDataDouble(double AutoConvertedValue, const FString &Name);

	UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"), Category = "Luprex|Utility")
	static FFormatArgumentData FormatArgumentDataText(FText AutoConvertedValue, const FString &Name);

	UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"), Category = "Luprex|Utility")
	static FFormatArgumentData FormatArgumentDataString(FString AutoConvertedValue, const FString &Name);

	UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"), Category = "Luprex|Utility")
	static FFormatArgumentData FormatArgumentDataName(FName AutoConvertedValue, const FString &Name);

	UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"), Category = "Luprex|Utility")
	static FFormatArgumentData FormatArgumentDataKey(FKey AutoConvertedValue, const FString &Name);

	UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"), Category = "Luprex|Utility")
	static FFormatArgumentData FormatArgumentDataGender(ETextGender AutoConvertedValue, const FString &Name);

	UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"), Category = "Luprex|Utility")
	static FFormatArgumentData FormatArgumentDataObject(UObject *AutoConvertedValue, const FString &Name);

	UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"), Category = "Luprex|Utility")
	static FFormatArgumentData FormatArgumentDataVector(const FVector &AutoConvertedValue, const FString &Name);

	UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"), Category = "Luprex|Utility")
	static FFormatArgumentData FormatArgumentDataVector2D(const FVector2D &AutoConvertedValue, const FString &Name);

	UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"), Category = "Luprex|Utility")
	static FFormatArgumentData FormatArgumentDataRotator(const FRotator &AutoConvertedValue, const FString &Name);

	UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"), Category = "Luprex|Utility")
	static FFormatArgumentData FormatArgumentDataTransform(const FTransform &AutoConvertedValue, const FString &Name);

	UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"), Category = "Luprex|Utility")
	static FFormatArgumentData FormatArgumentDataGeometry(const FGeometry &AutoConvertedValue, const FString &Name);

	UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"), Category = "Luprex|Utility")
	static FFormatArgumentData FormatArgumentDataAnchors(const FAnchors &AutoConvertedValue, const FString &Name);

	// For pins that were never connected.
	//
	UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"), Category = "Luprex|Utility")
	static FFormatArgumentData FormatArgumentDataBlank(const FString &Name);

	// For pins of enum types.
	//
	UFUNCTION(BlueprintPure, meta = (BlueprintInternalUseOnly = "true"), Category = "Luprex|Utility")
	static FFormatArgumentData FormatArgumentDataEnum(uint8 Value, const FString &Name, const UObject *PinSubCategoryObject);
};
