#pragma once

#include "CoreMinimal.h"
#include "WingServer.h"
#include "WingHandler.h"
#include "WingTypes.h"
#include "Test_TypeToText.generated.h"


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

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

public:
	UPROPERTY(EditAnywhere, meta=(Description="The type name to parse, e.g. 'Array<Vector>'"))
	FString Input;

	virtual void Register() override
	{
		UWingServer::AddHandler(this,
			TEXT("Test the type parser by parsing a type name and dumping the resulting FEdGraphPinType."));
	}
	virtual void Handle() override
	{
		FEdGraphPinType PinType;
		UWingTypes::Requirements Require;
		Require.BlueprintType = false;
		Require.Blueprintable = false;
		Require.AllowContainer = true;

		bool OK = UWingTypes::TextToType(Input, PinType, Require);

		auto& Out = UWingServer::GetPrintBuffer();
		Out.Appendf(TEXT("ParseResult: %s\n"), OK ? TEXT("OK") : TEXT("FAILED"));
		Out.Appendf(TEXT("PinCategory: %s\n"), *PinType.PinCategory.ToString());
		Out.Appendf(TEXT("PinSubCategory: %s\n"), *PinType.PinSubCategory.ToString());
		Out.Appendf(TEXT("PinSubCategoryObject: %s\n"),
			PinType.PinSubCategoryObject.IsValid()
				? *PinType.PinSubCategoryObject->GetPathName()
				: TEXT("(none)"));
		Out.Appendf(TEXT("ContainerType: %d\n"), (int32)PinType.ContainerType);
		if (PinType.IsMap())
		{
			Out.Appendf(TEXT("ValueTerminalCategory: %s\n"), *PinType.PinValueType.TerminalCategory.ToString());
			Out.Appendf(TEXT("ValueTerminalSubCategory: %s\n"), *PinType.PinValueType.TerminalSubCategory.ToString());
			Out.Appendf(TEXT("ValueTerminalSubCategoryObject: %s\n"),
				PinType.PinValueType.TerminalSubCategoryObject.IsValid()
					? *PinType.PinValueType.TerminalSubCategoryObject->GetPathName()
					: TEXT("(none)"));
		}

		if (OK)
		{
			FString RoundTrip = UWingTypes::TypeToText(PinType);
			Out.Appendf(TEXT("TypeToText: %s\n"), RoundTrip.IsEmpty() ? TEXT("(empty)") : *RoundTrip);
		}
	}
};
