#pragma once

#include "CoreMinimal.h"
#include "WingServer.h"
#include "WingBasics.h"
#include "WingFetcher.h"
#include "Editor.h"
#include "Subsystems/AssetEditorSubsystem.h"
#include "Editor_OpenAsset.generated.h"


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

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

public:
	UPROPERTY(EditAnywhere, meta=(Description="Asset to open"))
	FString AssetPath;

	virtual void Register() override
	{
		UWingServer::AddHandler(this,
			TEXT("Open an asset in its editor and bring it to focus."));
	}
	virtual void Handle() override
	{
		WingFetcher F(WingOut::Stdout);
		UObject* Obj = F.Walk(AssetPath).Cast<UObject>();
		if (!Obj) return;

		UAssetEditorSubsystem* Sub = GEditor->GetEditorSubsystem<UAssetEditorSubsystem>();
		if (!Sub)
		{
			WingOut::Stdout.Print(TEXT("Error: AssetEditorSubsystem not available\n"));
			return;
		}

		if (Sub->OpenEditorForAsset(Obj))
			WingOut::Stdout.Printf(TEXT("Opened editor for %s\n"), *Obj->GetPathName());
		else
			WingOut::Stdout.Printf(TEXT("Error: Could not open editor for %s\n"), *Obj->GetPathName());
	}
};
