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

#include "SimplygonSettings.h"
#include "Misc/CoreDelegates.h"
#include "Misc/Paths.h"

#if PLATFORM_WINDOWS
#include "Windows/AllowWindowsPlatformTypes.h"
#include <Windows.h>
#include "Windows/HideWindowsPlatformTypes.h"
#endif // PLATFORM_WINDOWS

#define LOCTEXT_NAMESPACE "SimplygonSettings"

DEFINE_LOG_CATEGORY_STATIC(LogSimplygonSettings, VeryVerbose, All);

static FString GetLicenseFilename()
{
	return TEXT("Simplygon_License.dat");
}

// Simplygon's default license path
static TArray<FFilePath> GetDefaultLicensePaths()
{
	FString SimplygonGlobalEnvPath = FPlatformMisc::GetEnvironmentVariable(TEXT("SIMPLYGON_9_PATH"));
#if PLATFORM_WINDOWS
	if (!SimplygonGlobalEnvPath.IsEmpty())
	{
		FString ExpandedPath;
		TArray<TCHAR>& CharArray = ExpandedPath.GetCharArray();
		CharArray.SetNumUninitialized(FPlatformMisc::GetMaxPathLength());

		uint32 ExpandedLength = ::ExpandEnvironmentStringsW(SimplygonGlobalEnvPath.GetCharArray().GetData(), CharArray.GetData(), CharArray.Num());
		if (ExpandedLength && ExpandedLength < (uint32)CharArray.Num())
		{
			ExpandedPath.TrimToNullTerminator();
			SimplygonGlobalEnvPath = ExpandedPath;
		}
	}
#endif

	FString LicenseAppPaths = FPaths::Combine(TEXT("Microsoft/SimplygonSDK"), GetLicenseFilename());

	TArray<FFilePath> DefaultLicensePaths;
	DefaultLicensePaths.Add({ FPaths::Combine(SimplygonGlobalEnvPath, GetLicenseFilename()) });
	DefaultLicensePaths.Add({ FPaths::Combine(FPlatformMisc::GetEnvironmentVariable(TEXT("PROGRAMDATA")), LicenseAppPaths) });
	DefaultLicensePaths.Add({ FPaths::Combine(FPlatformMisc::GetEnvironmentVariable(TEXT("LOCALAPPDATA")), LicenseAppPaths) });

	return DefaultLicensePaths;
}

static FString GetLocalLicensePath()
{
	return FPaths::Combine(*FPaths::EnginePluginsDir(), TEXT("Simplygon/Source/ThirdParty/SimplygonSDK"), GetLicenseFilename());
}

USimplygonSettings::USimplygonSettings(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	FFilePath PrimaryLicensePath{ /*.FilePath=*/GetLocalLicensePath() };
	LicensePaths.Add(PrimaryLicensePath);
	LicensePaths.Append(GetDefaultLicensePaths());

	UseDistribution = false;
	PreferredRemesher = ERemeshingProcessorVersion::Default;
	bDebug = false;
	bExportPipelineAndScene = false;
	DebugPath.FilePath = FPaths::Combine(FPaths::ProjectIntermediateDir(), TEXT("Simplygon"));
	SendInterval = 180;	
	Tangentspace = EDefaultTNBSimplygon::OrthonormalLeftHanded;

}

FString USimplygonSettings::GetLicensePath() const
{
	FString EmptyLicensePaths;

	//Return the first match
	for (const FFilePath& LP : LicensePaths)
	{
		if (FPaths::FileExists(LP.FilePath))
			return LP.FilePath;
	}

	// No license found
	UE_LOG(LogSimplygonSettings, Warning, TEXT("No license found. Tried the following paths:"));
	for (const FFilePath& LP : LicensePaths)
	{
		UE_LOG(LogSimplygonSettings, Warning, TEXT("    - %s"), *LP.FilePath);
	}
	
	return FString();
}

//void USimplygonSettings::GetToggleCategoryAndPropertyNames(FName& OutCategory, FName& OutProperty) const
//{
//	OutCategory = FName("Telemetry");
//	OutProperty = FName("bSendUsageData");
//};
//
//FText USimplygonSettings::GetFalseStateLabel() const
//{
//	return LOCTEXT("FalseStateLabel", "Don't Send");
//};
//
//FText USimplygonSettings::GetFalseStateTooltip() const
//{
//	return LOCTEXT("FalseStateTooltip", "Don't send Simplygon/(Simplygon plugin) usage data to Microsoft.");
//};
//
//FText USimplygonSettings::GetFalseStateDescription() const
//{
//	return LOCTEXT("FalseStateDescription", "By opting out you have chosen to not send Simplygon/(Simplygon plugin) usage data to Microsoft. Please consider opting in to help improve Simplygon. Microsoft will never sell or trade individual usage data to / with third party organizations. We will collect information about how you use the Simplygon plugin,  when you use the Unreal Engine editor, the type of Simplygon processing you are running, how you interact with the various Simplygon components.");
//};
//
//FText USimplygonSettings::GetTrueStateLabel() const
//{
//	return LOCTEXT("TrueStateLabel", "Send Usage Data");
//};
//
//FText USimplygonSettings::GetTrueStateTooltip() const
//{
//	return LOCTEXT("TrueStateTooltip", "Send your Simplygon/(Simplygon plugin) usage data to Microsoft.");
//};
//
//FText USimplygonSettings::GetTrueStateDescription() const
//{
//	return LOCTEXT("TrueStateDescription", "By opting in you have chosen to send Simplygon/(Simplygon plugin) usage data to Microsoft. Thank you for helping to improve Simplygon and our integratoin. Microsoft will never sell or trade individual usage data to / with third party organizations. We will collect information about how you use the Simplygon plugin,  when you use the Unreal Engine editor, the type of Simplygon processing you are running, how you interact with the various Simplygon components.");
//};
//
//FString USimplygonSettings::GetAdditionalInfoUrl() const
//{
//	return FString(TEXT("https://privacy.microsoft.com/privacystatement"));
//};
//
//FText USimplygonSettings::GetAdditionalInfoUrlLabel() const
//{
//	return LOCTEXT("HyperlinkLabel", "Microsoft Privacy Statement");
//};

#undef LOCTEXT_NAMESPACE
