#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "ZMMOCharSummary.h"
#include "UICharCard_Base.generated.h"

class UCommonTextBlock;
class UBorder;
class UUIButton_Base;
class UWidget;

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FZMMOOnCharCardSelected, FString, CharId);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FZMMOOnCharCardDeleteRequest, FString, CharId);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FZMMOOnCharCardDeleteAccept, FString, CharId);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FZMMOOnCharCardDeleteCancel, FString, CharId);

/**
 * Card de personagem (instanciado dinamicamente pelo Lobby/CharSelect).
 *
 * Quando `Summary.DeleteScheduledAtMs > 0`, o card alterna os botoes:
 *   - Btn_Select   → escondido
 *   - Btn_Delete   → escondido
 *   - Btn_AcceptDelete → visivel (efetiva o delete depois da janela)
 *   - Btn_CancelDelete → visivel (cancela o agendamento)
 *
 * Visibilidade so e alterada se os widgets opcionais estiverem bindados —
 * o WBP pode optar por mostrar/esconder via BP override.
 */
UCLASS(Abstract, Blueprintable)
class ZMMO_API UUICharCard_Base : public UUserWidget
{
	GENERATED_BODY()

public:
	UFUNCTION(BlueprintCallable, Category = "Zeus|CharCard")
	void SetFromSummary(const FZMMOCharSummary& InSummary);

	UPROPERTY(BlueprintReadOnly, Category = "Zeus|CharCard")
	FZMMOCharSummary Summary;

	UPROPERTY(BlueprintAssignable, Category = "Zeus|CharCard")
	FZMMOOnCharCardSelected OnCardSelected;

	UPROPERTY(BlueprintAssignable, Category = "Zeus|CharCard")
	FZMMOOnCharCardDeleteRequest OnCardDeleteRequest;

	UPROPERTY(BlueprintAssignable, Category = "Zeus|CharCard")
	FZMMOOnCharCardDeleteAccept OnCardDeleteAccept;

	UPROPERTY(BlueprintAssignable, Category = "Zeus|CharCard")
	FZMMOOnCharCardDeleteCancel OnCardDeleteCancel;

	/**
	 * Hook BP — chamado apos SetFromSummary aplicar valores. Util pra
	 * ajustar visual quando delete esta agendado (badges, cores, etc).
	 */
	UFUNCTION(BlueprintImplementableEvent, Category = "Zeus|CharCard")
	void OnSummaryApplied(bool bIsDeleteScheduled);

protected:
	virtual void NativeConstruct() override;
	virtual void NativeDestruct() override;

	void HandleSelectClicked();
	void HandleDeleteClicked();
	void HandleAcceptDeleteClicked();
	void HandleCancelDeleteClicked();

	/** Re-renderiza o texto do countdown (chamado pelo timer 1s). */
	UFUNCTION()
	void TickCountdown();

	void StartCountdownTimer();
	void StopCountdownTimer();

	UPROPERTY(meta = (BindWidgetOptional)) TObjectPtr<UBorder>          Card_Bg;
	UPROPERTY(meta = (BindWidgetOptional)) TObjectPtr<UCommonTextBlock> Text_Name;
	UPROPERTY(meta = (BindWidgetOptional)) TObjectPtr<UCommonTextBlock> Text_Level;
	UPROPERTY(meta = (BindWidgetOptional)) TObjectPtr<UCommonTextBlock> Text_Class;
	UPROPERTY(meta = (BindWidget))         TObjectPtr<UUIButton_Base>   Btn_Select;
	UPROPERTY(meta = (BindWidgetOptional)) TObjectPtr<UUIButton_Base>   Btn_Delete;
	UPROPERTY(meta = (BindWidgetOptional)) TObjectPtr<UUIButton_Base>   Btn_AcceptDelete;
	UPROPERTY(meta = (BindWidgetOptional)) TObjectPtr<UUIButton_Base>   Btn_CancelDelete;
	UPROPERTY(meta = (BindWidgetOptional)) TObjectPtr<UCommonTextBlock> Text_DeleteCountdown;

private:
	bool bBound = false;

	FTimerHandle CountdownTimerHandle;
};
