/**
 * Copyright 2022-2025 Theai, Inc. dba Inworld AI
 */

#include "InworldRuntimeEditorSettings.h"

#include "Internationalization/StringTableCore.h"
#include "Internationalization/StringTableRegistry.h"
#include "Misc/SecureHash.h"

const FInworldDataTypeEditorInfo& UInworldRuntimeEditorSettings::GetDataTypeEditorInfo(const UStruct* Struct) const
{
	if (Struct == nullptr)
	{
		static FInworldDataTypeEditorInfo NullDataTypeEditorInfo{FColor::Red};
		return NullDataTypeEditorInfo;
	}

	const FString Key{Struct->GetName()};
	if (!DataTypeEditorInfo.Contains(Key))
	{
		// Generate a random hex code by the data type name
		auto GenerateColor = [this, Key]() -> FColor
		{
			FMD5 Hash;
			Hash.Update(reinterpret_cast<const uint8*>(TCHAR_TO_UTF8(*Key)), Key.Len());
			uint8 Digest[16];
			Hash.Final(Digest);

			// --- Use the first byte directly for Hue ---
			const uint8 Hue = Digest[0];	// The most efficient way to get the first byte

			// Use the specified FLinearColor::MakeFromHSV8 function.
			FLinearColor FinalLinearColor = FLinearColor::MakeFromHSV8(Hue, PinDefaultColorSaturation, PinDefaultColorValue);

			return FinalLinearColor.ToFColor(true);
		};

		DataTypeEditorInfo.Add(Key, {GenerateColor()});
		const_cast<UInworldRuntimeEditorSettings*>(this)->SaveConfig();
	}
	return DataTypeEditorInfo[Key];
}
