#pragma once

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


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

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

public:
	virtual void Register() override
	{
		UWingServer::AddHandler(this,
			TEXT("List all currently open asset editors, showing which has focus and whether they have unsaved changes."));
	}
	virtual void Handle() override
	{
		UAssetEditorSubsystem* Sub = GEditor->GetEditorSubsystem<UAssetEditorSubsystem>();
		if (!Sub)
		{
			WingOut::Stdout.Print(TEXT("Error: AssetEditorSubsystem not available\n"));
			return;
		}

		TArray<UObject*> EditedAssets = Sub->GetAllEditedAssets();
		if (EditedAssets.IsEmpty())
		{
			WingOut::Stdout.Print(TEXT("No asset editors are open.\n"));
			return;
		}

		for (UObject* Asset : EditedAssets)
		{
			bool bDirty = Asset->GetOutermost()->IsDirty();

			WingOut::Stdout.Printf(TEXT("  %s%s\n"),
				bDirty ? TEXT("[unsaved] ") : TEXT(""),
				*Asset->GetPathName());
		}
	}
};
