#pragma once

#include "CoreMinimal.h"
#include "WingServer.h"
#include "WingHandler.h"
#include "WingFetcher.h"
#include "WingUtils.h"
#include "WingMaterialParameter.h"
#include "Materials/MaterialInstanceConstant.h"
#include "MaterialTypes.h"
#include "MaterialInstance_DumpParameters.generated.h"


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

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

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

	virtual void Register() override
	{
		UWingServer::AddHandler(this,
			TEXT("List all parameters on a Material Instance, showing current values and which are overridden."));
	}
	virtual void Handle() override
	{
		WingFetcher F;
		UMaterialInstanceConstant* MI = F.Asset(MaterialInstance).Cast<UMaterialInstanceConstant>();
		if (!MI) return;

		auto AllParams = WingMaterialParameter::GetMaterialParameters(MI);

		// Overridden parameters first.
		bool bHasOverrides = false;
		for (auto& [Info, Meta] : AllParams)
		{
			if (!Meta.bOverride) continue;
			if (!bHasOverrides) { UWingServer::Print(TEXT("\nOverridden Parameters:\n")); bHasOverrides = true; }
			WingMaterialParameter::FormatMaterialParameter(Info, Meta);
		}

		// Inherited (non-overridden) parameters.
		bool bHasInherited = false;
		for (auto& [Info, Meta] : AllParams)
		{
			if (Meta.bOverride) continue;
			if (!bHasInherited) { UWingServer::Print(TEXT("\nInherited Parameters (not overridden):\n")); bHasInherited = true; }
			WingMaterialParameter::FormatMaterialParameter(Info, Meta);
		}
	}
};
