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

#include "LODRecipeMiscSettingsCustomization.h"
#include "LODRecipeMiscSettings.h"
#include "LODRecipe.h"
#include "Engine/SkeletalMesh.h"
#include "SAllowMorphTargetsWidget.h"

#include "IDetailGroup.h"
#include "DetailWidgetRow.h"
#include "IDetailChildrenBuilder.h"
#include "IDetailPropertyRow.h"

TSharedRef<IPropertyTypeCustomization> FLODRecipeMiscSettingsCustomization::MakeInstance()
{
	return MakeShareable(new FLODRecipeMiscSettingsCustomization);
}

void FLODRecipeMiscSettingsCustomization::CustomizeHeader(TSharedRef<class IPropertyHandle> PropertyHandle, FDetailWidgetRow& HeaderRow, IPropertyTypeCustomizationUtils& CustomizationUtils)
{
	HeaderRow.
		NameContent()
		[
			PropertyHandle->CreatePropertyNameWidget()
		]
		.ValueContent()
		[
			PropertyHandle->CreatePropertyValueWidget()
		];
}

void FLODRecipeMiscSettingsCustomization::CustomizeChildren(TSharedRef<class IPropertyHandle> StructPropertyHandle, IDetailChildrenBuilder& ChildBuilder, IPropertyTypeCustomizationUtils& StructCustomizationUtils)
{
	uint32 NumChildren;
	StructPropertyHandle->GetNumChildren(NumChildren);
	TMap<FName, TSharedPtr< IPropertyHandle > > PropertyHandles;
	TMap<FName, TSharedPtr< IPropertyHandle > > AdvancedPropertyHandles;
	for (uint32 ChildIndex = 0; ChildIndex < NumChildren; ++ChildIndex)
	{
		TSharedRef<IPropertyHandle> ChildHandle = StructPropertyHandle->GetChildHandle(ChildIndex).ToSharedRef();
		const FName PropertyName = ChildHandle->GetProperty()->GetFName();

		PropertyHandles.Add(PropertyName, ChildHandle);
	}

	// Hide properties
	// Remapping morph targets are already handled in a separate tab
	TSharedPtr<IPropertyHandle> RemoveMorphTargets = PropertyHandles.FindAndRemoveChecked(GET_MEMBER_NAME_CHECKED(FLODRecipeMiscSettings, MorphTargetsToRemove));
	TSharedPtr<IPropertyHandle> AllowMorphTargets = PropertyHandles.FindAndRemoveChecked(GET_MEMBER_NAME_CHECKED(FLODRecipeMiscSettings, bApplyMorphTargets));

	TArray<UObject*> OutersList;
	StructPropertyHandle->GetOuterObjects(OutersList);
	if (OutersList.Num() == 1)
	{
		TWeakObjectPtr<ULODRecipe> LODRecipeBeingCustomized = Cast< ULODRecipe >(OutersList[0]);
		/*ChildBuilder.AddCustomRow(FText::FromString(TEXT("Remove Morph Targets")))
		.WholeRowContent()
		[
			SNew(SBox)
			.MaxDesiredHeight(300.0f)
			[
				SAssignNew(RemoveMorphTargetWidget, SAllowMorphTargetsWidget, LODRecipeBeingCustomized)
			]
		];*/
	}

	//Make sure to add the rest of the settings
	for (auto Iter(PropertyHandles.CreateConstIterator()); Iter; ++Iter)
	{
		IDetailPropertyRow& SettingsRow = ChildBuilder.AddProperty(Iter.Value().ToSharedRef());
	}
}
