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


		#include "core/emitter.h"
		#include "core/embedded_resource_handler_interface.h"
		#include "log/log.h"

		using namespace GS::Core;
		using namespace GS::NML;


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

	NMLTagForeach(pt, tag)
	{
				if	(pt->name == "Material")
					IEmbeddedResourceHandler::Get()->ExtractEmbeddedMaterial(material, *pt, name, 0);
		else	if	(pt->name == "MaterialRef")
					material = pt->GetString();

		else	if	(pt->name == "TTL")
					time_to_live.setSec(pt->GetReal());
		else	if	(pt->name == "Damping")
					damping = pt->GetReal();
		else	if	(pt->name == "Gravity")
					gravity.FromMetaTag(*pt);

		else	if	(pt->name == "AngleCurve" && pt->GetTag("Curve"))
					angle_curve.FromMetaTag(*pt->GetTag("Curve"));
		else	if	(pt->name == "SizeCurve" && pt->GetTag("Curve"))
					size_curve.FromMetaTag(*pt->GetTag("Curve"));

		else	if	(pt->name == "RedCurve"  && pt->GetTag("Curve"))
					red_curve.FromMetaTag(*pt->GetTag("Curve"));
		else	if	(pt->name == "GreenCurve" && pt->GetTag("Curve"))
					green_curve.FromMetaTag(*pt->GetTag("Curve"));
		else	if	(pt->name == "BlueCurve" && pt->GetTag("Curve"))
					blue_curve.FromMetaTag(*pt->GetTag("Curve"));
		else	if	(pt->name == "OpacityCurve" && pt->GetTag("Curve"))
					opacity_curve.FromMetaTag(*pt->GetTag("Curve"));

		else	__LOG_W__ << "Unknown tag '" << pt->name << "' in <ParticleModel>.\n";
	}
	return true;
}
Tag			*ParticleModel::AsMetaTag() const
{
	Tag *root = new Tag("ParticleModel");
	if	(!root)
		__ERR__(__LOG_E__ << "Could not serialize particle model. Failed to create root tag.\n", NULL)

	root->AddChild("MaterialRef", material.c_str());
	root->AddChild("TTL", time_to_live.toSec());
	root->AddChild("Damping", damping);
	root->AddChild(gravity.AsMetaTag("Gravity"));

	Tag *ct;
	ct = root->AddChild("AngleCurve"); ct->AddChild(angle_curve.AsMetaTag());
	ct = root->AddChild("SizeCurve"); ct->AddChild(size_curve.AsMetaTag());

	ct = root->AddChild("RedCurve"); ct->AddChild(red_curve.AsMetaTag());
	ct = root->AddChild("GreenCurve"); ct->AddChild(green_curve.AsMetaTag());
	ct = root->AddChild("BlueCurve"); ct->AddChild(blue_curve.AsMetaTag());
	ct = root->AddChild("OpacityCurve"); ct->AddChild(opacity_curve.AsMetaTag());
	return root;
}
//------------------------------------------------------------------------------

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

	birth_speed_min = birth_speed_max = 1;
	pool_size = 100;
	birth_rate = 1;

	model = Model_Spray;
	spray_angle = Units::Deg(45.f);

	particle_model.Clear();

	NMLTagForeach(pt, tag)
	{
				if	(pt->name == "Item")
					Item::FromMetaTag(*pt);

		else	if	(pt->name == "SpeedMin")
					birth_speed_min = pt->GetReal();
		else	if	(pt->name == "SpeedMax")
					birth_speed_max = pt->GetReal();

		else	if	(pt->name == "PoolSize")
					pool_size = pt->GetInteger();
		else	if	(pt->name == "BirthRate")
					birth_rate = pt->GetReal();

		else	if	(pt->name == "Type")
		{
			String _type(pt->GetString());

			if	(_type == "Spray")
				model = Model_Spray;
		}

		else	if	(pt->name == "SprayAngle")
				spray_angle = pt->GetReal();

		else	if	(pt->name == "ParticleModel")
				particle_model = pt->GetString();

		else	__LOG_W__ << "Unknown tag '" << pt->name << "' in <Emitter>.\n";
	}
	return true;
}
Tag			*Emitter::AsMetaTag() const
{
	Tag *root = new Tag("Emitter");
	if	(!root)
		__ERR__(__LOG_E__ << "Could not serialize emitter. Failed to create root tag.\n", NULL)

	// Store item.
	root->AddChild(Item::AsMetaTag());

	if	(birth_speed_min != 1)
		root->AddChild("SpeedMin", birth_speed_min);
	if	(birth_speed_max != 1)
		root->AddChild("SpeedMax", birth_speed_max);
	if	(birth_rate != 1)
		root->AddChild("BirthRate", birth_rate);
	if	(pool_size != 100)
		root->AddChild("PoolSize", (int)pool_size);

//	switch	(emitter_type)
//	{
//		case Type_Spray:	root->AddChild("Type", "Spray");
//	}

	root->AddChild("SprayAngle", spray_angle);

	if	(!particle_model.IsEmpty())
		root->AddChild("ParticleModel", particle_model.c_str());

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