// Project Lab - NHTV Igad

#include "UnrealProject.h"
#include "HealthBar.h"
#include "StatBar.h"

// 1 = Crown
// 2 = Mini-Boss
// 3 = Dot
// 4 = Camp
// 5 = PlayerIcon
static UTexture2D* barIcons[6];
UHealthBar::UHealthBar(const FObjectInitializer& init) : Super(init)
{
	barIcons[0] = ConstructorHelpers::FObjectFinder<UTexture2D>(L"/Game/Assets/Art/UI/Minimap/T_IconCrown").Object;
	barIcons[1] = ConstructorHelpers::FObjectFinder<UTexture2D>(L"/Game/Assets/Art/UI/Minimap/T_IconMini").Object;
	barIcons[2] = ConstructorHelpers::FObjectFinder<UTexture2D>(L"/Game/Assets/Art/UI/Minimap/T_IconDot").Object;
	barIcons[3] = ConstructorHelpers::FObjectFinder<UTexture2D>(L"/Game/Assets/Art/UI/Minimap/T_IconCamp").Object;
	barIcons[4] = ConstructorHelpers::FObjectFinder<UTexture2D>(L"/Game/Assets/Art/UI/Minimap/T_IconPlayer").Object;
}

void UHealthBar::NativeConstruct()
{
	Super::NativeConstruct();
	m_currentIcon = (EMinimapIcon)-1;
	m_HP = Cast<UStatBar>(WidgetTree->FindWidget("HP"));
	m_mana = Cast<UStatBar>(WidgetTree->FindWidget("Mana"));
	m_styleSwitcher = Cast<UWidgetSwitcher>(WidgetTree->FindWidget("StyleSwitcher"));
	m_HP1 = Cast<UStatBar>(WidgetTree->FindWidget("HP1"));
	m_icon = Cast<UImage>(WidgetTree->FindWidget("Icon"));
	m_name = Cast<UTextBlock>(WidgetTree->FindWidget("Name"));
	SetName("");
	SetIcon(EMinimapIcon::None);
	if(m_mana)
	{
		m_mana->SetBarColor(EBarColor::Blue);
	}
	m_style = 0;
	m_friendly = false;
}

void UHealthBar::NativeDestruct()
{
	Super::NativeDestruct();
}

void UHealthBar::SetIcon(EMinimapIcon _iconID)
{
	if(!m_icon)
		return;
	if(_iconID == m_currentIcon)
		return;
	int32 iconID = FMath::Clamp((int32)_iconID, 0, 5);
	if(iconID == 0)
		m_icon->SetVisibility(ESlateVisibility::Collapsed);
	else
	{
		m_icon->SetBrushFromTexture(barIcons[iconID - 1]);
		m_icon->SetVisibility(ESlateVisibility::SelfHitTestInvisible);
	}
}

void UHealthBar::SetStyle(int32 style)
{
	if(style == m_style)
		return;
	m_style = FMath::Clamp(style, 0, 1);
	m_styleSwitcher->SetActiveWidgetIndex(m_style);
}
void UHealthBar::UpdateHealth(int32 curr, int32 max)
{
	m_updateCount++;
	bool showAnimation = m_updateCount > 8;
	if(m_style == 0)
	{
		m_HP->SetStat(curr, max, showAnimation);
	}
	else
	{
		m_HP1->SetStat(curr, max, showAnimation);
	}
}

void UHealthBar::UpdateMana(int32 curr, int32 max, float blocked)
{
	if(m_style == 0)
	{
		m_mana->SetStat(curr, max, true);
	}
}

void UHealthBar::UpdateAlignment(bool friendly)
{
	m_friendly = friendly;

	const FLinearColor colorFriendly = FLinearColor(0.18f, 0.8f, 0.23f);
	const FLinearColor colorEnemy = FLinearColor(0.73f, 0.1f, 0.13f);

	if(m_style == 0)
	{
		m_HP->SetBarColor(friendly ? EBarColor::Green : EBarColor::Red);
	}
	else
	{
		m_HP1->SetBarColor(friendly ? EBarColor::Green : EBarColor::Red);
	}
}

void UHealthBar::SetName(FString name)
{
	if(!m_name)
		return;
	if(name.IsEmpty())
	{
		m_name->SetVisibility(ESlateVisibility::Collapsed);
	}
	else
	{
		if(name.Len() > 16)
		{
			name = name.Left(13) + "...";
		}
		m_name->SetText(FText::FromString(name));
		m_name->SetVisibility(ESlateVisibility::SelfHitTestInvisible);
	}
}
