// Georgy Treshchev 2026 All Rights Reserved.

using UnrealBuildTool;
using System.IO;

public class RuntimeAudioImporterWebRTCAEC3 : ModuleRules
{
	// In earlier UE versions (such as 4.27), the architecture string is empty.
	protected string ArchitectureString
	{
		get
		{
			if (Target.Architecture.ToString().Length > 0)
			{
				return Target.Architecture.ToString();
			}
			else
			{
				return "x64";
			}
		}
	}

	public RuntimeAudioImporterWebRTCAEC3(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;

		string PlatformDir = Target.Platform.ToString();
		string IncDirPath = Path.Combine(ModuleDirectory, "..", "ThirdParty", "aec3", "Include");
		string LibDirPath = "";
		string RelativeLibDirPath = "";
		string SharedLibFileName = "UNSUPPORTED_PLATFORM";
		if (Target.Platform == UnrealTargetPlatform.Win64)
		{
			RelativeLibDirPath = Path.Combine("..", "ThirdParty", "aec3", PlatformDir, ArchitectureString);
			LibDirPath = Path.Combine(ModuleDirectory, RelativeLibDirPath);
			SharedLibFileName = "aec3.dll";
			PublicAdditionalLibraries.Add(Path.Combine(LibDirPath, "aec3.lib"));
			RuntimeDependencies.Add(Path.Combine(LibDirPath, SharedLibFileName));
			PublicDelayLoadDLLs.Add(SharedLibFileName);
			PublicIncludePaths.Add(IncDirPath);
		}
		else if (Target.Platform == UnrealTargetPlatform.Linux)
		{
			RelativeLibDirPath = Path.Combine("..", "ThirdParty", "aec3", PlatformDir, ArchitectureString);
			LibDirPath = Path.Combine(ModuleDirectory, RelativeLibDirPath);
			SharedLibFileName = "libaec3.so";
			PublicAdditionalLibraries.Add(Path.Combine(LibDirPath, SharedLibFileName));
			RuntimeDependencies.Add(Path.Combine(LibDirPath, SharedLibFileName));
			PublicIncludePaths.Add(IncDirPath);
		}
		else if (Target.Platform == UnrealTargetPlatform.Mac)
		{
			RelativeLibDirPath = Path.Combine("..", "ThirdParty", "aec3", PlatformDir);
			LibDirPath = Path.Combine(ModuleDirectory, RelativeLibDirPath);
			SharedLibFileName = "libaec3.dylib";
			PublicAdditionalLibraries.Add(Path.Combine(LibDirPath, SharedLibFileName));
			PublicDelayLoadDLLs.Add(Path.Combine(LibDirPath, SharedLibFileName));
			RuntimeDependencies.Add(Path.Combine(LibDirPath, SharedLibFileName));
			PublicIncludePaths.Add(IncDirPath);
		}
		else if (Target.Platform == UnrealTargetPlatform.Android)
		{
			RelativeLibDirPath = Path.Combine("..", "ThirdParty", "aec3", PlatformDir);
			LibDirPath = Path.Combine(ModuleDirectory, RelativeLibDirPath);
			SharedLibFileName = "libaec3.so";

			string[] Architectures = new string[]
			{
				"arm64-v8a",
				"armeabi-v7a",
			};

			foreach (string Arch in Architectures)
			{
				string LibPath = Path.Combine(LibDirPath, Arch, SharedLibFileName);
				if (File.Exists(LibPath))
				{
					PublicAdditionalLibraries.Add(LibPath);
				}
			}

			PublicIncludePaths.Add(IncDirPath);
		}
		else if (Target.Platform == UnrealTargetPlatform.IOS)
		{
			PublicFrameworks.AddRange(new string[]
			{
				"CoreFoundation"
			});

			RelativeLibDirPath = Path.Combine("..", "ThirdParty", "aec3", PlatformDir);
			LibDirPath = Path.Combine(ModuleDirectory, RelativeLibDirPath);
			SharedLibFileName = "libaec3.a";
			PublicAdditionalLibraries.Add(Path.Combine(LibDirPath, SharedLibFileName));
			
			PublicIncludePaths.Add(IncDirPath);
		}

		string RelativeSharedLibPath = Path.Combine(RelativeLibDirPath, SharedLibFileName).Replace('\\', '/');

		PublicDefinitions.Add("WEBRTCAEC3_SHAREDLIB_PATH=" + RelativeSharedLibPath);

		PublicDependencyModuleNames.AddRange(
			new string[]
			{
				"Core",
				"RuntimeAudioImporter"
			}
		);

		PrivateDependencyModuleNames.AddRange(
			new string[]
			{
				"CoreUObject",
				"Engine"
			}
		);
		
		PrivateDependencyModuleNames.Add("Projects");
	}
}