// Copyright (c) Microsoft Corporation. 
// Licensed under the MIT license.
#ifdef IMPOSTER_BAKER
#include "ImpostorGeneratorHelperActorCustomizations.h"
#include "DetailCategoryBuilder.h"
#include "DetailLayoutBuilder.h"
#include "DetailWidgetRow.h"
#include "ImpostorGeneratorHelperActor.h"
#include "Widgets/Input/SButton.h"
#include "Widgets/SBoxPanel.h"
#include "Algo/AnyOf.h"
#include "EditorCategoryUtils.h"
#include "CustomizationHelper.h"
#include "SimplygonEditorModule.h"

#define LOCTEXT_NAMESPACE "ImposterGeneratorHelperActorCustomization" 


FImpostorGeneratorHelperActorCustomization::FImpostorGeneratorHelperActorCustomization()
	:CategoriesToHide({ "Rendering"
		, "Physics"
		, "Mobility"
		, "LOD"
		, "Activation"
		, "Collision"
		, "Cooking"
		, "CameraSettings"
		, "CameraOptions"
		, "Camera"
		, "PostProcess"
		, "Lighting"
		, "HLOD"
		, "TextureStreaming"
		, "Mobile"
		, "Navigation"
		, "VirtualTexture"
		, "Activation"
		, "Replication"
		, "Input"
		, "Tags"
		, "Actor"
		, "AssetUserData"
		, "ArrowComponent" })
{
}

TSharedRef<IDetailCustomization> FImpostorGeneratorHelperActorCustomization::MakeInstance()
{
	return MakeShareable(new FImpostorGeneratorHelperActorCustomization);
}

static void HideSubCategories(IDetailLayoutBuilder& DetailBuilder, const FName& CategoryName)
{
	IDetailCategoryBuilder& TransfromCategory = DetailBuilder.EditCategory(CategoryName);

	
	TArray<TSharedRef<IPropertyHandle>> Properties;
	TransfromCategory.GetDefaultProperties(Properties,false,true);
	for (TSharedRef<IPropertyHandle> Prop : Properties)
	{
		if (!Prop->GetDefaultCategoryName().IsEqual(CategoryName))
			Prop->MarkHiddenByCustomization();
	}
}




void FImpostorGeneratorHelperActorCustomization::CustomizeDetails(IDetailLayoutBuilder& DetailBuilder)
{
	
	FCustomizationHelpers::HideCategories(DetailBuilder, CategoriesToHide);
	

	IDetailCategoryBuilder& Category = DetailBuilder.EditCategory("Asset Actions", FText::GetEmpty(), ECategoryPriority::Important);
	
	Category.AddCustomRow(LOCTEXT("ImposterCommand_Update", "Update"))
			.WholeRowContent()
			[
				SNew(SButton)
				.Text(LOCTEXT("Imposter_Update", "Update"))
				.OnClicked(FOnClicked::CreateStatic(&FImpostorGeneratorHelperActorCustomization::HandleUpdate, &DetailBuilder))
			];

	Category.AddCustomRow(LOCTEXT("ImposterCommand_Build", "Build"))
		.WholeRowContent()
		[
			SNew(SButton)
			.Text(LOCTEXT("Imposter_Build", "Build"))
		.OnClicked(FOnClicked::CreateStatic(&FImpostorGeneratorHelperActorCustomization::HandleBuild, &DetailBuilder))
		];

	
	

}

FReply FImpostorGeneratorHelperActorCustomization::HandleUpdate(IDetailLayoutBuilder* DetailBuilder)
{
	TArray<AImpostorGeneratorHelperActor*> Actors = FCustomizationHelpers::GetObjectBeingCustomized< AImpostorGeneratorHelperActor>(DetailBuilder);
	for (AImpostorGeneratorHelperActor* Actor : Actors) {
		 if (Actor) {
			 Actor->Update();
		 }
	}
	return FReply::Handled();
}

FReply FImpostorGeneratorHelperActorCustomization::HandleBuild(IDetailLayoutBuilder* DetailBuilder)
{
	TArray<AImpostorGeneratorHelperActor*> Actors = FCustomizationHelpers::GetObjectBeingCustomized< AImpostorGeneratorHelperActor>(DetailBuilder);
	for (AImpostorGeneratorHelperActor* Actor : Actors) {
		if (Actor) {
			Actor->Update();
			ISimplygonEditorModule& SimplygonEditorModule = FModuleManager::LoadModuleChecked<ISimplygonEditorModule>("SimplygonEditor");
			if (Actor->ActorsToBake.Num() > 0 && Actor->IsValidPipeline())
			{
				SimplygonEditorModule.BakeImpostors(Actor);
			}
			
		}
	}
	return FReply::Handled();
}

#undef LOCTEXT_NAMESPACE 
#endif