#pragma once

#include "CoreMinimal.h"
#include "WingBasics.h"
#include "WingServer.h"
#include "WingFetcher.h"
#include "WingVariables.h"
#include "Variables_Add.generated.h"


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

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

public:
	UPROPERTY(EditAnywhere, meta=(Description="Path to a blueprint, graph, or custom event node"))
	FString ObjectPath;

	UPROPERTY(EditAnywhere, meta=(Description="Variable descriptions"))
	FWingRestOfArgv Variables;

	virtual void Register() override
	{
		UWingServer::AddHandler(this,
			TEXT("Add variables to a blueprint, function graph, "
			"macro graph, event dispatcher graph, or custom event node. "
			"Each variable must be expressed as: 'kind type name (flags) = default'. "
			"Kind can be blueprint, input, output, or local."));
	}
	virtual void Handle() override
	{
		WingFetcher F(WingOut::Stdout);
		UObject* Obj = F.Walk(ObjectPath).Cast<UObject>();
		if (!Obj) return;

		WingVariables Vars;
		if (!Vars.SetBackingStore(Obj, WingOut::Stdout)) return;
		if (!Vars.Parse(Variables.Argv, false, WingOut::Stdout)) return;
		if (!Vars.Check(WingOut::Stdout)) return;
		if (!Vars.Create(WingOut::Stdout)) return;
		WingOut::Stdout.Printf(TEXT("Success.\n"));
	}
};
