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

#pragma once

#if WITH_EDITOR
#include "CoreMinimal.h"
#include "Developer/MaterialUtilities/Public/MaterialUtilities.h"

/**
 * Helper class to convert between Simplygon material channel name to
 * flatten material property
 */
struct FSimplygonMaterialChannelConverter
{
public:
	/**
	 * Get the raw simplygon channel to flatten material property map
	 * @return channel to flatten material property map 
	 */
	static const TMap<FString, EFlattenMaterialProperties>& GetChannelToPropertyMap()
	{
		static const TMap<FString, EFlattenMaterialProperties> MaterialChannelToPropertyMap =
		{
				{ "BaseColor", EFlattenMaterialProperties::Diffuse },
				{ "Metallic", EFlattenMaterialProperties::Metallic },
				{ "Specular", EFlattenMaterialProperties::Specular },
				{ "Roughness", EFlattenMaterialProperties::Roughness },
				{ "Normal", EFlattenMaterialProperties::Normal },
				{ "Opacity", EFlattenMaterialProperties::Opacity },
				{ "Emissive", EFlattenMaterialProperties::Emissive },
				{ "SubSurface", EFlattenMaterialProperties::SubSurface },
				{ "OpacityMask", EFlattenMaterialProperties::OpacityMask },
				{ "AmbientOcclusion", EFlattenMaterialProperties::AmbientOcclusion }
		};

		return MaterialChannelToPropertyMap;
	};

	/**
	 * Convert from flatten material property to simplygon material channel
	 * @param Property - Input flatten material property to convert 
	 * @param OutChannel - Output simplygon channel name
	 * @return True if property was converted
	 */
	static bool MaterialPropertyToChannel(const EFlattenMaterialProperties Property, FString& OutChannel)
	{
		const auto& MaterialChannelToPropertyMap = GetChannelToPropertyMap();
		for (const auto& ChannelPropertyKvp : MaterialChannelToPropertyMap)
		{
			if (ChannelPropertyKvp.Value == Property)
			{
				OutChannel = ChannelPropertyKvp.Key;
				return true;
			}
		}
		return false;
	}

	/**
	 * Convert from simplygon material channel to flatten material property 
	 * @param Channel - Input Simplygon material channel name to convert
	 * @param OutProperty - Output flatten material property
	 * @return True if channel was converted
	 */
	static bool MaterialChannelToProperty(const FString& Channel, EFlattenMaterialProperties& OutProperty)
	{
		const auto& MaterialChannelToPropertyMap = GetChannelToPropertyMap();
		if (MaterialChannelToPropertyMap.Contains(Channel))
		{
			OutProperty = MaterialChannelToPropertyMap[Channel];
			return true;
		}
		return false;
	}
};
#endif
