#pragma once

#include "CoreMinimal.h"
#include "UI/Common/UIDraggableWindow_Base.h"
#include "ZMMOAttributeTypes.h"
#include "UIPlayerStatus_Window.generated.h"

class UButton;
class UTextBlock;
class UHorizontalBox;
class UUIPlayerStatus_BonusRow;
class UUIPlayerStatus_StatRow;
class UZMMOAttributeComponent;

/**
 *  Janela de Status do player — composta por instâncias dos sub-widgets
 *  UUIPlayerStatus_StatRow (6 primários) e UUIPlayerStatus_BonusRow (8 derivados).
 *
 *  Consome o UZMMOAttributeComponent do PlayerState e a pipeline de
 *  C_STAT_ALLOC (RequestStatAlloc → server valida → OnStatAllocReply).
 *
 *  Pareada com o WBP UI_PlayerStatus_Window. Os 14 rows são BindWidgetOptional
 *  pra suportar variantes (ex.: HUD compacto sem CRIT/ASPD).
 */
UCLASS(Abstract, Blueprintable, BlueprintType)
class ZMMO_API UUIPlayerStatus_Window : public UUIDraggableWindow_Base
{
	GENERATED_BODY()

public:
	UUIPlayerStatus_Window(const FObjectInitializer& ObjectInitializer);

	/** Chave de persistencia no UZMMOUISaveGame. Estavel — nao renomear. */
	virtual FName GetWindowId() const override { return TEXT("PlayerStatus"); }

protected:
	virtual void NativeOnActivated() override;
	virtual void NativeOnDeactivated() override;
	virtual TOptional<FUIInputConfig> GetDesiredInputConfig() const override;

	/** Diz ao stack pra mover foco pra ca' (precisa pra NativeOnKeyDown ser chamado). */
	virtual UWidget* NativeGetDesiredFocusTarget() const override;

	/** Atalho Alt+A — fecha a janela em modo Menu (PC controller nao recebe). */
	virtual FReply NativeOnKeyDown(const FGeometry& Geom, const FKeyEvent& KeyEvent) override;

	/** Drag area = Container_Header (header inteiro). Header_DragHandle no WBP nao necessario. */
	virtual bool IsDragHandleHit(const FPointerEvent& MouseEvent) const override;

	/** Header do WBP (HorizontalBox raiz da identidade) — usado como drag area. */
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status|Header") TObjectPtr<UHorizontalBox> Container_Header;

	/**
	 *  Zona EXCLUÍDA do drag — é onde ficam os rows interativos (+STR etc.).
	 *  Clicar aqui não deve iniciar drag mesmo em area "vazia" entre rows.
	 */
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status") TObjectPtr<UWidget> Container_Stats;

	/**
	 *  Panel de fundo (UIPanel_Base). Setado pra Visible em NativeOnActivated
	 *  pra capturar clicks em areas vazias do modal — sem isso, drag só funciona
	 *  onde algum filho é Visible (header/footer).
	 */
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status") TObjectPtr<UWidget> Background_Panel;

	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status") TObjectPtr<UTextBlock> TitleText;
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status") TObjectPtr<UTextBlock> StatusPointText;

	// Header: identidade do char.
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status|Header") TObjectPtr<UTextBlock> Text_CharName;
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status|Header") TObjectPtr<UTextBlock> Text_Job;
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status|Header") TObjectPtr<UTextBlock> Text_BaseLevel_Value;
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status|Header") TObjectPtr<UTextBlock> Text_JobLevel_Value;

	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status") TObjectPtr<UUIPlayerStatus_StatRow> Row_Str;
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status") TObjectPtr<UUIPlayerStatus_StatRow> Row_Agi;
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status") TObjectPtr<UUIPlayerStatus_StatRow> Row_Vit;
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status") TObjectPtr<UUIPlayerStatus_StatRow> Row_Int;
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status") TObjectPtr<UUIPlayerStatus_StatRow> Row_Dex;
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status") TObjectPtr<UUIPlayerStatus_StatRow> Row_Luk;

	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status|Derived") TObjectPtr<UUIPlayerStatus_BonusRow> Row_Atk;
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status|Derived") TObjectPtr<UUIPlayerStatus_BonusRow> Row_Matk;
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status|Derived") TObjectPtr<UUIPlayerStatus_BonusRow> Row_Def;
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status|Derived") TObjectPtr<UUIPlayerStatus_BonusRow> Row_Mdef;
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status|Derived") TObjectPtr<UUIPlayerStatus_BonusRow> Row_Hit;
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status|Derived") TObjectPtr<UUIPlayerStatus_BonusRow> Row_Flee;
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status|Derived") TObjectPtr<UUIPlayerStatus_BonusRow> Row_Crit;
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status|Derived") TObjectPtr<UUIPlayerStatus_BonusRow> Row_Aspd;

	/** Botao de fechar na header — UButton simples (placeholder, estilizar depois). */
	UPROPERTY(BlueprintReadOnly, meta = (BindWidgetOptional), Category = "Zeus|Status") TObjectPtr<UButton> Button_Close;

private:
	void BindToLocalPlayer();
	void UnbindFromComponent();
	void RefreshFromSnapshot(const FZMMOAttributesSnapshot& Snap);
	void SetAllPrimaryRowsPlusEnabled(bool bEnabled);
	void BindRow(UUIPlayerStatus_StatRow* Row, int32 StatId, const TCHAR* IconLetter, const TCHAR* DisplayName);

	UFUNCTION() void HandleAttributesChanged(const FZMMOAttributesSnapshot& Snapshot);
	UFUNCTION() void HandleStatAllocReply(bool bAccepted, int32 Reason);
	UFUNCTION() void HandleRowPlusClicked(int32 StatId);
	UFUNCTION() void HandleCloseClicked();

	UPROPERTY(Transient)
	TWeakObjectPtr<UZMMOAttributeComponent> BoundComponent;

	/** Trava local pra evitar spam-click: vira true ao clicar, false ao
	 *  receber StatAllocReply (ou snapshot novo, o que vier primeiro). */
	bool bPlusButtonsLocked = false;
};
