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

#include "ReductionSettingsCustomization.h"
#include "SimplygonUE4UI.h"

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

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

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

void FReductionSettingsCustomization::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();

		if (ChildHandle->GetProperty()->HasAnyPropertyFlags(CPF_AdvancedDisplay))
			AdvancedPropertyHandles.Add(PropertyName, ChildHandle);
		else
			PropertyHandles.Add(PropertyName, ChildHandle);
	}

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

	//SG-TODO: Create an advanced widget
	//IDetailGroup& AdvancedGroup = ChildBuilder.AddGroup(NAME_None, FText::FromString("Advance"));
}
