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

#pragma once

#include "CoreMinimal.h"
#include "Kismet/KismetSystemLibrary.h"
#include "Input/Events.h"
#include "Common.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Components/CanvasPanelSlot.h"

#include "UtilityLibrary.generated.h"

class UEnhancedInputLocalPlayerSubsystem;
class UAnimSequenceBase;


/**
 *
 * UlxUtilityLibrary is for functions that are aren't particularly luprex-specific,
 * but rather, are just generally-useful functionality that could help in any
 * Unreal program.
 * 
 */
UCLASS()
class INTEGRATION_API UlxUtilityLibrary : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

public:
	
	// Call a function by name, on any UObject.  If the function doesn't exist, calls
	// the fallback function instead.  If that isn't found either, returns false.
	//
	UFUNCTION(BlueprintCallable, Meta = (DefaultToSelf = "target"), Category = "Luprex|Utility")
		static void CallFunctionByName(UObject *target, const FString &NamePart1, const FString &NamePart2, const FString &fallback, bool bFailIfNotFound = true);

	// Get the axis-aligned bounding box of an actor.
	//
	UFUNCTION(BlueprintCallable, Meta = (DefaultToSelf = "target"), Category = "Luprex|Utility")
		static FBox GetActorBounds(const AActor *target, bool bOnlyCollidingComponents = false, bool bIncludeFromChildActors = false);
	
	// Add movement input, using the yaw of the control rotation to find a rightward vector.
	//
	UFUNCTION(BlueprintCallable, Meta = (DefaultToSelf = "target"), Category = "Pawn|Input")
		static void AddMovementInputRightward(APawn *target, double ScaleValue=1.0, bool Force=false);

	// Add movement input, using the yaw of the control rotation to find a forward vector.
	//
	UFUNCTION(BlueprintCallable, Meta = (DefaultToSelf = "target"), Category = "Pawn|Input")
		static void AddMovementInputForward(APawn *target, double ScaleValue=1.0, bool Force=false);

	// Get the enhanced input local player subsystem from a controller.  If the controller
	// is not a player controller, or if it is a player controller but it doesn't have an
	// enhanced input subsystem, return nullptr.
	UFUNCTION(BlueprintPure, Category="Player Controller|Local Player Subsystems")
		static UEnhancedInputLocalPlayerSubsystem *GetEnhancedInputLocalPlayerSubsystem(AController *Controller);

	// Given a Pixel XY coordinate, convert it to a viewport position.
	//
	// The input should be in the range (0.0, 0.0) to (ViewportX, ViewportY).
	// The output XY will be in the range 0.0 to 1.0.  The input
	// may specify a fraction of a pixel.  So the input (0.0, 0.0)
	// represents the upper-left corner of the upper-left pixel, whereas the
	// input (0.5, 0.5) represents the center of the upper-left pixel.
	UFUNCTION(BlueprintPure, meta = (ReturnDisplayName = "Percent XY"), Category="Luprex|Utility")
	static FVector2D PixelToViewportPosition(FVector2D PixelXY);

	// Given a viewport position, convert it to a Pixel XY coordinate.
	//
	// The input X and Y coordinates should be in the range 0 to 1.  The output
	// will be in the range (0,0) to (ViewportX, ViewportY).  The output may
	// specify a fraction of a pixel.  If SnapToCenter is true, the output is
	// rounded to the center of the nearest pixel.
	UFUNCTION(BlueprintPure, meta = (ReturnDisplayName = "Pixel XY"), Category="Luprex|Utility")
	static FVector2D ViewportPositionToPixel(FVector2D PercentXY, bool SnapToCenter);
	
	// Do a Line Trace from the camera through a specified pixel.
	//
	// This can be used when you have a crosshair on the screen and you want to
	// determine the object that the crosshairs are pointing at.  It can also
	// be used to do a line trace through the mouse. Fractional pixels are allowed.
	// Be aware that (0.0, 0.0) is the upper-left corner of the upper-left pixel,
	// whereas (0.5, 0.5) is the center of the upper-left pixel.
	//
	// The resulting TraceStart and TraceEnd fields of the HitResult will not
	// contain world positions, instead, they will contain the PixelXY.
	//
	UFUNCTION(BlueprintCallable, Category="Collision", meta=(AutoCreateRefTerm="ActorsToIgnore", Keywords="raycast"))
		static bool LineTraceThroughPixel(const APlayerController* PlayerController, 
			FVector2D PixelXY, double MaxDistanceFromCamera,
			ETraceTypeQuery TraceChannel, bool bTraceComplex, EDrawDebugTrace::Type DrawDebugType, bool bIgnorePlayerPawn,
			const TArray<AActor*>& ActorsToIgnore, FHitResult& HitResult);


	// Configure a CanvasPanelSlot's parameters in a single call.
	//
	// Target must be either a UCanvasPanelSlot directly, or a UWidget whose
	// Slot is a UCanvasPanelSlot. If it is neither, logs an error and
	// does nothing.
	//
	UFUNCTION(BlueprintCallable, Category = "Widget", meta = (SizeToContent = "true"))
	static void ConfigureCanvasPanelSlot(UObject *Target, FAnchors Anchors, FVector2D Position, FVector2D Size, FVector2D Alignment, bool SizeToContent);

	// Check if a given key is used by the specified mapping context.
	//
	// This is true if the key is mapped to anything at all within
	// the specified mapping context.
	//
    UFUNCTION(BlueprintCallable, Category = "Input", meta = (ExpandEnumAsExecs="ReturnValue"))
	static ElxUsedOrNotUsed IsKeyUsedByMappingContext(const FKey &Key, const UInputMappingContext *MappingContext);

	// Get a key by name.
	//
	// Returns the null key if there is no such key.
	//
    UFUNCTION(BlueprintPure, Category = "Input|Key")
	static FKey GetKeyByName(const FName &Name);

	// Get a key by name string.
	//
	// Returns the null key if there is no such key.
	//
    UFUNCTION(BlueprintPure, Category = "Input|Key")
	static FKey GetKeyByNameString(const FString &Name);

	// Find an InputAction within an InputMappingContext, matched by asset
	// name (e.g. "IA_Jump"). Returns nullptr if the mapping context is
	// null or no action with the given name is mapped.
	//
    UFUNCTION(BlueprintPure, Category = "Input")
	static UInputAction *FindInputActionByName(const UInputMappingContext *MappingContext, const FString &Name);

	// Get the actor's forward vector multiplied by a speed.
	// If SnapToXY is true, the forward vector is projected
	// onto the XY plane and renormalized before scaling.
	//
	UFUNCTION(BlueprintCallable, BlueprintPure, Category = "Luprex|Utility", meta = (DefaultToSelf = "Actor"))
	static FVector GetActorForwardVelocity(const AActor *Actor, double Speed = 1.0, bool bSnapToXY = false);

	// Syntactically validate lua code. Parses the
	// code and returns an error message. If the code
	// is error-free, the error message is empty.
	//
	UFUNCTION(BlueprintCallable, meta = (WorldContext = "context"), Category = "Luprex|Utility")
	static void ValidateLuaExpr(ElxLuaSyntaxCheck &Status, FString &ErrorMessage, UObject *context, const FString &Code);

	// Log a message to the LogLuprexConsole category.
	//
	UFUNCTION(BlueprintCallable, Category = "Luprex|Utility")
	static void LogLuprexConsoleMessage(const FString &Message);
};
