#pragma once

#include "CoreMinimal.h"
#include "WingServer.h"
#include "WingBasics.h"
#include "WingFetcher.h"
#include "WingParameterEditor.h"
#include "Materials/MaterialInstanceConstant.h"
#include "MaterialInstance_SetParameter.generated.h"


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

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

public:
	UPROPERTY(EditAnywhere, meta=(Description="Target material instance"))
	FString MaterialInstance;

	UPROPERTY(EditAnywhere, meta=(Description="Parameter ID: 'Name', 'Layer:N:Name', or 'Blend:N:Name'"))
	FString Parameter;

	UPROPERTY(EditAnywhere, meta=(Description="Value to set (uses Unreal text format, e.g. '0.5' for scalar, '(R=1,G=0,B=0,A=1)' for vector)"))
	FString Value;

	virtual void Register() override
	{
		UWingServer::AddHandler(this,
			TEXT("Set a parameter override on a Material Instance."));
	}
	virtual void Handle() override
	{
		WingFetcher F(WingOut::Stdout);
		UMaterialInstanceConstant* MI = F.Asset(MaterialInstance).Cast<UMaterialInstanceConstant>();
		if (!MI) return;

		// Parse the parameter ID.
		FWingParameterEditor::Info ID;
		if (!FWingParameterEditor::ParseID(Parameter, ID, WingOut::Stdout))
			return;

		// Set the parameter
		if (!FWingParameterEditor::AddOverride(ID, MI, Value, WingOut::Stdout)) return;
		WingOut::Stdout.Printf(TEXT("Assigned.\n"));
	}
};
