/*  $Id: resource_manager_generic.cpp,v 1.23 2004/02/19 17:41:58 mbn Exp $
**
**  ClanLib Game SDK
**  Copyright (C) 2003  The ClanLib Team
**  For a total list of contributers see the file CREDITS.
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**  Lesser General Public License for more details.
**
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
**
*/

#include "Core/precomp.h"
#include "resource_manager_generic.h"
#include "resource_generic.h"
#include "API/Core/IOData/inputsource_provider_file.h"
#include "API/Core/System/error.h"
#include "API/Core/System/clanstring.h"

/////////////////////////////////////////////////////////////////////////////
// CL_ResourceManager_Generic Construction:

CL_ResourceManager_Generic::CL_ResourceManager_Generic() : provider(0), delete_provider(false)
{
}

CL_ResourceManager_Generic::~CL_ResourceManager_Generic()
{
	if (delete_provider) delete provider;
}

/////////////////////////////////////////////////////////////////////////////
// CL_ResourceManager_Generic Attributes:

bool CL_ResourceManager_Generic::exists(const std::string &res_id)
{
	if (resources.find(res_id) != resources.end()) return true;

	// Search in all additional resources:
	std::list<CL_ResourceManager>::iterator it;
	for (it = additional_resources.begin(); it != additional_resources.end(); ++it)
	{
		if (it->exists(res_id)) return true;
	}

	return false;
}

CL_Resource &CL_ResourceManager_Generic::get_resource(const std::string &res_id)
{
	std::map<std::string, CL_Resource>::iterator it = resources.find(res_id);
	if (it == resources.end())
	{
		std::list<CL_ResourceManager>::iterator it2;
		for (it2 = additional_resources.begin(); it2 != additional_resources.end(); ++it2)
		{
			CL_ResourceManager &additional_manager = *it2;
			if (additional_manager.exists(res_id))
			{
				return additional_manager.get_resource(res_id);
			}
		}

		throw CL_Error(CL_String::format("Resource '%1' not found", res_id));
	}
	return it->second;
}

std::list<std::string> CL_ResourceManager_Generic::get_all_resources(const std::string &original_section_name)
{
	std::list<std::string> result;

	// Make sure we have a trailing / in section
	std::string section_name = add_trailing_slash(original_section_name);

	// Add local resources:
	{
		std::map<std::string, CL_Resource>::iterator it;
		for (it = resources.begin(); it != resources.end(); ++it)
		{
			if (it->first.substr(0, section_name.length()) == section_name)
			{
				result.push_back(it->first);
			}
		}
	}

	// Add resources for all additional resources:
	{
		std::list<CL_ResourceManager>::iterator it;
		for (it = additional_resources.begin(); it != additional_resources.end(); ++it)
		{
			std::list<std::string> additionals = it->get_all_resources(section_name);
			result.merge(additionals);
		}
	}

	return result;
}

std::list<std::string> CL_ResourceManager_Generic::get_all_resources()
{
	std::list<std::string> result;

	// Add local resources:
	{
		std::map<std::string, CL_Resource>::iterator it;
		for (it = resources.begin(); it != resources.end(); ++it)
		{
			result.push_back(it->first);
		}
	}

	// Add resources for all additional resources:
	{
		std::list<CL_ResourceManager>::iterator it;
		for (it = additional_resources.begin(); it != additional_resources.end(); ++it)
		{
			std::list<std::string> additionals = it->get_all_resources();
			result.merge(additionals);
		}
	}

	return result;
}

std::list<std::string> CL_ResourceManager_Generic::get_all_sections()
{
	std::list<std::string> result;

	// Add local sections:
	{
		CL_DomElement element = document.named_item("resources").to_element();
		add_sections(result, element);

		std::map<std::string, CL_Resource>::iterator it;
		for (it = resources.begin(); it != resources.end(); ++it)
		{
			result.push_back(it->first);
		}
	}

	// Add sections for all additional resources:
	{
		std::list<CL_ResourceManager>::iterator it;
		for (it = additional_resources.begin(); it != additional_resources.end(); ++it)
		{
			std::list<std::string> additionals = it->get_all_sections();
			result.merge(additionals);
		}
	}

	return result;
}

std::list<std::string> CL_ResourceManager_Generic::get_resources_of_type(const std::string &type_id)
{
	std::list<std::string> result;

	// Add local resources:
	{
		std::map<std::string, CL_Resource>::iterator it;
		for (it = resources.begin(); it != resources.end(); ++it)
		{
			if (it->second.get_type() == type_id) result.push_back(it->first);
		}
	}

	// Add resources for all additional resources:
	{
		std::list<CL_ResourceManager>::iterator it;
		for (it = additional_resources.begin(); it != additional_resources.end(); ++it)
		{
			std::list<std::string> additionals = it->get_resources_of_type(type_id);
			result.merge(additionals);
		}
	}

	return result;
}

std::list<std::string> CL_ResourceManager_Generic::get_resources_of_type(const std::string &type_id, const std::string &original_section_name)
{
	std::list<std::string> result;

	// Make sure we have a trailing / in section
	std::string section_name = add_trailing_slash(original_section_name);

	// Add local resources:
	{
		std::map<std::string, CL_Resource>::iterator it;
		for (it = resources.begin(); it != resources.end(); ++it)
		{
			if (it->first.substr(0, section_name.length()) == section_name)
			{
				if (it->second.get_type() == type_id) result.push_back(it->first);
			}
		}
	}

	// Add resources for all additional resources:
	{
		std::list<CL_ResourceManager>::iterator it;
		for (it = additional_resources.begin(); it != additional_resources.end(); ++it)
		{
			std::list<std::string> additionals = it->get_resources_of_type(type_id, section_name);
			result.merge(additionals);
		}
	}

	return result;
}

