#include "DialogueTextNode.h"
#include "DialogueTree.h"

#define LOCTEXT_NAMESPACE "UDialogueTextNode"

UDialogueTextNode::UDialogueTextNode()
{
#if WITH_EDITORONLY_DATA
	CompatibleGraphType = UDialogueTree::StaticClass();

	ContextMenuName = LOCTEXT("ConextMenuName", "Dialogue Node");
#endif
}

#if WITH_EDITOR

FText UDialogueTextNode::GetNodeTitle() const
{
	if (Text.IsEmpty()) return LOCTEXT("EmptyParagraph", "(Empty paragraph)");
	FString WrappedText = Text.ToString();
	WrappedText = WrappedText.TrimStartAndEnd();
	if (Text.ToString().Len() > 20)
	{
		WrappedText = "";
		int CharCount = 0;
		for (int i = 0; i < Text.ToString().Len(); i++)
		{
			if (CharCount > 20 && Text.ToString()[i] == ' ')
			{
				WrappedText += "\n";
				CharCount = 0;
			}
			else
			{
				WrappedText += Text.ToString()[i];
				CharCount++;
			}
		}
	}
	return FText::FromString(WrappedText);
}

void UDialogueTextNode::SetNodeTitle(const FText& NewTitle)
{
	Text = NewTitle;
}

FLinearColor UDialogueTextNode::GetBackgroundColor() const
{
	const UDialogueTree* DialogueTree = Cast<UDialogueTree>(GetGraph());

	if (DialogueTree == nullptr)
		return Super::GetBackgroundColor();

	switch (DialogueCharacterSpeaking)
	{
	case ECharacterSpeaking::Character1:
		return DialogueTree->Character1.DialogueNodeBgColor;
	case ECharacterSpeaking::Character2:
		return DialogueTree->Character2.DialogueNodeBgColor;
	case ECharacterSpeaking::Character3:
		return DialogueTree->Character3.DialogueNodeBgColor;
	case ECharacterSpeaking::Character4:
		return DialogueTree->Character4.DialogueNodeBgColor;
	default:
		return FLinearColor::Black;
	}
}

#endif

#undef LOCTEXT_NAMESPACE
