#pragma once

#include "CoreMinimal.h"
#include "WingServer.h"
#include "WingBasics.h"
#include "WingFetcher.h"
#include "WingProperty.h"
#include "WingUtils.h"
#include "Details_Set.generated.h"

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

public:
	UPROPERTY(EditAnywhere, meta=(Description="Target object"))
	FString ObjectPath;

	UPROPERTY(EditAnywhere, meta=(Description="Property name"))
	FString Property;

	UPROPERTY(EditAnywhere, meta=(Description="New value in Unreal text format"))
	FString Value;

	virtual void Register() override
	{
		UWingServer::AddHandler(this,
			TEXT("Set a single editable property. Value uses Unreal text format."));
	}

	virtual void Handle() override
	{
		WingFetcher F(WingOut::Stdout);
		UObject* Obj = F.Walk(ObjectPath).Cast<UObject>();
		if (!Obj) return;

		TArray<FWingProperty> Props = FWingProperty::GetDetails(Obj, true);
		FWingProperty* P = WingUtils::FindOneWithExternalID(Property, Props, TEXT("Property"), WingOut::Stdout);
		if (!P) return;
		
		if (P->SetText(Value, WingOut::Stdout))
			WingOut::Stdout.Print(TEXT("OK\n"));
	}
};
