//////////////////////////////////////////////////////////////////////////////
//
// ToolsAVP_Mode_Blueprint.cpp
//
// Copyright (C) 2008 Rebellion. All Rights Reserved.
//
//////////////////////////////////////////////////////////////////////////////

// I N C L U D E S ///////////////////////////////////////////////////////////

// core
#include "Core/Asura.h"

// header
#include "Blueprint/ToolsAVP_Mode_Blueprint.h"

// asura
#include "Core/Asura_NameList.h"

// avp
#include "Blueprint/AVP_Blueprint_HashIDs.h"

// tools
#include "Tools_System.h"
#include "Blueprint/ToolsAVP_Blueprint_Edit_Object.h"
#include "Entity/PhysicalObject/Lift/ToolsAVP_Entity_Lift.h"
#include "Entity/PhysicalObject/PhysicsObject/ToolsAVP_Entity_PhysicsObject.h"
#include "Interface/Tools_ContextMenu_System.h"
#include "Misc/Tools_Timed_Messages.h"
#include "PC/FileSystem/Tools_FileSystem.h"

//////////////////////////////////////////////////////////////////////////////
//
// ToolsAVP_Mode_Blueprint
//
//////////////////////////////////////////////////////////////////////////////

ToolsAVP_Mode_Blueprint::ToolsAVP_Mode_Blueprint()
{
	s_bAllowRecursiveExportsUsingParameters = true;
}

Tools_Mode_Blueprint* Tools_Mode_Blueprint::Project_CreateBlueprintMode()
{
    return new ToolsAVP_Mode_Blueprint();
}

Tools_Blueprint_Edit_Object* ToolsAVP_Mode_Blueprint::CreateBlueprintEditObject()
{
    return new ToolsAVP_Blueprint_Edit_Object;
}

void ToolsAVP_Mode_Blueprint::AddContextMenuItems( Tools_ContextMenu& xMenu, const Tools_ContextMenu_Context& xContext )
{
	PARENT::AddContextMenuItems( xMenu, xContext );

	Tools_ContextMenu_Section* pxSection = xMenu.GetSection( TOOLS_CONTEXTMENU_SECTION_SELECTION );

	Tools_ContextMenu_Item* pxItem = NULL;

	// If we are in object mode then add the ability to create a physical/static object, etc, from the blueprint.
	if ( Tools_System::GetCurrentMode() == TOOLS_MODE_OBJECT )
	{
		if ( TreeSelectionIsPhysicalObjectBlueprint() )
		{
			pxItem = new Tools_ContextMenu_Item_Separator();
			pxSection->AddItem( pxItem );

			pxItem = new Tools_ContextMenu_Item_Function( "Create Static From Blueprint", CreateStaticFromBlueprint );
			pxItem->SetHelpText( "Creates a static object and initialises it from the currently selected blueprint." );
			pxSection->AddItem( pxItem );

			pxItem = new Tools_ContextMenu_Item_Function( "Create Physics From Blueprint", CreatePhysicsFromBlueprint );
			pxItem->SetHelpText( "Creates a physical object and initialises it from the currently selected blueprint." );
			pxSection->AddItem( pxItem );

			//pxItem = new Tools_ContextMenu_Item_Function( "Create Lift From Blueprint", CreateLiftFromBlueprint );
			//pxItem->SetHelpText( "Creates a lift and initialises it from the currently selected blueprint." );
			//pxSection->AddItem( pxItem );
		}
	}
}

bool ToolsAVP_Mode_Blueprint::TreeSelectionIsPhysicalObjectBlueprint()
{
	const Asura_TreeDialog_Item* pxCurrentItem = s_pxThisBlueprintMode->s_xTree.GetSelectedItem();
	return ( pxCurrentItem && pxCurrentItem->GetUserData() == uAVP_BLUEPRINT_TYPE_PHYSICAL_OBJECTS );
}

