#include "UIPlayerStatus_BonusRow.h"

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

void UUIPlayerStatus_BonusRow::SetStatName(FText DisplayName)
{
	if (Text_StatName) Text_StatName->SetText(DisplayName);
}

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

	const bool bShowBonus = (Bonus != 0);
	SetBonusVisibility(bShowBonus);
	if (bShowBonus)
	{
		if (Text_SplitOp)   Text_SplitOp->SetText(FText::FromString(Bonus > 0 ? TEXT("+") : TEXT("-")));
		if (Text_BonusPart) Text_BonusPart->SetText(FText::AsNumber(FMath::Abs(Bonus)));
	}
}

void UUIPlayerStatus_BonusRow::SetRangeValues(int32 MinBase, int32 MaxBase, int32 Bonus)
{
	if (Text_BasePart)
	{
		Text_BasePart->SetText(FText::FromString(FString::Printf(TEXT("%d~%d"), MinBase, MaxBase)));
	}

	const bool bShowBonus = (Bonus != 0);
	SetBonusVisibility(bShowBonus);
	if (bShowBonus)
	{
		if (Text_SplitOp)   Text_SplitOp->SetText(FText::FromString(Bonus > 0 ? TEXT("+") : TEXT("-")));
		if (Text_BonusPart) Text_BonusPart->SetText(FText::AsNumber(FMath::Abs(Bonus)));
	}
}

void UUIPlayerStatus_BonusRow::SetValuesX10(int32 BaseX10, int32 BonusX10)
{
	auto FormatX10 = [](int32 ValX10) -> FString
	{
		const int32 Sign = ValX10 < 0 ? -1 : 1;
		const int32 Abs  = FMath::Abs(ValX10);
		return FString::Printf(TEXT("%d.%d"), Sign * (Abs / 10), Abs % 10);
	};

	if (Text_BasePart) Text_BasePart->SetText(FText::FromString(FormatX10(BaseX10)));

	const bool bShowBonus = (BonusX10 != 0);
	SetBonusVisibility(bShowBonus);
	if (bShowBonus)
	{
		if (Text_SplitOp)   Text_SplitOp->SetText(FText::FromString(BonusX10 > 0 ? TEXT("+") : TEXT("-")));
		if (Text_BonusPart)
		{
			const int32 Abs = FMath::Abs(BonusX10);
			Text_BonusPart->SetText(FText::FromString(FString::Printf(TEXT("%d.%d"), Abs / 10, Abs % 10)));
		}
	}
}

void UUIPlayerStatus_BonusRow::SetBonusVisibility(bool bShow)
{
	const ESlateVisibility Vis = bShow ? ESlateVisibility::Visible : ESlateVisibility::Collapsed;
	if (Text_SplitOp)   Text_SplitOp->SetVisibility(Vis);
	if (Text_BonusPart) Text_BonusPart->SetVisibility(Vis);
}
