#include "DialogueChoiceNode.h"

#include "DialogueEdge.h"
#include "DialogueTree.h"

#define LOCTEXT_NAMESPACE "UDialogueChoiceNode"

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

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

#if WITH_EDITOR

FText UDialogueChoiceNode::GetNodeTitle() const
{
	bool bHasValidNumberOfChoices = false;
	if (Choices.Num() == ChildrenNodes.Num()) bHasValidNumberOfChoices = true;
	for (int i = 0; i < Edges.Num(); i++)
	{
		if (!bHasValidNumberOfChoices)
		{
			Cast<UDialogueEdge>(Edges[ChildrenNodes[i]])->EdgeColour = FLinearColor::Red;
			Cast<UDialogueEdge>(Edges[ChildrenNodes[i]])->SetNodeTitle(FText::FromString("INVALID"));
		}
		else if (Choices[i].Len() > 15)
		{
			FString Substring = Choices[i].Left(15);
			Substring.Append("...");
			Cast<UDialogueEdge>(Edges[ChildrenNodes[i]])->SetNodeTitle(FText::FromString(Substring));
		}
		else
		{
			Cast<UDialogueEdge>(Edges[ChildrenNodes[i]])->SetNodeTitle(FText::FromString(Choices[i]));
			Cast<UDialogueEdge>(Edges[ChildrenNodes[i]])->EdgeColour = FLinearColor::White;
		}
	}
	const FText ChoiceNodeTitle = NodeTitle.IsEmpty() ? LOCTEXT("Choice Node", "Choice Node") : NodeTitle;
	return FText::Format(LOCTEXT("Choice Node Title", "{0} [{1}]"), ChoiceNodeTitle, FText::AsNumber(Choices.Num()));
}

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

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

	return FLinearColor(1.f, 0.5f, 0.f, 1.f);
}

#endif

#undef LOCTEXT_NAMESPACE
