#pragma once

#include "CoreMinimal.h"
#include "WingBasics.h"
#include "WingServer.h"
#include "WingFetcher.h"
#include "WingUtils.h"
#include "EdGraph/EdGraphPin.h"
#include "EdGraphSchema_K2.h"
#include "MaterialGraph/MaterialGraphSchema.h"
#include "GraphNode_SetDefault.generated.h"


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

public:
	UPROPERTY(EditAnywhere, meta=(Description="Target node"))
	FString NodePath;

	UPROPERTY(EditAnywhere, meta=(Description="Pin or property name"))
	FString Name;

	UPROPERTY(EditAnywhere, meta=(Description="New default value"))
	FString Value;

	virtual void Register() override
	{
		UWingServer::AddHandler(this,
			TEXT("Set the default value of input pins or material expression properties on nodes."));
	}
	// -----------------------------------------------------------------------
	// K2 graphs: set pin default values.
	// -----------------------------------------------------------------------

	void HandleK2(UEdGraphNode* FoundNode, const UEdGraphSchema_K2* K2Schema)
	{
		UEdGraphPin* Pin = WingUtils::CheckGetPin(FoundNode, FName(*Name), WingOut::Stdout);
		if (!Pin) return;

		if (Pin->Direction != EGPD_Input)
		{
			WingOut::Stdout.Printf(TEXT("error: %s is an output pin\n"), *WingUtils::FormatName(Pin));
			return;
		}

		FString UseDefaultValue;
		TObjectPtr<UObject> UseDefaultObject = nullptr;
		FText UseDefaultText;
		K2Schema->GetPinDefaultValuesFromString(Pin->PinType, FoundNode, Value, UseDefaultValue, UseDefaultObject, UseDefaultText, false);
		FString Error = K2Schema->IsPinDefaultValid(Pin, UseDefaultValue, UseDefaultObject, UseDefaultText);
		if (!Error.IsEmpty())
		{
			WingOut::Stdout.Printf(TEXT("error: %s: %s\n"), *WingUtils::FormatName(Pin), *Error);
			return;
		}
		UWingServer::AddTouchedObject(FoundNode);
		K2Schema->TrySetDefaultValue(*Pin, Value);
	}

	// -----------------------------------------------------------------------
	// Material graphs: set material expression properties.
	// -----------------------------------------------------------------------

	void HandleMaterial(UEdGraphNode* FoundNode)
	{
		TArray<FWingProperty> All = FWingProperty::GetDetails(FoundNode, true);
		FWingProperty *P = WingUtils::FindOneWithExternalID(Name, All, TEXT("Property"), WingOut::Stdout);
		if (!P) return;

		UWingServer::AddTouchedObject(FoundNode);

		if (!P->SetText(Value, WingOut::Stdout))
			return;
	}

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

	virtual void Handle() override
	{
		WingFetcher F(WingOut::Stdout);
		UEdGraphNode* FoundNode = F.Walk(NodePath).Cast<UEdGraphNode>();
		if (!FoundNode) return;

		const UEdGraphSchema* Schema = FoundNode->GetGraph()->GetSchema();
		const UEdGraphSchema_K2* K2Schema = Cast<UEdGraphSchema_K2>(Schema);
		const UMaterialGraphSchema* MGSchema = Cast<UMaterialGraphSchema>(Schema);

		if (!K2Schema && !MGSchema)
		{
			WingOut::Stdout.Printf(TEXT("error: unsupported graph schema %s\n"), *Schema->GetClass()->GetName());
			return;
		}

		if (K2Schema) HandleK2(FoundNode, K2Schema);
		else if (MGSchema) HandleMaterial(FoundNode);

		WingOut::Stdout.Printf(TEXT("Done.\n"));
	}
};
