// Project Lab - NHTV IGAD
//////////////////////////////////////////
// Author: Yoshi van Belkom - 130118
//////////////////////////////////////////

#include "SkillTreeEditorPrivatePCH.h"
#include "AssetToolsModule.h"
#include "ModuleManager.h"
#include "SkillTreeAsset.h"
#include "SkillAsset.h"
#include "SkillTreeEditMode.h"
#include "SkillTreeEditorCommands.h"

#define LOCTEXT_NAMESPACE "SkillTreeEditor" 

EAssetTypeCategories::Type skillTreeAssetCategoryBit;

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

	void RegisterAssetTypeAction( IAssetTools& a_assetTools, TSharedRef<IAssetTypeActions> a_action );
private:
	TArray< TSharedPtr<IAssetTypeActions> > m_createdAssetTypeActions;
};


void FSkillTreeEditor::RegisterAssetTypeAction( IAssetTools& a_assetTools, TSharedRef<IAssetTypeActions> a_action )
{
	a_assetTools.RegisterAssetTypeActions( a_action );
	m_createdAssetTypeActions.Add( a_action );
}

void FSkillTreeEditor::StartupModule()
{
	FSkillTreeEditorCommands::Register();

	IAssetTools& assetTools = FModuleManager::LoadModuleChecked<FAssetToolsModule>( "AssetTools" ).Get();

	//Register the category of the assets.
	skillTreeAssetCategoryBit = assetTools.RegisterAdvancedAssetCategory( FName( TEXT( "SkillTree" ) ), LOCTEXT( "SkillTreeAssetCategory", "Skill Tree" ) );

	//Register the asset to the category.
	RegisterAssetTypeAction( assetTools, MakeShareable( new FSkillTreeAsset( skillTreeAssetCategoryBit ) ) );

	FEditorModeRegistry::Get().RegisterMode<FSkillTreeEditMode>( FSkillTreeEditMode::EM_skillTree, LOCTEXT( "SkillTreeEditMode", "Skill Tree Editor" ),	FSlateIcon(), false );
}

void FSkillTreeEditor::ShutdownModule()
{
	FEditorModeRegistry::Get().UnregisterMode( FSkillTreeEditMode::EM_skillTree );

	//Unregister all the assets.
	if ( FModuleManager::Get().IsModuleLoaded( "AssetTools" ) )
	{
		IAssetTools& AssetTools = FModuleManager::GetModuleChecked<FAssetToolsModule>( "AssetTools" ).Get();
		for ( int32 i = 0; i < m_createdAssetTypeActions.Num(); ++i )
		{
			AssetTools.UnregisterAssetTypeActions( m_createdAssetTypeActions[i].ToSharedRef() );
		}
	}
	m_createdAssetTypeActions.Empty();

	FSkillTreeEditorCommands::Unregister();
}

IMPLEMENT_MODULE( FSkillTreeEditor, SkillTreeEditor )

#undef LOCTEXT_NAMESPACE