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

#include "LODRecipeEditorToolbarExtender.h"
#include "LODRecipe.h"
#include "LODRecipeManager.h"

#include "Framework/Commands/UICommandList.h"
#include "FrameWork/MultiBox/MultiBoxBuilder.h"
#include "Framework/Application/SlateApplication.h"
#include "PropertyCustomizationHelpers.h"

#include "Engine/StaticMesh.h"
#include "Engine/SkeletalMesh.h"

#define LOCTEXT_NAMESPACE "LODRecipeEditor"

FLODRecipeEditorToolbarExtender::FLODRecipeEditorToolbarExtender(ULODRecipe* InLODRecipe, const FSimpleDelegate& OnAddMeshes)
{
	LODRecipe = InLODRecipe;
	OnAddMeshesDelegate = OnAddMeshes;

	TSharedRef<FUICommandList> DummyList = MakeShareable(new FUICommandList);
	this->AddToolBarExtension(
		"Asset",
		EExtensionHook::After,
		DummyList,
		FToolBarExtensionDelegate::CreateRaw(this, &FLODRecipeEditorToolbarExtender::CreateToolbarButtons)
	);
}

void FLODRecipeEditorToolbarExtender::CreateToolbarButtons(FToolBarBuilder& ToolbarBuilder)
{
	ToolbarBuilder.BeginSection("Settings");
	{
		ToolbarBuilder.AddComboButton(
			FUIAction(),
			FOnGetContent::CreateRaw(this, &FLODRecipeEditorToolbarExtender::CreateMeshPickerWidget),
			LOCTEXT("RecipeEditorToolbar_AddMesh", "Add Mesh"),
			LOCTEXT("RecipeEditorToolbar_AddMesh_ToolTip", "Add either static or skeletal mesh to the recipe"),
			FSlateIcon(FEditorStyle::GetStyleSetName(), "MaterialEditor.SetPreviewMeshFromSelection"),
			false);
	}
	ToolbarBuilder.EndSection();

	ToolbarBuilder.BeginSection("Build");
	{
		FUIAction GenerateLODRecipeAction(FExecuteAction::CreateRaw(this, &FLODRecipeEditorToolbarExtender::OnGenerateLODs));

		ToolbarBuilder.AddToolBarButton(
			GenerateLODRecipeAction,
			NAME_None,
			LOCTEXT("RecipeEditorToolbar_BuildLODs", "Build"),
			LOCTEXT("RecipeEditorToolbar_BuildLODs_ToolTip", "Generate all LODs"),
			FSlateIcon(/*FEditorStyle::GetStyleSetName(), "SimplygonIcon.Enabled"*/),
			EUserInterfaceActionType::ToggleButton);
	}
}

TSharedRef<SWidget> FLODRecipeEditorToolbarExtender::CreateMeshPickerWidget()
{
	TArray<const UClass*> AllowedClasses;
	AllowedClasses.Add(UStaticMesh::StaticClass());
	AllowedClasses.Add(USkeletalMesh::StaticClass());

	TSharedRef<SWidget> AssetPickerWidget = PropertyCustomizationHelpers::MakeAssetPickerWithMenu(FAssetData(),
		/*bAllowClear*/ false,
		/*bAllowCopyPaste*/ false,
		AllowedClasses,
		PropertyCustomizationHelpers::GetNewAssetFactoriesForClasses(AllowedClasses),
		FOnShouldFilterAsset(),
		FOnAssetSelected::CreateRaw(this, &FLODRecipeEditorToolbarExtender::OnMeshSelected),
		FSimpleDelegate::CreateRaw(this, &FLODRecipeEditorToolbarExtender::CloseMenu));

	TSharedPtr<FExtender> MenuExtender = MakeShareable(new FExtender);
	
	FMenuBuilder MenuBuilder(true, nullptr, MenuExtender);
	MenuBuilder.AddWidget(AssetPickerWidget, FText());

	return MenuBuilder.MakeWidget();
}

void FLODRecipeEditorToolbarExtender::OnMeshSelected(const FAssetData& AssetData)
{
	if (LODRecipe == nullptr)
		return;

	TSoftObjectPtr<UObject> Mesh(AssetData.ToSoftObjectPath());
	LODRecipe->AddMesh(Mesh);
	OnAddMeshesDelegate.ExecuteIfBound();
}

void FLODRecipeEditorToolbarExtender::CloseMenu()
{
	FSlateApplication::Get().DismissAllMenus();
}

void FLODRecipeEditorToolbarExtender::OnGenerateLODs()
{
	FLODRecipeManager::Get().Build(TArray<ULODRecipe*>{ LODRecipe });
}

#undef LOCTEXT_NAMESPACE
