/* -----------------------------------------------------------------------------
	GSFramework
	Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */


		#include "scene3d/mitem.h"
		#include "scene3d/scene.h"
		#include "physic/physic_world.h"
		#include "core/engine.h"
		#include "script/scripted_object.h"
		#include "log/log.h"

		using GS::NML::Tag;
		using namespace GS::S3D;


//------------------------------------------------------------------------------
bool				MItem::FromMetaTag(Tag &tag)
{
	if	(tag.name != "MItem")
		__ERR__(__LOG_E__ << "Could not parse managed item, incorrect root tag (" << tag.name << ").\n", false)

	// Set item default state.
	initial_state_set = false;
	initial_matrix = Matrix4::IdentityMatrix();

	mitem_flags.Set(0);

	uid = (uint)~0;

	NMLTagForeach(pt, tag)
	{
				if	(pt->name == "Id")				name = pt->GetString();
		else	if	(pt->name == "UId")				uid = pt->GetUnsigned();

		else	if	(pt->name == "Inactive")		mitem_flags.Raise(Flag_IsActive, false);
		else	if	(pt->name == "Static")			mitem_flags.Set(Flag_IsStatic);
		else	if	(pt->name == "Helper")			mitem_flags.Set(Flag_IsHelper);

		else	if	(pt->name == "Billboard")		mitem_flags.Set(Flag_Billboard);
		else	if	(pt->name == "SolveOverlap")	mitem_flags.Set(Flag_SolveOverlap);
		else	if	(pt->name == "Ghost")			mitem_flags.Set(Flag_Ghost);
		else	if	(pt->name == "TriggerDetected")	mitem_flags.Set(Flag_TriggerDetected);

		else	if	(pt->name == "InitialMatrix")
		{
			if	(initial_matrix.FromMetaTag(*pt))
				initial_state_set = true;
		}

		else	if	(pt->name == "Priority")
			priority = pt->GetReal();

		// Components.
		else	if	(pt->name == "MotionPlayer")
				automation_player->FromMetaTag(*pt);
		else	if	(pt->name == "PhysicItem")
				physic_item_desc.FromMetaTag(*pt);
		else	if	(pt->name == "ScriptedObject")
		{
			if	(scripted_object)
				scripted_object->FromMetaTag(*pt);
		}

#if		1
		else	if	(pt->name == "Active");

		else	if	(pt->name == "ScriptLogicFq");
		else	if	(pt->name == "TauItem");
		else	if	(pt->name == "GColItem");
		else	if	(pt->name == "ScriptUnit");

		else	if	(pt->name == "Proxy");
		else	if	(pt->name == "ProxyScene");
		else	if	(pt->name == "ProxyGroup");
		else	if	(pt->name == "ProxyFlag");
		else	if	(pt->name == "ProxyRootParamTo");

		else	if	(pt->name == "ToolSpecific")
				mitem_flags.Set(Flag_IsHelper);
#endif

		else
			__LOG_W__ << "Unknown tag '" << pt->name << "' in <MItem>.\n";
	}
	if	(uid == ~0)
		__ERR__(__LOG_E__ << "Managed item '" << name << "' Uid is invalid.\n", false)

	return true;
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
Tag					*MItem::AsMetaTag()
{
	Tag *root = new Tag("MItem");
	if	(!root)
		__ERR__(__LOG_E__ << "Could not create root tag to serialize.\n", NULL)

	// Id.
	root->AddChild("Id", name);
	root->AddChild("UId", uid);

	// Flags.
	if	(!mitem_flags.IsSet(Flag_IsActive))
		root->AddChild("Inactive");
	if	(mitem_flags.IsSet(Flag_Billboard))
		root->AddChild("Billboard");
	if	(mitem_flags.IsSet(Flag_SolveOverlap))
		root->AddChild("SolveOverlap");
	if	(mitem_flags.IsSet(Flag_Ghost))
		root->AddChild("Ghost");
	if	(mitem_flags.IsSet(Flag_TriggerDetected))
		root->AddChild("TriggerDetected");

	if	(mitem_flags.IsSet(Flag_IsActive))
		root->AddChild("Active");
	if	(mitem_flags.IsSet(Flag_IsHelper))
		root->AddChild("Helper");
	if	(mitem_flags.IsSet(Flag_IsStatic))
		root->AddChild("Static");

	// Transformation.
	if	(initial_state_set)
		root->AddChild(initial_matrix.AsMetaTag("InitialMatrix"));

	root->AddChild("Priority", priority);

	// Components.
	root->AddChild(automation_player->AsMetaTag());
	root->AddChild(scripted_object->AsMetaTag());
	root->AddChild(physic_item_desc.AsMetaTag());

	return root;
}
//------------------------------------------------------------------------------
