#pragma once

#include "CoreMinimal.h"
#include "WingServer.h"
#include "WingHandler.h"
#include "WingProperty.h"
#include "WingFactories.h"
#include "SysInfo_Factories.generated.h"


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

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

public:
	virtual void Register() override
	{
		UWingServer::AddHandler(this,
			TEXT("Sysinfo commands are intended for the human who "
			"is developing this plugin, they help him to understand. "
			"unreal's internals better."));
	}

	virtual void Handle() override
	{
		TArray<UClass*> FactoryClasses;
		GetDerivedClasses(UFactory::StaticClass(), FactoryClasses);

		TArray<FString> Results;

		for (UClass *Factory : FactoryClasses)
		{
			if (Factory->HasAnyClassFlags(CLASS_Abstract)) continue;
			UFactory *CDO = Factory->GetDefaultObject<UFactory>();
			if (!CDO->CanCreateNew()) continue;
			if (!CDO->ShouldShowInNewMenu()) continue;
			TArray<FName> Params = FWingProperty::GetNames(Factory, CPF_Edit);
			TStringBuilder<512> Line;
			Line.Appendf(TEXT("%2d %s  "), Params.Num(), *Factory->GetName());
			for (const FName& Prop : Params)
			{
				Line.Appendf(TEXT(" %s"), *Prop.ToString());
			}
			Results.Add(Line.ToString());
		}
		Results.Sort();
		for (const FString &Line : Results)
		{
			UWingServer::Printf(TEXT("%s\n"), *Line);
		}
	}
};
