#pragma once

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


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

UCLASS()
class UWing_MaterialInstance_ClearParameter : 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;

	virtual void Register() override
	{
		UWingServer::AddHandler(this,
			TEXT("Remove a parameter override from a Material Instance, reverting it to the parent material's value."));
	}
	
	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;

		if (!FWingParameterEditor::RemoveOverride(ID, MI, WingOut::Stdout)) return;
		WingOut::Stdout.Printf(TEXT("Removed override\n"));
	}
};
