//----------------------------------------------------------------------//
// GameplayTasksComponent-related functions
//----------------------------------------------------------------------//
void UGameplayTask::ActivateInTaskQueue()
{
	switch(TaskState)
	{
	case EGameplayTaskState::Uninitialized:
		UE_VLOG(GetGameplayTasksComponent(), LogGameplayTasks, Error
			, TEXT("UGameplayTask::ActivateInTaskQueue Task %s passed for activation withouth having InitTask called on it!")
			, *GetName());
		break;
	case EGameplayTaskState::AwaitingActivation:
		PerformActivation();
		break;
	case EGameplayTaskState::Paused:
		// resume
		Resume();
		break;
	case EGameplayTaskState::Active:
		// nothing to do here
		break;
	case EGameplayTaskState::Finished:
		// If a task has finished, and it's being revived let's just treat the same as AwaitingActivation
		PerformActivation();
		break;
	default:
		checkNoEntry(); // looks like unhandled value! Probably a new enum entry has been added
		break;
	}
}
bool CFlowGraphDebugger::CheckForDelayedActivations()
{
	if (m_BreakpointHit)
		InvalidateBreakpoint();

	if (m_DelayedActivations.empty() && m_BreakPoint.type == eBT_Output_Without_Edges)
	{
		// we just stopped at an output port without edges and no delayed activations exist
		return true;
	}

	bool bPerform = true;

	while (bPerform)
	{
		if (m_DelayedActivations.empty())
		{
			break;
		}

		SBreakPoint delayedActivation = m_DelayedActivations.front();

		if (delayedActivation.addr.isOutput && delayedActivation.type == eBT_Output_Without_Edges)
		{
			// We have a delayed output port activation with NO edges, invalidate the breakpoint
			// and perform the activation directly
			bPerform = PerformActivation(delayedActivation.flowGraph, delayedActivation.addr, delayedActivation.value);

			m_DelayedActivations.pop_front();
		}
		else if (delayedActivation.type == eBT_Output)
		{
			// We have a delayed output port activation WITH edges, invalidate the breakpoint
			// and perform the activation to check if some edges result in a new breakpoint

			// ensure sorted edges so we activate in the same order as usual
			delayedActivation.flowGraph->EnsureSortedEdges();

			IFlowEdgeIteratorPtr pEdgeIter = delayedActivation.flowGraph->CreateEdgeIterator();
			if (pEdgeIter)
			{
				IFlowEdgeIterator::Edge edge;
				int edgeIndex = 0;
				while (pEdgeIter->Next(edge) && bPerform)
				{
					if (edge.fromNodeId == delayedActivation.addr.node && edge.fromPortId == delayedActivation.addr.port)
					{
						TryActivatePort(delayedActivation, edge, bPerform, edgeIndex);
						++edgeIndex;
					}
				}
			}

			m_DelayedActivations.pop_front();
		}
	}

	return bPerform;
}
void UGameplayTask::ReadyForActivation()
{
	if (TasksComponent.IsValid())
	{
		if (RequiresPriorityOrResourceManagement() == false)
		{
			PerformActivation();
		}
		else
		{
			TasksComponent->AddTaskReadyForActivation(*this);
		}
	}
	else
	{
		EndTask();
	}
}
bool CFlowGraphDebugger::TryActivatePort(SBreakPoint& breakpoint, IFlowEdgeIterator::Edge& edge, bool& bPerform, int edgeIndex)
{
	SFlowAddress fromAddr;
	fromAddr.node = edge.fromNodeId;
	fromAddr.port = edge.fromPortId;
	fromAddr.isOutput = true;

	SFlowAddress toAddr;
	toAddr.node = edge.toNodeId;
	toAddr.port = edge.toPortId;
	toAddr.isOutput = false;

	bPerform = PerformActivation(breakpoint.flowGraph, edgeIndex, fromAddr, toAddr, breakpoint.value);	

	if (bPerform)
	{
		CFlowData* pData = static_cast<CFlowData*>(breakpoint.flowGraph->GetNodeData(edge.toNodeId));

		if (pData)
		{
			pData->ActivateInputPort(edge.toPortId, breakpoint.value);
			breakpoint.flowGraph->ActivateNode(edge.toNodeId);

			string val;
			if (breakpoint.value.GetValueWithConversion(val))	
			{
				breakpoint.flowGraph->NotifyFlowNodeActivationListeners(fromAddr.node, fromAddr.port, toAddr.node, toAddr.port, val.c_str());
			}
		}
	}	
	else
	{
		m_RePerformActivation = false;
		return false;
	}

	return true;
}