#pragma once

#include "CoreMinimal.h"
#include "WingServer.h"
#include "WingHandler.h"
#include "WingFetcher.h"
#include "WingUtils.h"
#include "WingGraphActions.h"
#include "EdGraph/EdGraph.h"
#include "EdGraph/EdGraphSchema.h"
#include "GraphNode_SearchTypes.generated.h"


// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------

UCLASS()
class UWing_GraphNode_SearchTypes : public UWingHandler
{
	GENERATED_BODY()

public:
	UPROPERTY(EditAnywhere, meta=(Description="Query string, can contain *"))
	FString Query;

	UPROPERTY(meta=(Optional, Description="Maximum number of results (default 50)"))
	int32 MaxResults = 50;

	UPROPERTY(EditAnywhere, meta=(Description="Target graph"))
	FString Graph;

	virtual void Register() override
	{
		UWingServer::AddHandler(this,
			TEXT("Search the action database for node types that can be spawned in a graph. "
			"Works with any graph type (Blueprint, Material, etc.). "
			"Returns full action names for use with GraphNode_Create."));
	}
	virtual void Handle() override
	{
		WingFetcher F;
		UEdGraph* TargetGraph = F.Walk(Graph).Cast<UEdGraph>();
		if (!TargetGraph) return;

		FWingGraphActions GraphActions(TargetGraph);
		TArray<FWingGraphAction*> Results = GraphActions.Search(Query, MaxResults, false);
		for (const FWingGraphAction* Action : Results)
		{
			UWingServer::Printf(TEXT("%s\n"), *Action->Name);
		}

		if (Results.Num() == 0)
		{
			UWingServer::Print(TEXT("No matching node types found.\n"));
		}
		else if (Results.Num() >= MaxResults)
		{
			UWingServer::Printf(TEXT("WARNING: Reached limit of %d results. You may specify MaxResults.\n"), MaxResults);
		}
	}
};
