/**
 * Copyright 2022-2025 Theai, Inc. dba Inworld AI
 */
#include "GraphEditor/InworldGraphAssetEditor/InworldGraphEditorStyle.h"

#include "Interfaces/IPluginManager.h"
#include "Misc/Paths.h"
#include "Styling/SlateStyleRegistry.h"
#include "Styling/SlateTypes.h"

TSharedPtr<FSlateStyleSet> FInworldGraphEditorStyle::StyleSet = nullptr;
TSharedPtr<class ISlateStyle> FInworldGraphEditorStyle::Get()
{
	return StyleSet;
}

#define IMAGE_BRUSH(RelativePath, ...) FSlateImageBrush(StyleSet->RootToContentDir(RelativePath, TEXT(".png")), __VA_ARGS__)
#define BOX_BRUSH(RelativePath, ...) FSlateBoxBrush(StyleSet->RootToContentDir(RelativePath, TEXT(".png")), __VA_ARGS__)
#define BORDER_BRUSH(RelativePath, ...) FSlateBorderBrush(StyleSet->RootToContentDir(RelativePath, TEXT(".png")), __VA_ARGS__)
#define TTF_FONT(RelativePath, ...) FSlateFontInfo(StyleSet->RootToContentDir(RelativePath, TEXT(".ttf")), __VA_ARGS__)
#define OTF_FONT(RelativePath, ...) FSlateFontInfo(StyleSet->RootToContentDir(RelativePath, TEXT(".otf")), __VA_ARGS__)

void FInworldGraphEditorStyle::Initialize()
{
	// Const icon sizes
	const FVector2D Icon8x8(8.0f, 8.0f);
	const FVector2D Icon16x16(16.0f, 16.0f);
	const FVector2D Icon20x20(20.0f, 20.0f);
	const FVector2D Icon40x40(40.0f, 40.0f);
	const FVector2D Icon64x64(64.0f, 64.0f);

	if (StyleSet.IsValid())
	{
		return;
	}

	StyleSet = MakeShareable(new FSlateStyleSet("InworldGraphEditorStyle"));

	StyleSet->SetContentRoot(IPluginManager::Get().FindPlugin(TEXT("InworldRuntime"))->GetBaseDir());
	StyleSet->SetCoreContentRoot(FPaths::EngineContentDir() / TEXT("Slate"));

	StyleSet->Set("ClassIcon.InworldGraphData", new IMAGE_BRUSH("Resources/Icon16", Icon16x16));
	StyleSet->Set("ClassThumbnail.InworldGraphData", new IMAGE_BRUSH("Resources/Icon64", Icon64x64));

	StyleSet->Set("ClassIcon.InworldNode_Custom", new IMAGE_BRUSH("Resources/Icon16", Icon16x16));
	StyleSet->Set("ClassThumbnail.InworldNode_Custom", new IMAGE_BRUSH("Resources/Icon64node", Icon64x64));

	StyleSet->Set("ClassIcon.InworldEdge_WithCondition", new IMAGE_BRUSH("Resources/Icon16", Icon16x16));
	StyleSet->Set("ClassThumbnail.InworldEdge_WithCondition", new IMAGE_BRUSH("Resources/Icon64edge", Icon64x64));
	
	StyleSet->Set("Inworld.Graph.SubGraph", new IMAGE_BRUSH("Resources/SubGraph", Icon64x64));
	StyleSet->Set("Inworld.Graph.InOut", new IMAGE_BRUSH("Resources/InOutNode", FVector2D(24.f, 19.f)));
	StyleSet->Set("Inworld.Graph.Warning", new IMAGE_BRUSH("Resources/WarningIcon", FVector2D(24.f, 24.f)));
	StyleSet->Set("Inworld.Graph.Warning20", new IMAGE_BRUSH("Resources/WarningIcon", Icon20x20));
	StyleSet->Set("Inworld.Graph.Warning16", new IMAGE_BRUSH("Resources/WarningIcon", Icon16x16));
	StyleSet->Set("Inworld.Graph.SearchSubgraph", new IMAGE_BRUSH("Resources/SearchGlassSubgraph", Icon20x20));
	StyleSet->Set("Inworld.Graph.SearchGlass", new IMAGE_BRUSH("Resources/SearchGlass", Icon16x16));

	FSlateStyleRegistry::RegisterSlateStyle(*StyleSet.Get());
}

void FInworldGraphEditorStyle::Shutdown()
{
	if (StyleSet.IsValid())
	{
		FSlateStyleRegistry::UnRegisterSlateStyle(*StyleSet.Get());
		ensure(StyleSet.IsUnique());
		StyleSet.Reset();
	}
}

const FName& FInworldGraphEditorStyle::GetStyleSetName()
{
	return StyleSet->GetStyleSetName();
}

#undef IMAGE_BRUSH
#undef BOX_BRUSH
#undef BORDER_BRUSH
#undef TTF_FONT
#undef OTF_FONT
