#include "UIPlayerStatus_StatRow.h"

#include "Components/Button.h"
#include "Components/HorizontalBox.h"
#include "Components/TextBlock.h"
#include "Internationalization/Text.h"

void UUIPlayerStatus_StatRow::SetupStat(int32 InStatId, FText IconLetter, FText DisplayName)
{
	StatId = InStatId;
	if (Border_StatIconWrap_Label) Border_StatIconWrap_Label->SetText(IconLetter);
	if (Text_StatName) Text_StatName->SetText(DisplayName);
}

void UUIPlayerStatus_StatRow::SetValues(int32 Base, int32 Bonus)
{
	if (Text_StatBase) Text_StatBase->SetText(FText::AsNumber(Base));

	const bool bShowBonus = (Bonus != 0);
	const ESlateVisibility BonusVis = bShowBonus ? ESlateVisibility::Visible : ESlateVisibility::Collapsed;

	if (Layout_BonusGroup)
	{
		Layout_BonusGroup->SetVisibility(BonusVis);
	}
	else
	{
		if (Text_Sign)      Text_Sign->SetVisibility(BonusVis);
		if (Text_StatBonus) Text_StatBonus->SetVisibility(BonusVis);
	}

	if (bShowBonus)
	{
		if (Text_Sign)      Text_Sign->SetText(FText::FromString(Bonus > 0 ? TEXT("+") : TEXT("-")));
		if (Text_StatBonus) Text_StatBonus->SetText(FText::AsNumber(FMath::Abs(Bonus)));
	}
}

void UUIPlayerStatus_StatRow::SetPlusVisible(bool bVisible)
{
	if (Button_PlusBtn)
	{
		Button_PlusBtn->SetVisibility(bVisible ? ESlateVisibility::Visible : ESlateVisibility::Collapsed);
	}
}

void UUIPlayerStatus_StatRow::SetPlusEnabled(bool bEnabled)
{
	if (Button_PlusBtn) Button_PlusBtn->SetIsEnabled(bEnabled);
}

void UUIPlayerStatus_StatRow::NativeOnInitialized()
{
	Super::NativeOnInitialized();
	if (Button_PlusBtn)
	{
		Button_PlusBtn->OnClicked.AddDynamic(this, &UUIPlayerStatus_StatRow::HandlePlusClicked);
	}
}

void UUIPlayerStatus_StatRow::NativeDestruct()
{
	if (Button_PlusBtn)
	{
		Button_PlusBtn->OnClicked.RemoveDynamic(this, &UUIPlayerStatus_StatRow::HandlePlusClicked);
	}
	Super::NativeDestruct();
}

void UUIPlayerStatus_StatRow::HandlePlusClicked()
{
	OnPlusClicked.Broadcast(StatId);
}
