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

#include "ReductionPipelineCustomizations.h"
#include "SimplygonCustomPipelines.h"


#include "PropertyHandle.h"
#include "DetailLayoutBuilder.h"
#include "DetailCategoryBuilder.h"
#include "DetailWidgetRow.h"
#include "PropertyCustomizationHelpers.h"
#include "IDetailChildrenBuilder.h"
#include "IDetailPropertyRow.h"

//////// Details
FReductionPipelineDetailsCustomization::FReductionPipelineDetailsCustomization()
{
}

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

void FReductionPipelineDetailsCustomization::CustomizeDetails(IDetailLayoutBuilder& DetailLayout)
{
	TArray<TWeakObjectPtr<UObject>> Objects;
	DetailLayout.GetObjectsBeingCustomized(Objects);
}

/////////// Property
TSharedRef<IPropertyTypeCustomization> FReductionPipelinePropertyCustomization::MakeInstance()
{
	return MakeShareable(new FReductionPipelinePropertyCustomization());
}

void FReductionPipelinePropertyCustomization::CustomizeHeader(TSharedRef<IPropertyHandle> StructPropertyHandle, FDetailWidgetRow& HeaderRow, IPropertyTypeCustomizationUtils& StructCustomizationUtils)
{

}

void FReductionPipelinePropertyCustomization::CustomizeChildren(TSharedRef<IPropertyHandle> StructPropertyHandle, IDetailChildrenBuilder& ChildBuilder, IPropertyTypeCustomizationUtils& StructCustomizationUtils)
{
	// Retrieve structure's child properties
	uint32 NumChildren;
	StructPropertyHandle->GetNumChildren(NumChildren);
	TMap<FName, TSharedPtr< IPropertyHandle > > PropertyHandles;
	for (uint32 ChildIndex = 0; ChildIndex < NumChildren; ++ChildIndex)
	{
		TSharedRef<IPropertyHandle> ChildHandle = StructPropertyHandle->GetChildHandle(ChildIndex).ToSharedRef();
		FProperty* Property = ChildHandle->GetProperty();
		if (Property)
		{
			const FName PropertyName = ChildHandle->GetProperty()->GetFName();
			PropertyHandles.Add(PropertyName, ChildHandle);
		}
	}

	//Hide settings
	PropertyHandles.FindAndRemoveChecked(GET_MEMBER_NAME_CHECKED(FReductionPipelineSettings, VertexWeightSettings));
	PropertyHandles.FindAndRemoveChecked(GET_MEMBER_NAME_CHECKED(FReductionPipelineSettings, VisibilitySettings));

	TSharedPtr<IPropertyHandle> MappingImageHandle = PropertyHandles.FindChecked(GET_MEMBER_NAME_CHECKED(FReductionPipelineSettings, MappingImageSettings));
	TSharedPtr<IPropertyHandle> PropertyCasterHandle = PropertyHandles.FindChecked(GET_MEMBER_NAME_CHECKED(FReductionPipelineSettings, MaterialPropertyCasters));
	
	for (auto Iter(PropertyHandles.CreateConstIterator()); Iter; ++Iter)
	{
		IDetailPropertyRow& SettingsRow = ChildBuilder.AddProperty(Iter.Value().ToSharedRef());
	}
}

FReductionPipelinePropertyCustomization::FReductionPipelinePropertyCustomization()
{
}