void ToolsAVP_Mode_Blueprint::CreateStaticFromBlueprint()
{
	Asura_TreeDialog_Item* pxCurrentItem = s_pxThisBlueprintMode->s_xTree.GetSelectedItem();
	if ( !pxCurrentItem )
	{
		return;
	}
	else if ( pxCurrentItem->GetUserData() )
	{
		// If the current item has user data then it is a blueprint, the user data being the blueprint type hash.
		const Asura_Hash_ID uTypeHashID      = pxCurrentItem->GetUserData();
		const Asura_Hash_ID uBlueprintHashID = Asura_GetHashID( pxCurrentItem->GetName() );

		if ( uTypeHashID != uAVP_BLUEPRINT_TYPE_PHYSICAL_OBJECTS )
		{
			Tools_Timed_Message_System::AddMessage( Tools_Timed_Message_System::COLOUR_ERROR,
				"Cannot create a static object from a non-physical object blueprint." );
			return;
		}

		ToolsAVP_Entity_PhysicsObject::Create( true );
		if ( !Tools_Object_System::SelectedObject )
		{
			return;
		}

		ToolsAVP_Entity_PhysicsObject* pxPhyObject = ToolsAVP_Entity_PhysicsObject::FindEntity( Tools_Object_System::SelectedObject->GetGuid() );
		pxPhyObject->SetBlueprintHashID( uBlueprintHashID );
		pxPhyObject->RefreshOverlay();
	}
}

void ToolsAVP_Mode_Blueprint::CreatePhysicsFromBlueprint()
{
	Asura_TreeDialog_Item* pxCurrentItem = s_pxThisBlueprintMode->s_xTree.GetSelectedItem();
	if ( !pxCurrentItem )
	{
		return;
	}
	else if ( pxCurrentItem->GetUserData() )
	{
		// If the current item has user data then it is a blueprint, the user data being the blueprint type hash.
		const Asura_Hash_ID uTypeHashID      = pxCurrentItem->GetUserData();
		const Asura_Hash_ID uBlueprintHashID = Asura_GetHashID( pxCurrentItem->GetName() );

		if ( uTypeHashID != uAVP_BLUEPRINT_TYPE_PHYSICAL_OBJECTS )
		{
			Tools_Timed_Message_System::AddMessage( Tools_Timed_Message_System::COLOUR_ERROR,
				"Cannot create a physical object from a non-physical object blueprint." );
			return;
		}

		ToolsAVP_Entity_PhysicsObject::Create( false );
		if ( !Tools_Object_System::SelectedObject )
		{
			return;
		}

		ToolsAVP_Entity_PhysicsObject* pxPhyObject = ToolsAVP_Entity_PhysicsObject::FindEntity( Tools_Object_System::SelectedObject->GetGuid() );
		pxPhyObject->SetBlueprintHashID( uBlueprintHashID );
		pxPhyObject->RefreshOverlay();
	}
}

void ToolsAVP_Mode_Blueprint::CreateLiftFromBlueprint()
{
	Asura_TreeDialog_Item* pxCurrentItem = s_pxThisBlueprintMode->s_xTree.GetSelectedItem();
	if ( !pxCurrentItem )
	{
		return;
	}
	else if ( pxCurrentItem->GetUserData() )
	{
		// If the current item has user data then it is a blueprint, the user data being the blueprint type hash.
		const Asura_Hash_ID uTypeHashID      = pxCurrentItem->GetUserData();
		const Asura_Hash_ID uBlueprintHashID = Asura_GetHashID( pxCurrentItem->GetName() );

		if ( uTypeHashID != uAVP_BLUEPRINT_TYPE_PHYSICAL_OBJECTS )
		{
			Tools_Timed_Message_System::AddMessage( Tools_Timed_Message_System::COLOUR_ERROR,
				"Cannot create a lift from a non-physical object blueprint." );
			return;
		}

		ToolsAVP_Entity_Lift::Create();
		if ( !Tools_Object_System::SelectedObject )
		{
			return;
		}

		ToolsAVP_Entity_Lift* pxLiftObject = ToolsAVP_Entity_Lift::FindEntity( Tools_Object_System::SelectedObject->GetGuid() );
		pxLiftObject->SetBlueprintHashID( uBlueprintHashID );
		pxLiftObject->RefreshOverlay();
	}
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
