#include "HostWidgetController.h"
#include "AbilitySystem/GasaAbilitySystemComponent_Inlines.h"
#include "AbilitySystem/GasaAttributeSet.h"
#include "GameplayEffectTypes.h"
#include "GasaDevOptions.h"
#include "TaggedMessageRow.h"
using namespace Gasa;




#pragma region Attribute Changed Callbacks
// Attribute Changed Callbacks are generated by GasaGen/GasaGen_HostWidgetController.cpp

void UHostWidgetController::HealthChanged( FOnAttributeChangeData const& Attribute )
{
	Event_OnHealthChanged.Broadcast( Attribute.NewValue );
}

void UHostWidgetController::MaxHealthChanged( FOnAttributeChangeData const& Attribute )
{
	Event_OnMaxHealthChanged.Broadcast( Attribute.NewValue );
}

void UHostWidgetController::ManaChanged( FOnAttributeChangeData const& Attribute )
{
	Event_OnManaChanged.Broadcast( Attribute.NewValue );
}

void UHostWidgetController::MaxManaChanged( FOnAttributeChangeData const& Attribute )
{
	Event_OnMaxManaChanged.Broadcast( Attribute.NewValue );
}
#pragma endregion Attribute Changed Callbacks

void UHostWidgetController::OnEffectAppliedAssetTags( FGameplayTagContainer const& AssetTags )
{
	UDataTable* TaggedMessages = GetDevOptions()->TaggedMessageTable.Get();
	for ( FGameplayTag const& Tag : AssetTags )
	{
		FGameplayTag MessageTagCategory = FGameplayTag::RequestGameplayTag( FName( "Message" ) );
		if ( ! Tag.MatchesTag( MessageTagCategory ) )
			continue;

		FTaggedMessageRow* Row = TaggedMessages->FindRow<FTaggedMessageRow>( Tag.GetTagName(), TEXT( "" ) );
		OnTaggedAssetMessage.Broadcast( *Row );
	}
}

void UHostWidgetController::BroadcastInitialValues()
{
	// This function is managed by: GenGasa/GenGasa_HostWidgetController.cpp

	UGasaAttributeSet* GasaAttribs = Cast<UGasaAttributeSet>( Data.Attributes );
	if ( GasaAttribs )
	{
		Event_OnHealthChanged.Broadcast( GasaAttribs->GetHealth() );
		Event_OnMaxHealthChanged.Broadcast( GasaAttribs->GetMaxHealth() );
		Event_OnManaChanged.Broadcast( GasaAttribs->GetMana() );
		Event_OnMaxManaChanged.Broadcast( GasaAttribs->GetMaxMana() );
	}
	BindCallbacksToDependencies();
}

void UHostWidgetController::BindCallbacksToDependencies()
{
	// This function is managed by: GenGasa/GenGasa_HostWidgetController.cpp

	UGasaAbilitySystemComp* AbilitySystem = Cast<UGasaAbilitySystemComp>( Data.AbilitySystem );
	UGasaAttributeSet*      GasaAttribs   = Cast<UGasaAttributeSet>( Data.Attributes );

	FOnGameplayAttributeValueChange& HealthAttributeChangedDelegate =
	    AbilitySystem->GetGameplayAttributeValueChangeDelegate( GasaAttribs->GetHealthAttribute() );
	HealthAttributeChangedDelegate.AddUObject( this, &ThisClass::HealthChanged );

	FOnGameplayAttributeValueChange& MaxHealthAttributeChangedDelegate =
	    AbilitySystem->GetGameplayAttributeValueChangeDelegate( GasaAttribs->GetMaxHealthAttribute() );
	MaxHealthAttributeChangedDelegate.AddUObject( this, &ThisClass::MaxHealthChanged );

	FOnGameplayAttributeValueChange& ManaAttributeChangedDelegate = AbilitySystem->GetGameplayAttributeValueChangeDelegate( GasaAttribs->GetManaAttribute() );
	ManaAttributeChangedDelegate.AddUObject( this, &ThisClass::ManaChanged );

	FOnGameplayAttributeValueChange& MaxManaAttributeChangedDelegate =
	    AbilitySystem->GetGameplayAttributeValueChangeDelegate( GasaAttribs->GetMaxManaAttribute() );
	MaxManaAttributeChangedDelegate.AddUObject( this, &ThisClass::MaxManaChanged );

	AbilitySystem->Event_OnEffectAppliedAssetTags.AddUObject( this, &UHostWidgetController::OnEffectAppliedAssetTags );
}
