// Fill out your copyright notice in the Description page of Project Settings.


#include "FayeBaseWidget.h"

#include "Controller/WidgetControllerBase.h"

UFayeBaseWidget::UFayeBaseWidget()
{
	bIsBackHandler = true;
	ActivatedVisibility = ESlateVisibility::Visible;
	DeactivatedVisibility = ESlateVisibility::Hidden;
	SetIsFocusable(true);
	bIsModal = true;
}

bool UFayeBaseWidget::Initialize()
{
	if (IsDesignTime())
	{
		return Super::Initialize();
	}
	
	TSubclassOf<UWidgetControllerBase> WidgetControllerClass = UWidgetControllerBase::StaticClass();
	if (ControllerClass)
	{
		WidgetControllerClass = ControllerClass;
	}
	
	WidgetController = NewObject<UWidgetControllerBase>(this, WidgetControllerClass);
	WidgetController->InitializeController();
	
	return Super::Initialize();
}

void UFayeBaseWidget::ToggleWidget_Implementation(bool bActive)
{
	if (bActive)
	{
		ActivateWidget();
	}
	else
	{
		DeactivateWidget();
	}
}

void UFayeBaseWidget::SetupWidget(const FInstancedStruct& DataPayload)
{
	WidgetController->PassPayload(DataPayload);
	K2_SetupWidget(DataPayload);
}

void UFayeBaseWidget::NativeOnActivated()
{
	Super::NativeOnActivated();
	
	SetVisibility(ActivatedVisibility);
	SetFocus();
}

void UFayeBaseWidget::NativeOnDeactivated()
{
	Super::NativeOnDeactivated();
	
	SetVisibility(DeactivatedVisibility);
}

bool UFayeBaseWidget::NativeOnHandleBackAction()
{
	DeactivateWidget();
	return Super::NativeOnHandleBackAction();
}
