// Copyright (c) Microsoft Corporation. 
// Licensed under the MIT license.

#include "SimplygonStyle.h"
#include "Styling/SlateStyle.h"
#include "Styling/SlateStyleRegistry.h"
#include "Styling/CoreStyle.h"
#include "Styling/SlateTypes.h"
#include "Framework/Application/SlateApplication.h"
#include "Slate/SlateGameResources.h"
#include "Interfaces/IPluginManager.h"

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

#define IMAGE_BRUSH_SVG(RelativePath, ...) FSlateVectorImageBrush(Style->RootToContentDir(RelativePath, TEXT(".svg")), __VA_ARGS__)

TSharedPtr<FSlateStyleSet> FSimplygonStyle::StyleSet = nullptr;

const FVector2D Icon16x16(16.0f, 16.0f);
const FVector2D Icon20x20(20.0f, 20.0f);
const FVector2D Icon40x40(40.0f, 40.0f);

const ISlateStyle& FSimplygonStyle::Get()
{
	return *StyleSet;
}

FName FSimplygonStyle::GetStyleSetName()
{
	static FName SimplygonStyleName(TEXT("SimplygonStyle"));
	return SimplygonStyleName;
}

TSharedRef< FSlateStyleSet > FSimplygonStyle::Create()
{
	TSharedRef< FSlateStyleSet > Style = MakeShareable(new FSlateStyleSet("SimplygonStyle"));

	FString ContentDir = IPluginManager::Get().FindPlugin("Simplygon")->GetBaseDir();
	Style->SetContentRoot(IPluginManager::Get().FindPlugin("Simplygon")->GetBaseDir() / TEXT("Resources"));

	Style->Set("Simplygon.Tabs.StandIn", new IMAGE_BRUSH_SVG(TEXT("SimplygonLogo"), Icon20x20));
	Style->Set("Simplygon.Tabs.SimplygonLogo", new IMAGE_BRUSH_SVG(TEXT("SimplygonLogo"), Icon20x20));
	Style->Set("SimplygonIcon.LogoIcon", new IMAGE_BRUSH_SVG("SimplygonLogo", Icon40x40));
	Style->Set("SimplygonIcon.Disabled", new IMAGE_BRUSH_SVG("SimplygonDisabled", Icon40x40));
	Style->Set("SimplygonIcon.Enabled", new IMAGE_BRUSH_SVG("SimplygonEnabled", Icon40x40));
	Style->Set("SimplygonIcon.Warn", new IMAGE_BRUSH_SVG("SimplygonWarn", Icon40x40));
	
	//Fonts
	Style->Set("Standin.HeaderFontStyle", DEFAULT_FONT("Bold", 10));

	Style->Set("ContentBrowser.TopBar.Font", FTextBlockStyle()
		.SetFont(DEFAULT_FONT("Bold", 10))
		.SetColorAndOpacity(FLinearColor(1.0f, 1.0f, 1.0f))
		.SetHighlightColor(FLinearColor(1.0f, 1.0f, 1.0f))
		.SetShadowOffset(FVector2D(1, 1))
		.SetShadowColorAndOpacity(FLinearColor(0, 0, 0, 0.9f)));

	return Style;
}

void FSimplygonStyle::Initialize()
{
	if (!StyleSet.IsValid())
	{
		StyleSet = Create();
		FSlateStyleRegistry::RegisterSlateStyle(*StyleSet);
	}
};

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

void FSimplygonStyle::ReloadTextures()
{
	if (FSlateApplication::IsInitialized())
	{
		FSlateApplication::Get().GetRenderer()->ReloadTextureResources();
	}
}

void FSimplygonStyle::Shutdown()
{
	FSlateStyleRegistry::UnRegisterSlateStyle(*StyleSet);
	ensure(StyleSet.IsUnique());
	StyleSet.Reset();
}
