// Copyright Pacificao. All Rights Reserved.

#pragma once

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

class UButton;
class UTextBlock;
class UVerticalBox;

UENUM(BlueprintType)
enum class EAgrarianMvpFrontendScreen : uint8
{
	MainMenu,
	CharacterSelection,
	JoinServer,
	Loading,
	SavingAndQuit
};

UENUM(BlueprintType)
enum class EAgrarianMvpCharacterArchetype : uint8
{
	YoungAdultMale,
	YoungAdultFemale
};

UCLASS()
class AGRARIANGAME_API UAgrarianMvpFrontendWidget : public UUserWidget
{
	GENERATED_BODY()

public:
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|MVP UI")
	EAgrarianMvpFrontendScreen ActiveScreen = EAgrarianMvpFrontendScreen::MainMenu;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|MVP UI")
	EAgrarianMvpCharacterArchetype SelectedCharacterArchetype = EAgrarianMvpCharacterArchetype::YoungAdultMale;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|MVP UI")
	FText MainMenuTitle = FText::FromString(TEXT("Agrarian"));

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|MVP UI")
	FText MainMenuSubtitle = FText::FromString(TEXT("MVP investor build"));

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|MVP UI")
	FText PrimaryActionLabel = FText::FromString(TEXT("Begin"));

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|MVP UI")
	FText JoinServerAddress = FText::FromString(TEXT("play.agrariangame.com:7777"));

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|MVP UI", meta = (ClampMin = "0.75", ClampMax = "1.5"))
	float UiScale = 1.0f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|MVP UI")
	bool bUseHighContrast = false;

	UFUNCTION(BlueprintCallable, Category = "Agrarian|MVP UI")
	void SetActiveScreen(EAgrarianMvpFrontendScreen NewScreen);

	UFUNCTION(BlueprintCallable, Category = "Agrarian|MVP UI")
	void SetSelectedCharacterArchetype(EAgrarianMvpCharacterArchetype NewArchetype);

	UFUNCTION(BlueprintCallable, Category = "Agrarian|MVP UI")
	void SetUiScale(float NewUiScale);

	UFUNCTION(BlueprintCallable, Category = "Agrarian|MVP UI")
	void SetHighContrastMode(bool bNewUseHighContrast);

	UFUNCTION(BlueprintCallable, Category = "Agrarian|MVP UI")
	void ConfirmActiveScreen();

	UFUNCTION(BlueprintCallable, Category = "Agrarian|MVP UI")
	void BackFromActiveScreen();

	UFUNCTION(BlueprintCallable, Category = "Agrarian|MVP UI")
	void SaveAndQuit();

protected:
	virtual void NativeConstruct() override;

	virtual FReply NativeOnKeyDown(const FGeometry& InGeometry, const FKeyEvent& InKeyEvent) override;

private:
	void ContinueFromActiveScreen();
	void ReturnFromActiveScreen();
	void CompleteFrontendFlow();
	void RebuildFrontendTree();
	UTextBlock* AddText(UVerticalBox* Parent, const FText& Text, int32 FontSize, bool bBold, const FLinearColor& Color, float BottomPadding);
	UButton* AddButton(UVerticalBox* Parent, const FText& Text, const FLinearColor& NormalColor, const FLinearColor& HoveredColor, float BottomPadding);

	UFUNCTION()
	void FocusPrimaryButton();

	UFUNCTION()
	void ExecuteSaveAndQuit();

	UFUNCTION()
	void HandlePrimaryActionClicked();

	UFUNCTION()
	void HandleBackClicked();

	UFUNCTION()
	void HandleSaveAndQuitClicked();

	UFUNCTION()
	void HandleMaleCharacterClicked();

	UFUNCTION()
	void HandleFemaleCharacterClicked();

	void DeferFrontendAction(TFunction<void()> Action);

	UPROPERTY()
	TObjectPtr<UButton> PrimaryFocusButton;

	FText GetSelectedCharacterLabel() const;
	FText GetSelectedRoleLabel() const;
};
