#pragma once

#include "CoreMinimal.h"
#include "WingServer.h"
#include "WingBasics.h"
#include "WingWidgets.h"
#include "Widget_SearchTypes.generated.h"


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

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

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

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

	virtual void Register() override
	{
		UWingServer::AddHandler(this,
			TEXT("Search for widget types that can be added to a Widget Blueprint. "
			"Returns names for use with Widget_Add."));
	}
	virtual void Handle() override
	{
		WingWidgets Widgets;
		for (const FString& Query : Queries.Argv)
		{
			WingOut::Stdout.Printf(TEXT("\n=== %s ===\n\n"), *Query);
			TArray<WingWidgets::Type> Results = Widgets.Search(Query, MaxResults, false);
			for (const WingWidgets::Type& Entry : Results)
			{
				WingOut::Stdout.Printf(TEXT("%s\n"), *Entry.MenuName);
			}

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