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

#include "SimplygonContentModule.h"
#include "SimplygonStyle.h"
#include "SimplygonSettings.h"

#include "ISettingsModule.h"
#include "Misc/CoreDelegates.h"

#define LOCTEXT_NAMESPACE "SimplygonContent"

class FSimplygonContentModule : public ISimplygonContentModule
{
	/** IModuleInterface implementation */
	virtual void StartupModule() override;
	virtual void ShutdownModule() override;

private:
	void OnModulesChanged(FName InModuleName, EModuleChangeReason InChangeReason);
	void OnPostEngineInit();

private:
	FDelegateHandle SlateCoreModuleLoaded;
};

IMPLEMENT_MODULE( FSimplygonContentModule, SimplygonContent )

void FSimplygonContentModule::StartupModule()
{
	SlateCoreModuleLoaded = FModuleManager::Get().OnModulesChanged().AddRaw(this, &FSimplygonContentModule::OnModulesChanged);

	FCoreDelegates::OnPostEngineInit.AddRaw(this, &FSimplygonContentModule::OnPostEngineInit);
	
}

void FSimplygonContentModule::ShutdownModule()
{
	FModuleManager::Get().OnModulesChanged().Remove(SlateCoreModuleLoaded);

	if (ISettingsModule* SettingsModule = FModuleManager::GetModulePtr<ISettingsModule>("Settings"))
	{
		SettingsModule->UnregisterSettings("Project", "Plugins", "Simplygon");

	}
}

void FSimplygonContentModule::OnModulesChanged(FName InModuleName, EModuleChangeReason InChangeReason)
{
	if (InChangeReason == EModuleChangeReason::ModuleLoaded)
	{
		if (InModuleName == "SlateCore")
		{
			//Initalize Style
			FSimplygonStyle::Initialize();
		}
	}
}

void FSimplygonContentModule::OnPostEngineInit()
{
	if (ISettingsModule* SettingsModule = FModuleManager::GetModulePtr<ISettingsModule>("Settings"))
	{

		SettingsModule->RegisterSettings("Project", "Plugins", "Simplygon",
			LOCTEXT("SimplygonPluginSettingsName", "Simplygon Plugin"),
			LOCTEXT("SimplygonPluginSettingsNameTooltip", "Configure the Simplygon Plugin"),
			GetMutableDefault<USimplygonSettings>());
		
	}
}

#undef LOCTEXT_NAMESPACE

