#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"

#include "HyperTwistHigherDimensionalHUDWidget.generated.h"

class UButton;
class UHorizontalBox;
class UTextBlock;

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FHyperTwistHigherDimensionalHudActionSignature);

/**
 * Native, family-aware control surface for the exact Magic120Cell and
 * MagicCube5D runtimes. The widget remains deliberately independent of either
 * simulation engine; the owning controller translates its semantic actions.
 */
UCLASS(BlueprintType, Blueprintable)
class UNREALHYPERTWIST_API UHyperTwistHigherDimensionalHUDWidget
	: public UUserWidget
{
	GENERATED_BODY()

public:
	UHyperTwistHigherDimensionalHUDWidget(
		const FObjectInitializer& ObjectInitializer);

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|HigherDimensional|HUD")
	bool PrepareHudSurface();

	UFUNCTION(BlueprintPure, Category = "HyperTwist|HigherDimensional|HUD")
	bool IsHudSurfaceReady() const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|HigherDimensional|HUD")
	void ConfigureSurface(
		bool bMagic120Cell,
		bool bPositive5DFace,
		bool bAutoRotating,
		const FString& Title,
		const FString& StateSummary,
		const FString& SelectionSummary,
		const FString& ProjectionSummary,
		const FString& LastAction,
		const FString& PersistenceSummary
	);

	UPROPERTY(BlueprintAssignable, Category = "HyperTwist|HigherDimensional|HUD")
	FHyperTwistHigherDimensionalHudActionSignature OnPreviousPrimaryRequested;

	UPROPERTY(BlueprintAssignable, Category = "HyperTwist|HigherDimensional|HUD")
	FHyperTwistHigherDimensionalHudActionSignature OnNextPrimaryRequested;

	UPROPERTY(BlueprintAssignable, Category = "HyperTwist|HigherDimensional|HUD")
	FHyperTwistHigherDimensionalHudActionSignature OnPreviousSecondaryRequested;

	UPROPERTY(BlueprintAssignable, Category = "HyperTwist|HigherDimensional|HUD")
	FHyperTwistHigherDimensionalHudActionSignature OnNextSecondaryRequested;

	UPROPERTY(BlueprintAssignable, Category = "HyperTwist|HigherDimensional|HUD")
	FHyperTwistHigherDimensionalHudActionSignature OnPreviousTertiaryRequested;

	UPROPERTY(BlueprintAssignable, Category = "HyperTwist|HigherDimensional|HUD")
	FHyperTwistHigherDimensionalHudActionSignature OnNextTertiaryRequested;

	UPROPERTY(BlueprintAssignable, Category = "HyperTwist|HigherDimensional|HUD")
	FHyperTwistHigherDimensionalHudActionSignature OnToggleFaceSideRequested;

	UPROPERTY(BlueprintAssignable, Category = "HyperTwist|HigherDimensional|HUD")
	FHyperTwistHigherDimensionalHudActionSignature OnNegativeTurnRequested;

	UPROPERTY(BlueprintAssignable, Category = "HyperTwist|HigherDimensional|HUD")
	FHyperTwistHigherDimensionalHudActionSignature OnPositiveTurnRequested;

	UPROPERTY(BlueprintAssignable, Category = "HyperTwist|HigherDimensional|HUD")
	FHyperTwistHigherDimensionalHudActionSignature OnPreviousProjectionLayerRequested;

	UPROPERTY(BlueprintAssignable, Category = "HyperTwist|HigherDimensional|HUD")
	FHyperTwistHigherDimensionalHudActionSignature OnNextProjectionLayerRequested;

	UPROPERTY(BlueprintAssignable, Category = "HyperTwist|HigherDimensional|HUD")
	FHyperTwistHigherDimensionalHudActionSignature OnToggleAutoRotateRequested;

	UPROPERTY(BlueprintAssignable, Category = "HyperTwist|HigherDimensional|HUD")
	FHyperTwistHigherDimensionalHudActionSignature OnScrambleRequested;

	UPROPERTY(BlueprintAssignable, Category = "HyperTwist|HigherDimensional|HUD")
	FHyperTwistHigherDimensionalHudActionSignature OnResetRequested;

	UPROPERTY(BlueprintAssignable, Category = "HyperTwist|HigherDimensional|HUD")
	FHyperTwistHigherDimensionalHudActionSignature OnSaveRequested;

	UPROPERTY(BlueprintAssignable, Category = "HyperTwist|HigherDimensional|HUD")
	FHyperTwistHigherDimensionalHudActionSignature OnLoadRequested;

	UPROPERTY(BlueprintAssignable, Category = "HyperTwist|HigherDimensional|HUD")
	FHyperTwistHigherDimensionalHudActionSignature OnCoachRequested;

	UPROPERTY(BlueprintAssignable, Category = "HyperTwist|HigherDimensional|HUD")
	FHyperTwistHigherDimensionalHudActionSignature OnMenuRequested;

protected:
	virtual TSharedRef<SWidget> RebuildWidget() override;

private:
	void EnsureWidgetTreeBuilt();
	UTextBlock* MakeText(
		const FString& Text,
		int32 FontSize,
		const FLinearColor& Color,
		const FName& Name = NAME_None
	);
	UButton* AddButton(
		UHorizontalBox* Parent,
		const FString& Label,
		const FName& Name,
		bool bPrimary = false
	);
	static void SetButtonLabel(UButton* Button, const FString& Label);

	UFUNCTION()
	void HandlePreviousPrimary();

	UFUNCTION()
	void HandleNextPrimary();

	UFUNCTION()
	void HandlePreviousSecondary();

	UFUNCTION()
	void HandleNextSecondary();

	UFUNCTION()
	void HandlePreviousTertiary();

	UFUNCTION()
	void HandleNextTertiary();

	UFUNCTION()
	void HandleToggleFaceSide();

	UFUNCTION()
	void HandleNegativeTurn();

	UFUNCTION()
	void HandlePositiveTurn();

	UFUNCTION()
	void HandlePreviousProjectionLayer();

	UFUNCTION()
	void HandleNextProjectionLayer();

	UFUNCTION()
	void HandleToggleAutoRotate();

	UFUNCTION()
	void HandleScramble();

	UFUNCTION()
	void HandleReset();

	UFUNCTION()
	void HandleSave();

	UFUNCTION()
	void HandleLoad();

	UFUNCTION()
	void HandleCoach();

	UFUNCTION()
	void HandleMenu();

	UPROPERTY(Transient)
	TObjectPtr<UTextBlock> EyebrowText = nullptr;

	UPROPERTY(Transient)
	TObjectPtr<UTextBlock> TitleText = nullptr;

	UPROPERTY(Transient)
	TObjectPtr<UTextBlock> StateText = nullptr;

	UPROPERTY(Transient)
	TObjectPtr<UTextBlock> SelectionText = nullptr;

	UPROPERTY(Transient)
	TObjectPtr<UTextBlock> ProjectionText = nullptr;

	UPROPERTY(Transient)
	TObjectPtr<UTextBlock> LastActionText = nullptr;

	UPROPERTY(Transient)
	TObjectPtr<UTextBlock> PersistenceText = nullptr;

	UPROPERTY(Transient)
	TObjectPtr<UTextBlock> GuidanceText = nullptr;

	UPROPERTY(Transient)
	TObjectPtr<UHorizontalBox> TertiaryActionRow = nullptr;

	UPROPERTY(Transient)
	TObjectPtr<UButton> PreviousPrimaryButton = nullptr;

	UPROPERTY(Transient)
	TObjectPtr<UButton> NextPrimaryButton = nullptr;

	UPROPERTY(Transient)
	TObjectPtr<UButton> PreviousSecondaryButton = nullptr;

	UPROPERTY(Transient)
	TObjectPtr<UButton> NextSecondaryButton = nullptr;

	UPROPERTY(Transient)
	TObjectPtr<UButton> PreviousTertiaryButton = nullptr;

	UPROPERTY(Transient)
	TObjectPtr<UButton> NextTertiaryButton = nullptr;

	UPROPERTY(Transient)
	TObjectPtr<UButton> FaceSideButton = nullptr;

	UPROPERTY(Transient)
	TObjectPtr<UButton> NegativeTurnButton = nullptr;

	UPROPERTY(Transient)
	TObjectPtr<UButton> PositiveTurnButton = nullptr;

	UPROPERTY(Transient)
	TObjectPtr<UButton> AutoRotateButton = nullptr;
};
