// © 2025 Naked People Team. All Rights Reserved.


#include "MissionGoal.h"

#include "GoalRestriction.h"

void UMissionGoal::Init(ANakedDesireCharacter* PlayerCharacter)
{
	Player = PlayerCharacter;

	for (const auto& Elem : Restrictions)
	{
		Elem->Init(Player);
		auto Handle = Elem->OnUpdate.AddUObject(this, &UMissionGoal::OnRestrictionUpdated);
		RestrictionUpdateHandles.Add(Handle);
	}
}

void UMissionGoal::Complete()
{
	if (!CheckRestrictions())
	{
		return;
	}
	
	IsCompleted = true;
	for (const auto& Restriction  : Restrictions)
	{
		Restriction->Stop();
		for (const auto& Handle : RestrictionUpdateHandles)
		{
			Restriction->OnUpdate.Remove(Handle);
		}
	}
	
	OnUpdate.Broadcast(this);
}

bool UMissionGoal::CheckRestrictions()
{
	if (IsCompleted)
	{
		return true;
	}

	for (const auto& Restriction : Restrictions)
	{
		if (!Restriction->GetIsSuccess())
		{
			return false;
		}
	}
	return true;
}

FText UMissionGoal::GetDescription() const
{
	return FText::GetEmpty();
}

void UMissionGoal::OnRestrictionUpdated(UGoalRestriction* Restriction)
{
	if (IsCompleted)
	{
		return;
	}

	OnUpdate.Broadcast(this);
}