/////////////////////////////////////////////////////////////////////////////
// CL_ResourceManager_Generic Signals:

CL_Signal_v1<CL_Resource &> CL_ResourceManager_Generic::sig_resource_added;

/////////////////////////////////////////////////////////////////////////////
// CL_ResourceManager_Generic Operations:

void CL_ResourceManager_Generic::load(
	const std::string &config_file,
	CL_InputSourceProvider *input_provider,
	bool delete_inputsource_provider)
{
	if (delete_provider)
		delete provider;

	provider = input_provider;
	delete_provider = delete_inputsource_provider;

	if (provider == 0)
	{
		provider = new CL_InputSourceProvider_File(CL_String::get_path(config_file));
		delete_provider = true;
	}
	else
	{
		CL_InputSourceProvider *new_provider = provider->create_relative(CL_String::get_path(config_file));
		if (delete_provider) delete provider;
		provider = new_provider;
		delete_provider = true;
	}

	try
	{
		document.load(provider->open_source(CL_String::get_filename(config_file)), true);
	}
	catch( CL_Error err )
	{
		// a hack to report the filename of the malformed xml file
		err.message.insert(0, config_file + ": " );
		throw err;
	}

	CL_DomElement root = document.named_item("resources").to_element();
	if (root.is_null()) throw CL_Error(config_file + ": Not a valid XML resource file.");
	parse_section(root);

	std::map<std::string, CL_Resource>::iterator it;
	for (it = resources.begin(); it != resources.end(); ++it)
	{
		sig_resource_added(it->second);
	}
}

void CL_ResourceManager_Generic::add_resources(const CL_ResourceManager &resources)
{
	additional_resources.push_back(resources);
}

void CL_ResourceManager_Generic::remove_resources(const CL_ResourceManager &resources)
{
	additional_resources.remove(resources);
}

void CL_ResourceManager_Generic::load_all()
{
	load_section(std::string());
}

void CL_ResourceManager_Generic::unload_all()
{
	unload_section(std::string());
}

void CL_ResourceManager_Generic::load_section(const std::string &original_section_name)
{
	// Make sure we have a trailing / in section
	std::string section_name = add_trailing_slash(original_section_name);

	// Load local resources:
	{
		std::map<std::string, CL_Resource>::iterator it;
		for (it = resources.begin(); it != resources.end(); ++it)
		{
			if (it->first.substr(0, section_name.length()) == section_name)
			{
				it->second.load();
			}
		}
	}

	// Load additional resources:
	{
		std::list<CL_ResourceManager>::iterator it;
		for (it = additional_resources.begin(); it != additional_resources.end(); ++it)
		{
			CL_ResourceManager &additional_manager = *it;
			additional_manager.load_section(section_name);
		}
	}
}

void CL_ResourceManager_Generic::unload_section(const std::string &original_section_name)
{
	// Make sure we have a trailing / in section
	std::string section_name = add_trailing_slash(original_section_name);

	// Load local resources:
	{
		std::map<std::string, CL_Resource>::iterator it;
		for (it = resources.begin(); it != resources.end(); ++it)
		{
			if (it->first.substr(0, section_name.length()) == section_name)
			{
				it->second.unload();
			}
		}
	}

	// Load additional resources:
	{
		std::list<CL_ResourceManager>::iterator it;
		for (it = additional_resources.begin(); it != additional_resources.end(); ++it)
		{
			CL_ResourceManager &additional_manager = *it;
			additional_manager.unload_section(section_name);
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
// CL_ResourceManager_Generic Implementation:

void CL_ResourceManager_Generic::parse_section(CL_DomElement &sectionElement, const std::string &prefix)
{
	std::string path = prefix + sectionElement.get_attribute("name") + "/";
	if (sectionElement.get_tag_name() == "resources") path = prefix;

	for (CL_DomNode node = sectionElement.get_first_child(); !node.is_null(); node = node.get_next_sibling())
	{
		if (!node.is_element()) continue;

		CL_DomElement element = node.to_element();
		if (element.get_tag_name() == "section")
		{
			parse_section(element, path);
		}
		else
		{
			CL_Resource resource(CL_SharedPtr<CL_Resource_Generic>(new CL_Resource_Generic(element, self)));
			resources[path + element.get_attribute("name")] = resource;
		}
	}
}

void CL_ResourceManager_Generic::add_sections(std::list<std::string> &returnList, CL_DomElement &sectionElement, const std::string &prefix)
{
	std::string path = prefix + sectionElement.get_attribute("name") + "/";
	if (sectionElement.get_tag_name() == "resources") path = prefix;

	for (CL_DomNode node = sectionElement.get_first_child(); !node.is_null(); node = node.get_next_sibling())
	{
		if (!node.is_element()) continue;

		CL_DomElement element = node.to_element();
		if (element.get_tag_name() == "section")
		{
			returnList.push_back(path + element.get_attribute("name"));
			add_sections(returnList, element, path);
		}
	}
}

std::string CL_ResourceManager_Generic::add_trailing_slash(const std::string &string)
{
	if(string.length() && string.at(string.length() - 1) != '/')
		return string + '/';
	else
		return string;
}
