#pragma once

#include "CoreMinimal.h"
#include "Common.h"
#include "Components/Widget.h"
#include "InputCoreTypes.h"
#include "Widgets/SOverlay.h"
#include "Widgets/Layout/SBox.h"
#include "Widgets/Layout/SScaleBox.h"
#include "Containers/Ticker.h"
#include "PromptWidget.generated.h"

class UInputAction;
class UInputMappingContext;


UCLASS(BlueprintType, Blueprintable)
class INTEGRATION_API UlxPromptWidget : public UWidget
{
	GENERATED_BODY()

public:
	UPROPERTY(EditAnywhere, Category="Prompt")
	TObjectPtr<UTexture2D> ButtonAtlas;

	UPROPERTY(EditAnywhere, Category="Prompt")
	ElxControllerType ControllerType = ElxControllerType::KeyboardMouse;

	UPROPERTY(EditAnywhere, Category="Prompt")
	FKey GamepadKey = EKeys::Gamepad_FaceButton_Left;

	UPROPERTY(EditAnywhere, Category="Prompt")
	FKey KeyboardKey = EKeys::Z;

	UPROPERTY(EditAnywhere, Setter, Category="Prompt")
	FVector2D Size = FVector2D(64, 64);

	UPROPERTY(EditAnywhere, Setter, Category="Prompt")
	FMargin GlyphMargins = FMargin(0.0f, 0.1f, 0.0f, 0.1f);

	UPROPERTY(EditAnywhere, Setter, Category="Prompt")
	FLinearColor GlyphColor = FLinearColor::White;

	UPROPERTY(EditAnywhere, Setter, Category="Prompt")
	bool Depressed = false;

public:
	UFUNCTION(BlueprintCallable, Category="Prompt")
	void SetKeys(FKey InGamepadKey, FKey InKeyboardKey);

	UFUNCTION(BlueprintCallable, Category="Prompt")
	void SetKeysFromBindings(const UInputMappingContext* InputMappingContext, const UInputAction* EnhancedInputAction);

	UFUNCTION(BlueprintCallable, Category="Prompt")
	void SetGlyphMargins(FMargin InMargins);

	UFUNCTION(BlueprintCallable, Category="Prompt")
	void SetGlyphColor(FLinearColor InColor);

	UFUNCTION(BlueprintCallable, Category="Prompt")
	void SetSize(FVector2D InSize);

	UFUNCTION(BlueprintCallable, Category="Prompt")
	void SetDepressed(bool InDepressed);


protected:
	virtual TSharedRef<SWidget> RebuildWidget() override;
	virtual void SynchronizeProperties() override;
	virtual void ReleaseSlateResources(bool bReleaseChildren) override;

private:
	FSlateBrush MyBrush;
	TSharedPtr<SBox> MyBox;
	TSharedPtr<SOverlay> MyOverlay;
	TSharedPtr<SImage> MyImage;
	TSharedPtr<SScaleBox> MyScaleBox;
	TSharedPtr<STextBlock> MyGlyph;
	SOverlay::FOverlaySlot* MyImageSlot = nullptr;
	SOverlay::FOverlaySlot* MyGlyphSlot = nullptr;

	FBox2f GetIconUVs(int32 IconIndex);
	FMargin GetScaledMargins() const;
	int32 ChooseIcon(bool Playstation, FKey Key) const;
	void ChooseAppearance(int32 &OutIcon, FString &OutGlyph);
	bool OnTick(float DeltaTime);

	FTSTicker::FDelegateHandle TickHandle;
};
