// Project Lab - NHTV Igad

#include "UnrealProject.h"
#include "SkillSelector.h"
#include "SkillTreeObject.h"
#include "SkillObject.h"
#include "SkillWidget.h"
#include "SkillSelectorItem.h"
#include "SkillTreeWidget.h"
#include "BaseSkillObject.h"
#include "AbilityInfo.h"

TSubclassOf<class USkillSelector> defaultSkillSelector;

USkillSelector::USkillSelector(const FObjectInitializer& init)
	: Super(init)
{
	defaultSkillSelector = ConstructorHelpers::FClassFinder<USkillSelector>(L"/Game/Assets/GUI/Components/WEEGEE_SkillSelector").Class;
}
void USkillSelector::NativeConstruct()
{
	Super::NativeConstruct();

	m_selectedItem = 0;

	TArray<UWidget*> swidgets;
	m_scrollBox = WidgetTree->FindWidget<UScrollBox>("ScrollBox");
	m_border = WidgetTree->FindWidget<UBorder>("Border");
	m_sizeRoot = WidgetTree->FindWidget<USizeBox>("SizeRoot");
	if(!m_sizeRoot)
	{
		GWERROR(L"USkillSelector doesn't have a SizeRoot set");
	}
	if(!m_scrollBox)
	{
		GWERROR(L"USkillSelector doesn't have a ScrollBox set");
	}
	if(!ItemWidgetTemplate)
	{
		GWERROR(L"USkillSelector doesn't have a Item Widget Template set");
		return;
	}
}

void USkillSelector::NativeOnMenuAction(EMenuActionBinding binding)
{
	switch(binding)
	{
	case EMenuActionBinding::Left:
		SetSelection(m_selectedItem - 1);
		break;
	case EMenuActionBinding::Right:
		SetSelection(m_selectedItem + 1);
		break;
	default:
		Super::NativeOnMenuAction(binding);
		break;
	}
}

void USkillSelector::SetSelection(int32 idx)
{
	if(idx < 0 || idx >= m_scrollBox->GetChildrenCount())
		return;

	if(m_selectedItem < m_scrollBox->GetChildrenCount())
		SetItemSelected(m_selectedItem, false);
	SetItemSelected(idx, true);

	m_selectedItem = idx;

	if(m_sizeRoot && m_scrollBox)
	{
		m_scrollBox->ScrollWidgetIntoView(m_items[m_selectedItem], true);
	}
}

void USkillSelector::SetItemSelected(int32 item, bool selected)
{
	m_items[item]->NativeOnSelectionChanged(selected, false);
	if(selected)
		onSkillSelectionChanged.Broadcast(m_items[item]);
}

void USkillSelector::SetSkillTree(class USkillTreeWidget* skillTree)
{
	if(!skillTree)
		return;
	this->skillTree = skillTree;
	USkillTreeObject* treeObject =  skillTree->skillTreeAsset;
	if(!m_scrollBox)
		return;
	if(!ItemWidgetTemplate)
		return;

	m_scrollBox->ClearChildren();
	m_items.SetNum(0);

	uint32 itemIndex = 0;
	for(int32 i = 0; i < treeObject->skills.Num(); i++)
	{
		UBaseSkillObject* skill = treeObject->skills[i]->GetDefaultObject<UBaseSkillObject>();
		if(!skillShapeFilter.Contains(skill->skillShapeType))
			continue;
		
		USkillSelectorItem* widget = CreateWidget<USkillSelectorItem>(GetWorld(), ItemWidgetTemplate);
		widget->baseSkillObject = skill;
		widget->index = itemIndex++;
		widget->parent = this;
		widget->skillTree = skillTree;
		m_scrollBox->AddChild(widget);
		m_items.Add(widget);
	}

	SetSelection(FMath::Min(m_selectedItem, m_items.Num() - 1));
}

USkillWidget* USkillSelector::GetSelectedSkillWidget()
{
	if(m_selectedItem >= m_items.Num())
		return nullptr;
	return m_items[m_selectedItem]->GetSkillWidget();
}

UBaseSkillObject* USkillSelector::GetSelectedSkillAsset()
{
	if(m_selectedItem >= m_items.Num())
		return nullptr;
	return m_items[m_selectedItem]->baseSkillObject;
}