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

#include "CoreMinimal.h"
#include "CoreGlobals.h"
#include "EngineGlobals.h"
#include "Misc/AutomationTest.h"
#include "Modules/ModuleManager.h"
//Automation
#include "Tests/AutomationCommon.h"
#include "Tests/AutomationEditorCommon.h"
#include "Interfaces/IPluginManager.h"
#include "SimplygonModule.h"
#include "SimplygonSettings.h"
#include "SimplygonPluginMeshReductionModule.h"
#include "AutomationDriverCommon.h"
#include "AutomationDriverTypeDefs.h"
#include "SimplygonTestsHelper.h"
#include "Engine/StaticMesh.h"
#include "Subsystems/AssetEditorSubsystem.h"

DEFINE_LOG_CATEGORY_STATIC(LogSimplygonSpec, All, All);



# if 0
BEGIN_DEFINE_SPEC(FSimplygonAutomationSpec, "Simplygon.Automation", EAutomationTestFlags::ProductFilter | EAutomationTestFlags::ApplicationContextMask)
	FAutomationDriverPtr Driver;
END_DEFINE_SPEC(FSimplygonAutomationSpec)
void FSimplygonAutomationSpec::Define()
{
	
	//@todo write my expectations here
	BeforeEach([this]() {
			
		if (IAutomationDriverModule::Get().IsEnabled())
		{
			IAutomationDriverModule::Get().Disable();
		}

		IAutomationDriverModule::Get().Enable();

		Driver = IAutomationDriverModule::Get().CreateDriver();
	});

	Describe("Execute()", [this]()
	{
		LatentIt("Should generate 4 LODs", [this](const FDoneDelegate& Done)
		{
			//	UStaticMesh* StaticMesh = SimplygonTestsHelper::GetAssetByPath<UStaticMesh>(TEXT("/Game/Assets/s_logo.s_logo"));
			//	if (!StaticMesh)
			//	{
			//		UE_LOG(LogSimplygonSpec, Error, TEXT("Failed to find Static Mesh"));
			//		Done.Execute();
			//	}
			//	
			//	UAssetEditorSubsystem* AssetEditorSubsystem = GEditor->GetEditorSubsystem<UAssetEditorSubsystem>();
			//	AssetEditorSubsystem->OpenEditorForAsset(StaticMesh);
			//	AssetEditorSubsystem->OnAssetOpenedInEditor().AddLambda([](UObject* Object, IAssetEditorInstance* InEditor) {
			//		
			//		InEditor->FocusWindow(Object);
			//		
			//	});

			//	

			//	//Driver->Wait(FTimespan::FromSeconds(3));

			//	auto NoOfLODs = Driver->FindElement(By::Path("NumberOfLODs"));
			//	auto ApplyChanges = Driver->FindElement(By::Path("ApplyChanges"));
			////	bool timeout = Driver->Wait(Until::ElementExists(NoOfLODs, FWaitTimeout::InSeconds(3)));

			//	/*if (!NoOfLODs->Exists())
			//	{
			//		UE_LOG(LogSimplygonSpec, Error, TEXT("NumberOfLODs Element not found"));
			//		AssetEditorSubsystem->CloseAllEditorsForAsset(StaticMesh);
			//		return;
			//	}*/
			//		

			//	
			//	
			//	auto Sequence = Driver->CreateSequence();
			//		Sequence->Actions()
			//			.Focus(NoOfLODs)
			//			.Type(TEXT("4"))
			//			.Click(ApplyChanges);
			//			
			//	bool sucess = Sequence->Perform();
			//	TestEqual("Sequence Ran", sucess, true);
			//	AssetEditorSubsystem->CloseAllEditorsForAsset(StaticMesh);
			//	Done.Execute();
		});

		/*It("should return false when unsuccessful", [this]()
		{
			return false;
		});*/
	});

	AfterEach([this]() {
		Driver.Reset();
		IAutomationDriverModule::Get().Disable();
	});
}
#endif

//IMPLEMENT_SIMPLE_AUTOMATION_TEST(FSimplygonMeshReductionTest, "Simplygon.Editor.StaticMeshEditor CreateLODs", EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter);
//
//bool FSimplygonMeshReductionTest::RunTest(const FString& Parameters)
//{
//	FString Description = TEXT("License Failure Validation:\n- Try to load simplygon with an invalid license.\n");
//	AddInfo(Description);
//
//	
//	//Driver->FindElement()
//
//	//FString AssetPath = FPackageName::FilenameToLongPackageName(FPaths::GameContentDir() + TEXT("Assets/s_logo"));
//	UStaticMesh* StaticMesh = SimplygonTestsHelper::GetAssetByPath<UStaticMesh>(TEXT("/Game/Assets/s_logo.s_logo"));
//
//	if (!StaticMesh)
//	{
//		UE_LOG(LogSimplygonTests, Error, TEXT("Failed to find Static Mesh"));
//		return false;
//	}
//	
//	SimplygonTestsHelper::OpenStaticMeshEditor(StaticMesh);
//	
//	if(!IAutomationDriverModule::Get().IsEnabled())
//		IAutomationDriverModule::Get().Enable();
//	
//	auto Driver = IAutomationDriverModule::Get().CreateAsyncDriver();
//	auto NoOfLODs = Driver->FindElement(By::Id("NumberOfLODs"));
//	auto ApplyChanges = Driver->FindElement(By::Id("ApplyChanges"));
//
//	auto Sequence = Driver->CreateSequence();
//	Sequence->Actions()
//		.Focus(NoOfLODs)
//		.Type(TEXT("4"))
//		.Click(ApplyChanges);
//	Sequence->Perform();
//
//
//	Driver.Get().Wait(5000);
//	
//	if (IAutomationDriverModule::Get().IsEnabled())
//		IAutomationDriverModule::Get().Disable();
//
//	return true;
//}
