#pragma once

#include "CoreMinimal.h"
#include "WingHandler.h"
#include "WingFetcher.h"
#include "WingToolMenu.h"
#include "WingServer.h"
#include "ToolMenus.h"
#include "MaterialGraph/MaterialGraphNode.h"
#include "GraphNode_ShowMenu.generated.h"


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

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

public:
	UPROPERTY(EditAnywhere, meta=(Description="Target node"))
	FString Node;

	virtual void Register() override
	{
		UWingServer::AddHandler(this,
			TEXT("Show context menu actions available for a node and its pins."));
	}
private:
	virtual void Handle() override
	{
		WingFetcher F;
		UEdGraphNode* NodeObj = F.Walk(Node).Cast<UEdGraphNode>();
		if (!NodeObj) return;

		if (Cast<UMaterialGraphNode>(NodeObj))
		{
			UWingServer::Printf(TEXT("Material graph nodes do not have usable context menus."));
			return;
		}
		FToolMenuContext Context;
		TArray<FToolMenuEntry> Entries = WingToolMenu::GetMenuItems(NodeObj, Context);
		for (FToolMenuEntry &Entry : Entries)
		{
			FString LabelText = Entry.Label.Get().ToString();
			UWingServer::Printf(TEXT("%s\n"), *LabelText);
		}
		if (Entries.IsEmpty()) UWingServer::Printf(TEXT("No selectable menu entries right now.\n"));
	}
};
