// Project Lab - NHTV Igad

#include "UnrealProject.h"
#include "EffectSelector.h"
#include "EffectSlot.h"
#include "BaseSkillObject.h"
#include "AbilityInfo.h"
#include "SkillWidget.h"

TSubclassOf<class UEffectSelector> defaultEffectSelectorClass;

UEffectSelector::UEffectSelector(const FObjectInitializer& init)
	: Super(init)
{
	defaultEffectSelectorClass = ConstructorHelpers::FClassFinder<UEffectSelector>(L"/Game/Assets/GUI/WEEGEE_SkillEffectSelector").Class;
}
void UEffectSelector::NativeConstruct()
{
	m_container = Cast<UPanelWidget>(WidgetTree->FindWidget("EffectSlotContainer"));
	if(!m_container)
	{
		GERROR("\"Container\" not found in effect selector widget " + GetName());
	}

	onItemSelected.AddDynamic(this, &UEffectSelector::m_OnItemSelected);
	onItemSelectionConfirmed.AddDynamic(this, &UEffectSelector::m_OnItemSelectionConfirmed);

	m_nameField = Cast<UTextBlock>(WidgetTree->FindWidget("NameField"));
	m_descriptionField = Cast<UMultiLineEditableTextBox>(WidgetTree->FindWidget("DescField"));

	Super::NativeConstruct();
}

void UEffectSelector::Init(class USkillWidget* widget)
{
	this->skill = widget->skillAsset;
	this->widget = widget;

	// Remove old items
	for(UEffectSlot* slot : m_effectSlots)
	{
		if(slot)
			slot->RemoveFromParent();
	}

	if(!skill)
	{
		m_effectSlots.SetNum(0);
		RescanItems();
		return;
	}

	// Add effect slot buttons
	for(UAbilityInfo* abilityEffect : skill->abilityEffects)
	{
		if(abilityEffect)
		{
			UEffectSlot* slot = CreateWidget<UEffectSlot>(GetWorld(), effectSlotClass);
			m_container->AddChild(slot);
			slot->Init(abilityEffect);
			m_effectSlots.Add(slot);
		}
		else
		{
			GERROR("Didn't set ability on skill " + skill->GetName());
		}
	}

	RescanItems();
}

void UEffectSelector::Init2(class TArray<UAbilityInfo*> abilities)
{
	this->skill = nullptr;
	this->widget = nullptr;
	// Remove old items
	for(UEffectSlot* slot : m_effectSlots)
	{
		if(slot)
			slot->RemoveFromParent();
	}

	// Add effect slot buttons
	for(UAbilityInfo* abilityEffect : abilities)
	{
		if(abilityEffect)
		{
			UEffectSlot* slot = CreateWidget<UEffectSlot>(GetWorld(), effectSlotClass);
			m_container->AddChild(slot);
			slot->Init(abilityEffect);
			m_effectSlots.Add(slot);
		}
		else
		{
			GERROR("Didn't set ability on skill " + skill->GetName());
		}
	}

	RescanItems();
}

void UEffectSelector::m_OnItemSelected(UMenuItemBase* item)
{
	UEffectSlot* slot = Cast<UEffectSlot>(item);
	if(slot && slot->ability)
	{
		if(m_nameField)
			m_nameField->SetText(FText::FromString(slot->ability->name));
		if(m_descriptionField)
			m_descriptionField->SetText(FText::FromString(slot->ability->description));
	}
}

void UEffectSelector::m_OnItemSelectionConfirmed(UMenuItemBase* item)
{
	UEffectSlot* slot = Cast<UEffectSlot>(item);
	if(slot && slot->ability)
	{
		if(widget)
		{
			widget->SetSelectedEffect(slot->index);
		}
		onEffectSelected.Broadcast(slot->index);
		OnSelectionConfirmed(slot);
	}
	else
	{
		GERROR(L"Invalid ability selected");
	}
}
