#pragma once

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

class UButton;
class UTextBlock;
class UUniformGridPanel;
class UVerticalBox;

UCLASS(BlueprintType, Blueprintable)
class UNREALHYPERTWIST_API UHyperTwistClassicCubeHUDWidget : public UUserWidget
{
	GENERATED_BODY()

public:
	virtual void NativeOnInitialized() override;
	virtual void NativeConstruct() override;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|ClassicCube|HUD")
	bool PrepareClassicCubeHudSurface();

	UFUNCTION(BlueprintPure, Category = "HyperTwist|ClassicCube|HUD")
	bool IsClassicCubeHudSurfaceReady() const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|ClassicCube|HUD")
	void SetHudLines(
		const FString& InStatusLine,
		const FString& InScrambleLine,
		const FString& InTimerLine,
		const FString& InInspectionLine,
		const FString& InResultLine,
		const FString& InControlsLine,
		const FString& InHintLine,
		const FString& InSolutionLine,
		const FString& InReplayLine,
		const FString& InLeaderboardLine,
		const FString& InModeLine,
		const FString& InTrainingLine
	);

	UFUNCTION(BlueprintPure, Category = "HyperTwist|ClassicCube|HUD")
	bool IsNewScrambleButtonHovered() const;

	UFUNCTION(BlueprintPure, Category = "HyperTwist|ClassicCube|HUD")
	bool IsAnyActionButtonHovered() const;

	UFUNCTION(BlueprintCallable, Category = "HyperTwist|ClassicCube|HUD")
	void SetModeToggleButtonLabel(const FString& InLabel);

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

private:
	void EnsureWidgetTreeBuilt();
	UTextBlock* AddLine(
		UVerticalBox* Parent,
		const TCHAR* WidgetName,
		int32 FontSize,
		const FLinearColor& Color
	);
	UButton* CreateActionButton(
		UUniformGridPanel* Parent,
		const TCHAR* ButtonName,
		const TCHAR* LabelName,
		TObjectPtr<UTextBlock>& OutLabel,
		const FString& InitialLabel,
		int32 Row,
		int32 Column
	);

	UFUNCTION()
	void HandleNewScrambleClicked();

	UFUNCTION()
	void HandleHintClicked();

	UFUNCTION()
	void HandleSubmitSolveClicked();

	UFUNCTION()
	void HandleModeToggleClicked();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	bool bNewScrambleButtonHovered = false;
};
