#pragma once

#include "CoreMinimal.h"
#include "WingHandler.h"
#include "WingFetcher.h"
#include "WingServer.h"
#include "WingTypes.h"
#include "WingProperty.h"
#include "WingManual.h"
#include "ShowCommands.generated.h"

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

public:
	UPROPERTY(meta=(Optional, Description="Substring filter for command names"))
	FString Query;

	UPROPERTY(meta=(Optional, Description="If true, return full details including parameter types and descriptions"))
	bool Verbose = false;

	UPROPERTY(meta=(Optional, Description="Kind of command: Normal, or Create"))
	EWingHandlerKind Kind = EWingHandlerKind::Normal;

	virtual void Register() override
	{
		UWingServer::AddHandler(this,
			TEXT("List all available commands with their descriptions."));
	}
	virtual void Handle() override
	{
		FString QueryLower = Query.ToLower();
		FString PrevGroup;

		for (const FWingHandlerConfig& H : UWingServer::AllHandlers())
		{
			if (H.Kind != Kind) continue;
			if (!H.Name.ToLower().Contains(QueryLower)) continue;

			// Blank line between groups
			if (!Verbose)
			{
				FString Group = WingUtils::GetHandlerGroup(H.Class.Get());
				if (Group != PrevGroup)
				{
					if (!PrevGroup.IsEmpty())
						UWingServer::Print(TEXT("\n"));
					PrevGroup = Group;
				}
			}

			if (Verbose)
			{
				WingManual::PrintHandlerHelp(H);
			}
			else
			{
				WingManual::PrintHandlerPrototype(H);
			}
		}
		if (Kind == EWingHandlerKind::Normal)
		{
			UWingServer::Printf(TEXT(
				"\n"
				"You can also use ShowCommands with Kind=Create to see commands that create new assets.\n"
				"\n"));
		}
	}
};
