#pragma once

#include "CoreMinimal.h"
#include "Factories/Factory.h"
#include "WingServer.h"
#include "WingBasics.h"
#include "WingFactories.h"
#include "Factories/BlueprintFunctionLibraryFactory.h"
#include "Create_NoArgs.generated.h"

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

public:
	UPROPERTY(EditAnywhere, meta=(Description="Full asset path for the new asset (e.g. '/Game/MyFolder/MyAsset')"))
	FString Path;

	virtual void Register() override
	{
		TArray<UClass*> FactoryClasses;
		GetDerivedClasses(UFactory::StaticClass(), FactoryClasses);
		for (UClass* Class : FactoryClasses)
		{
			if (!WingFactories::CanCreate(Class)) continue;
			if (WingFactories::IsSpecialCase(Class)) continue;
			if (WingFactories::GetParameterNames(Class).Num() > 0) continue;

			FString FactoryName = WingFactories::DeriveFactoryName(Class);
			FString CommandName = FString::Printf(TEXT("Create_%s"), *FactoryName);
			FString Doc = FString::Printf(TEXT("Create a new %s asset."), *FactoryName);
			UWingServer::AddHandler(this, CommandName, nullptr, EWingHandlerKind::Create, Class, Doc);
		}

		UWingServer::AddHandler(this, TEXT("Create_BlueprintFunctionLibrary"),
			nullptr, EWingHandlerKind::Create,
			UBlueprintFunctionLibraryFactory::StaticClass(),
			TEXT("Create a blueprint function library"));

		UClass *BlueprintInterfaceFactory = FindObject<UClass>(nullptr, TEXT("/Script/UnrealEd.BlueprintInterfaceFactory"));
		if (BlueprintInterfaceFactory)
			UWingServer::AddHandler(this, TEXT("Create_BlueprintInterface"),
				nullptr, EWingHandlerKind::Create,
				BlueprintInterfaceFactory,
				TEXT("Create a blueprint interface"));
	}

	virtual void Handle() override
	{
		UClass* FactoryClass = Configuration->FactoryClass.Get();
		UFactory* Factory = NewObject<UFactory>(GetTransientPackage(), FactoryClass);
		UObject *Obj = WingFactories::CreateAsset(Path, Factory, nullptr, WingOut::Stdout);
		if (!Obj) return;
		WingOut::Stdout.Printf(TEXT("Created.\n"));
	}
};
