#pragma once

#include "CoreMinimal.h"
#include "WingHandler.h"
#include "WingServer.h"
#include "WingFetcher.h"
#include "WingVariables.h"
#include "Variables_Create.generated.h"


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

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

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

	UPROPERTY(meta=(Optional, Description="Blueprint variables, one per line"))
	FString BlueprintVariables;

	UPROPERTY(meta=(Optional, Description="Input variables, one per line"))
	FString InputVariables;

	UPROPERTY(meta=(Optional, Description="Output variables, one per line"))
	FString OutputVariables;

	UPROPERTY(meta=(Optional, Description="Local variables, one per line"))
	FString LocalVariables;

	virtual void Register() override
	{
		UWingServer::AddHandler(this,
			TEXT("Add new variables. Format: 'type name (flags) = default', one per line."));
	}
	virtual void Handle() override
	{
		WingFetcher F;
		UObject* Obj = F.Walk(Object).Cast<UObject>();
		if (!Obj) return;

		WingVariables Vars;
		if (!Vars.SetBackingStore(Obj)) return;
		if (!Vars.BlueprintVariables.ParseString(BlueprintVariables)) return;
		if (!Vars.InputVariables.ParseString(InputVariables)) return;
		if (!Vars.OutputVariables.ParseString(OutputVariables)) return;
		if (!Vars.LocalVariables.ParseString(LocalVariables)) return;
		if (!Vars.Check()) return;
		if (!Vars.Create()) return;
		UWingServer::Printf(TEXT("Success.\n"));
	}
};
