#ifndef MATERIAL_MANAGER_H
#define MATERIAL_MANAGER_H

#include <map>
#include <string>

#include "Material.h"

using namespace std;



/**
*	Spravce materialu
*/
///Manage materials - materials can be inserted, removed and queryed
template< typename MATERIAL>
class MaterialManager
{
private:
	map<string,MATERIAL* > materials;
//	MaterialManager( const Material_manager<MATERIAL>&);	//nelze kopirovat;

public:
	~MaterialManager(void);
	MaterialManager(void);

	void insertMaterial(const string& name, const MATERIAL& material);	/**< Insert copy of given material into database  */
	void insertMaterialPtr( const string& name, MATERIAL* material);	/**< Store existing material into database. MaterialManager is responsible for dealocation. */
	void removeMaterial(const string& name);							/**<  Remove material from database. The material is deleted ( delete material is called ). */
	MATERIAL* getMaterial(const string &name) const;					/**<  Returns material from database */
};


template< typename MATERIAL> MaterialManager<MATERIAL>::~MaterialManager(void)
{
	//smazu dynamicky alokovane polozky
	for (map<string,MATERIAL* >::iterator it=this->materials.begin(); it!=this->materials.end(); ++it)
	{
		delete it->second;
	}
}

template< typename MATERIAL> MaterialManager<MATERIAL>::MaterialManager(void)
{
}

template< typename MATERIAL> void MaterialManager<MATERIAL>::insertMaterial(const string& name, const MATERIAL& material)
{
	MATERIAL* new_material=new MATERIAL;
	*new_material=material;

	this->materials[name]=new_material;
}


template< typename MATERIAL> void MaterialManager<MATERIAL>::removeMaterial(const string& name)
{
	MATERIAL* material = this->getMaterial( name );

	if ( material != NULL )
	{
		delete material;
	}

	this->materials.erase(name);
}

template< typename MATERIAL> MATERIAL* MaterialManager<MATERIAL>::getMaterial(const string &name) const
{
	map<string,MATERIAL* >::const_iterator it=this->materials.find(name);

	if (it==this->materials.end())
	{
		return NULL;
	}
	else
	{
		return it->second;
	}
}

#endif