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

#include "NearPipelineCustomization.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"

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

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

}

void FNearPipelinePropertyCustomization::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 SeparateOverlappingCharts since it is decided automatically based on if vertex colors exists or not
	TSharedPtr<IPropertyHandle> SeparateChartProperty = StructPropertyHandle->GetChildHandle(GET_MEMBER_NAME_CHECKED(FChartAggregatorSettings, SeparateOverlappingCharts));
	if (SeparateChartProperty.IsValid())
	{
		SeparateChartProperty->MarkHiddenByCustomization();
	}

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

FNearPipelinePropertyCustomization::FNearPipelinePropertyCustomization()
{
}

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

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

FNearPipelineDetailsCustomization::FNearPipelineDetailsCustomization()
{
}
