// Copyright 2015 Noitom Technology Co., Ltd. All Rights Reserved.
using System.IO;
namespace UnrealBuildTool.Rules
{

#if WITH_FORWARDED_MODULE_RULES_CTOR
    using zTargetInfo = ReadOnlyTargetRules;
#else
    using zTargetInfo = TargetInfo;
#endif

    public class MotionCaptureRuntime : ModuleRules
	{
        private string ModulePath
        {
            get { return ModuleDirectory; }
        }

        public MotionCaptureRuntime(zTargetInfo Target)
#if WITH_FORWARDED_MODULE_RULES_CTOR
            : base(Target)
#endif
        {
            Definitions.Add("WITH_AVATARID=0");
            Definitions.Add("WITH_DAPENG=0");

            // List of all paths to include files that are exposed to other modules.
			PublicIncludePaths.AddRange(
				new string[] {
                    "MotionCaptureRuntime/Public",
					// ... add public include paths required here ...
                }
				);

            // List of all paths to this module's internal include files, not exposed to other modules
			PrivateIncludePaths.AddRange(
				new string[] {
                    "MotionCaptureRuntime/Private",
					// ... add other private include paths required here ...
				}
				);

            // List of modules with header files that our module's public headers needs access to, but we don't need to "import" or link against.
            PublicIncludePathModuleNames.AddRange(
                new string[] {
                }
                );

            // List of modules with header files that our module's private code files needs access to, but we don't need to "import" or link against.
            PrivateIncludePathModuleNames.AddRange(
                new string[] {
                    "Launch",
                    "SteamVR",
                }
                );

            // List of public dependency module names. These are modules that are required by our public source files.
			PublicDependencyModuleNames.AddRange(
				new string[]
				{
					// ... add other public dependencies that you statically link with here ...
                    "HI5Glove_SDK",
                }
				);

            // List of private dependency module names. These are modules that our private code depends on but nothing in our public
            // include files depend on.
			PrivateDependencyModuleNames.AddRange(
				new string[]
				{
                    "Core",
					"CoreUObject",
					"Engine",
					"Networking",
					"Sockets",
                    "Projects",
                    //"HybridData",
					// ... add private dependencies that you statically link with here ...
                    "SteamVR",
                    "HeadMountedDisplay",
                    "XmlParser",
                }
                );

            // Addition modules this module may require at run-time.
			DynamicallyLoadedModuleNames.AddRange(
				new string[]
				{
					// ... add any modules that your module loads dynamically here ...
				}
				);

            LoadDependencies(Target);

            CheckAndSetMacros(Target);
        }

        private string ModuleRootPath
        {
            get { return Path.GetFullPath(Path.Combine(ModulePath, "../../")); }
        }

        public bool LoadDependencies(zTargetInfo Target)
        {
            bool isLibrarySupported = false;

            if ((Target.Platform == UnrealTargetPlatform.Win64) || (Target.Platform == UnrealTargetPlatform.Win32))
            {
                isLibrarySupported = true;

                BuildVersion Version;
                if (BuildVersion.TryRead(BuildVersion.GetDefaultFileName(), out Version))
                {
                    if (Version.MajorVersion == 4)
                    {
                        if (Version.MinorVersion == 14 ||
                            Version.MinorVersion == 15)
                        {
                            LoadOpenVR(Target, "v1_0_2");   // ue4.13/ue4.14/ue4.15
                        }
                        else if (Version.MinorVersion == 16)
                        {
                            LoadOpenVR(Target, "v1_0_6");   // ue4.16
                        }
                        else if (Version.MinorVersion == 17)
                        {
                            LoadOpenVR(Target, "v1_0_7");   // ue4.17
                        }
                        else if (Version.MinorVersion == 18 ||
                            Version.MinorVersion == 19)
                        {
                            LoadOpenVR(Target, "v1_0_10");  // ue4.18
                        }
                        else
                        {
                            isLibrarySupported = false;
                            System.Console.WriteLine("Not supported Minor version!");
                        }
                    }
                    else
                    {
                        isLibrarySupported = false;
                        System.Console.WriteLine("Not supported Major version!");
                    }
                }
                else
                {
                    isLibrarySupported = false;
                    System.Console.WriteLine("Cannot read Engine version.");
                }

                //RuntimeDependencies.Add(new RuntimeDependency(Path.Combine(ModuleRootPath,"MotionCapture.uplugin")));
                //RuntimeDependencies.Add(new RuntimeDependency(Path.Combine(BuildFilePath, "MotionCaptureRuntime.Build.cs")));
                //RuntimeDependencies.Add(new RuntimeDependency(Path.Combine(ContentIconsPath, "BVHAsset_16x.png")));
                //RuntimeDependencies.Add(new RuntimeDependency(Path.Combine(ContentIconsPath, "BVHAsset_64x.png")));
                //RuntimeDependencies.Add(new RuntimeDependency(Path.Combine(ContentIconsPath, "ManagerIcon.uasset")));
                //RuntimeDependencies.Add(new RuntimeDependency(Path.Combine(ContentIconsPath, "StartPointIcon.uasset")));
            }

            return isLibrarySupported;
        }

	    public void LoadOpenVR(zTargetInfo Target, string openvr_version)
	    {
	        string OpenVRVersion = openvr_version;
	        //string SdkBase = UEBuildConfiguration.UEThirdPartySourceDirectory + "OpenVR/OpenVR" + OpenVRVersion; // UEBuildConfiguration在4.19被移除了。
	        //string SdkBase = Target.UEThirdPartySourceDirectory + "OpenVR/OpenVR" + OpenVRVersion; // 直接用"ThirdParty"也可以
	        string SdkBase = "ThirdParty/OpenVR/OpenVR" + OpenVRVersion;

            if (!Directory.Exists(SdkBase))
            {
                System.Console.WriteLine(SdkBase + " not exist!");
                return;
            }
            else
            {
                System.Console.WriteLine("Load " + SdkBase);
            }

            PublicIncludePaths.Add(SdkBase + "/headers");
            string LibraryPath = SdkBase + "/lib/";

            if (Target.Platform == UnrealTargetPlatform.Win32 || Target.Platform == UnrealTargetPlatform.Win64)
	        {
	            // Doesnt work? (disabled)
	            //AddEngineThirdPartyPrivateStaticDependencies(Target, "OpenVR");

	            string LibPlatform = (Target.Platform == UnrealTargetPlatform.Win32 ? "win32" : "win64");

	            PublicLibraryPaths.Add(LibraryPath + LibPlatform);
	            PublicAdditionalLibraries.Add("openvr_api.lib");
	            PublicDelayLoadDLLs.Add("openvr_api.dll");

	            string OpenVRBinariesDir = string.Format("$(EngineDir)/Binaries/ThirdParty/OpenVR/OpenVR{0}/{1}/", OpenVRVersion, LibPlatform);
	            RuntimeDependencies.Add(new RuntimeDependency(OpenVRBinariesDir + "openvr_api.dll"));
	        }
        }

        public void CheckAndSetMacros(zTargetInfo Target)
        {
            if (Definitions.Contains("WITH_DAPENG=1"))
            {
                PublicDependencyModuleNames.Remove("SteamVR");
                PrivateDependencyModuleNames.Remove("SteamVR");
                DynamicallyLoadedModuleNames.Remove("SteamVR");

                Definitions.Add("STEAMVR_MODULE_LOADED=0");
            }
            else
            {
                Definitions.Add("STEAMVR_MODULE_LOADED=1");
            }
        }
    }
}