// ©2023 Batty Bovine Productions, LLC. All Rights Reserved.

#include "EdComboActionGraphNode.h"

#include "Ed/EdComboActionGraph.h"
#include "Helpers/ComboActionEditorBFC.h"
#include "Nodes/ComboActionGraphNode.h"
#include "Settings/ComboActionGraphEditorSettings.h"

#define LOCTEXT_NAMESPACE "UEdComboActionGraphNode"

DEFINE_LOG_CATEGORY(LogEdComboActionGraphNode);


UEdComboActionGraphNode::UEdComboActionGraphNode()
{
	bCanRenameNode = true;

	bAllowCopy = true;
	bAllowDelete = true;
	bAllowDuplicate = true;
	bAllowPaste = true;
}

void UEdComboActionGraphNode::SetComboActionGraphNode(UComboActionGraphNode *NewNode)
{
	this->ComboActionGraphNode = NewNode;

	if (this->ComboActionGraphNode)
	{
		this->bAllowCopy = this->ComboActionGraphNode->bAllowCopy;
		this->bAllowDelete = this->ComboActionGraphNode->bAllowDelete;
		this->bAllowDuplicate = this->ComboActionGraphNode->bAllowPaste;
		this->bAllowPaste = this->ComboActionGraphNode->bAllowPaste;
	}
}

UEdComboActionGraph *UEdComboActionGraphNode::GetEdComboActionGraph() const
{
	return Cast<UEdComboActionGraph>(this->GetGraph());
};

void UEdComboActionGraphNode::AllocateDefaultPins()
{
	if (this->ComboActionGraphNode == nullptr)
	{
		UE_LOG(LogEdComboActionGraphNode, Error, TEXT("[AllocateDefaultPins] Cannot find Owning Graph Node!"));
		return;
	}
		
	if (this->ComboActionGraphNode->bAllowInputNodes)
	{
		this->CreatePin(EGPD_Input, "MultipleNodes", FName(), TEXT("In"));
	}

	if (this->ComboActionGraphNode->bAllowOutputNodes)
	{
		this->CreatePin(EGPD_Output, "MultipleNodes", FName(), TEXT("Out"));
	}
}

FText UEdComboActionGraphNode::GetNodeTitle(ENodeTitleType::Type TitleType) const
{
	return UComboActionEditorBFC::GetNodeTitle(this->ComboActionGraphNode);
}

void UEdComboActionGraphNode::PrepareForCopying()
{
	this->ComboActionGraphNode->Rename(nullptr, this, REN_DontCreateRedirectors | REN_DoNotDirty);
}

void UEdComboActionGraphNode::AutowireNewNode(UEdGraphPin* FromPin)
{
	Super::AutowireNewNode(FromPin);

	if (FromPin != nullptr)
	{
		if (GetSchema()->TryCreateConnection(FromPin, GetInputPin()))
		{
			FromPin->GetOwningNode()->NodeConnectionListChanged();
		}
	}
}

FLinearColor UEdComboActionGraphNode::GetBackgroundColor() const
{
	// Getting Node colour based on the Settings if any found, otherwise use this logic
	if (UComboActionGraphEditorSettings *GraphEditorSettings = GetMutableDefault<UComboActionGraphEditorSettings>())
	{
		FLinearColor ReturnColour;
		if (GraphEditorSettings->FindNodeBackgroundColourOverride(this->ComboActionGraphNode->GetClass(), ReturnColour))
		{
			return ReturnColour;
		}
	}
	
	return this->ComboActionGraphNode ? this->ComboActionGraphNode->GetBackgroundColor() : FLinearColor::Black;
}

UEdGraphPin* UEdComboActionGraphNode::GetInputPin() const
{
	return Pins[0];
}

UEdGraphPin* UEdComboActionGraphNode::GetOutputPin() const
{
	if (Pins.IsValidIndex(1))
	{
		return Pins[1];
	}

	return Pins[0];
}

bool UEdComboActionGraphNode::CanUserDeleteNode() const
{
	if(!Super::CanUserDeleteNode())
	{
		return false;
	}

	if (this->ComboActionGraphNode)
	{
		return this->ComboActionGraphNode->bAllowDelete;
	}	
	return bAllowDelete;
}

bool UEdComboActionGraphNode::CanDuplicateNode() const
{
	if(!Super::CanUserDeleteNode())
	{
		return false;
	}

	if (this->ComboActionGraphNode)
	{
		return this->ComboActionGraphNode->bAllowCopy;
	}
	
	return bAllowCopy;
}

bool UEdComboActionGraphNode::CanUserPasteNodes() const
{
	if (this->ComboActionGraphNode)
	{
		return this->ComboActionGraphNode->bAllowPaste;
	}

	return bAllowPaste;
}

FText UEdComboActionGraphNode::GetTooltipText() const
{
	if (this->ComboActionGraphNode)
	{
		return this->ComboActionGraphNode->GetNodeTooltipText();
	}
	
	return NSLOCTEXT("UEdComboActionGraphNode", "DefaultToolTip", "Combo Action Node");
}

FSlateIcon UEdComboActionGraphNode::GetIconAndTint(FLinearColor& OutColor) const
{
	static const FSlateIcon Icon = FSlateIcon(FComboInputSlateStyle::GetAppStyleSetName(), "MDSStyleSet.Node.Icon.small");
	OutColor = this->ComboActionGraphNode->GetBackgroundColor();
	return Icon;
}

void UEdComboActionGraphNode::PostEditUndo()
{
	Super::PostEditUndo();
}

void UEdComboActionGraphNode::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
	Super::PostEditChangeProperty(PropertyChangedEvent);
}

#undef LOCTEXT_NAMESPACE
