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


		#include "project/project.h"
		#include "metafile/nml.h"
		#include "log/log.h"

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


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

	name.Clear();
	search_path.Clear();
	root_script_list.Clear();

	NMLTagForeach(t, tag)
	{
				if	(t->name == "Name")			name = t->GetString();
		else	if	(t->name == "Authors")		authors = t->GetString();
		else	if	(t->name == "Description")	description = t->GetString();
		else	if	(t->name == "Copyright")	company = t->GetString();
		else	if	(t->name == "Company")		company = t->GetString();

		else	if	(t->name == "Include")
		{
			NMLTagForeach(st, *t)
				if	(st->name == "URI")
					root_script_list.Add(st->GetString());
		}
		else	if	(t->name == "SearchPaths")
		{
			NMLTagForeach(path, *t)
				if	(path->GetType() == GS::Variant::VariantString)
					search_path.Add(path->GetString());
		}
		else	if	(t->name == "ScriptUnit")
			script_unit->FromMetaTag(*t);
	}
	return true;
}
//------------------------------------------------------------------------------

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

	root->AddChild("Name", name.c_str());
	root->AddChild("Authors", authors.c_str());
	root->AddChild("Description", description.c_str());
	root->AddChild("Company", company.c_str());

	root->AddChild(script_unit->AsMetaTag());

	// Save includes.
	if	(Tag *include_tag = root->AddChild("Include"))
		for (uint n = 0; n < root_script_list.GetCount(); ++n)
			include_tag->AddChild("URI", root_script_list.ObjectAt(n).c_str());

	// Save search paths.
	if	(Tag *path_section = root->AddChild("SearchPaths"))
		for (uint n = 0; n < search_path.GetCount(); ++n)
			path_section->AddChild("Path", search_path.ObjectAt(n).c_str());

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