#pragma once

#include "CoreMinimal.h"
#include "WingServer.h"
#include "WingBasics.h"
#include "WingFetcher.h"
#include "WingGraphActions.h"
#include "EdGraph/EdGraph.h"
#include "GraphNode_SearchTypes.generated.h"


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

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

public:
	UPROPERTY(EditAnywhere, meta=(Description="Target graph"))
	FString GraphPath;

	UPROPERTY(EditAnywhere, meta=(Description="Maximum number of results per query"))
	int32 MaxResults = 50;

	UPROPERTY(EditAnywhere, meta=(Description="Query strings; each may contain * wildcards"))
	FWingRestOfArgv Queries;

	virtual void Register() override
	{
		UWingServer::AddHandler(this,
			TEXT("Search for node types that can be spawned in a graph. "
				"Pass a string returned by this function to GraphNode_Add."));
	}
	virtual void Handle() override
	{
		WingFetcher F(WingOut::Stdout);
		UEdGraph* TargetGraph = F.Walk(GraphPath).Cast<UEdGraph>();
		if (!TargetGraph) return;

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

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