#pragma once

#include "CoreMinimal.h"
#include "WingServer.h"
#include "WingBasics.h"
#include "WingFetcher.h"
#include "WingProperty.h"
#include "Details_Dump.generated.h"

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

public:
	UPROPERTY(EditAnywhere, meta=(Description="Target object"))
	FString ObjectPath;

	virtual void Register() override
	{
		UWingServer::AddHandler(this,
			TEXT("Test handler: dump properties using FWingProperty."));
	}

	virtual void Handle() override
	{
		WingFetcher F(WingOut::Stdout);
		UObject* Target = F.Walk(ObjectPath).Cast<UObject>();
		if (!Target) return;

		TArray<FWingProperty> Props = FWingProperty::GetDetails(Target, false);

		// Group by category, preserving within-category order.
		TSortedMap<FString, TArray<FWingProperty>> Categories;
		for (const FWingProperty& P : Props)
		{
			FString Category = P.Prop->GetMetaData(TEXT("Category"));
			if (Category.IsEmpty()) Category = TEXT("Unclassified");
			Categories.FindOrAdd(Category).Add(P);
		}

		for (const auto& Pair : Categories)
		{
			WingOut::Stdout.Printf(TEXT("\n%s:\n"), *Pair.Key);
			for (const FWingProperty& P : Pair.Value)
				P.Print(WingOut::Stdout);
		}
	}
};
