/*
	Lan Wars.
	Copyright (c) 2009, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef RESOURCE_H
#define RESOURCE_H

//=====================================================================================================================
//=====================================================================================================================

namespace Game
{
	//=================================================================================================================	
	//=================================================================================================================
	
	template <class ResourceType> class ResourceManager;

	//=================================================================================================================
	/// @brief		Base class for all resources. Resources are immutable and cannot have their names changed once 
	///				loaded, however it is possible for them to be temporarily purged for whatever reason and reloaded 
	///				later if required.	
	//=================================================================================================================

	template <class ResourceType> class Resource
	{
		public:

			//=========================================================================================================
			// Functions
			//=========================================================================================================
	        
				//=====================================================================================================
				///	@brief		Creates the resource and loads the resource data from the given file.
				/// @param		name - name of the resource file to load
				///	@param		manager_user_data - user data that the resource manager has. for a texture manager this might be the 
				///				graphics device object etc.
				/// @param		manager - manager the resource belongs to
				/// @pre		The resource must have a valid manager
				//=====================================================================================================
				
				Resource( std::string & name , long manager_user_data , ResourceManager<ResourceType> * manager ) 
				: 
				m_name(name) , m_manager_user_data(manager_user_data) , m_manager(manager)
				{
					// Make sure this is ok in debug
					
					ENSURE( m_manager != 0 , "The resource must have a valid manager" )
				
					// Resource is not valid initially
					
					m_is_valid = false;
					
					// Clear our own user data:
					
					m_user_data = 0;
				
					// Attempt to load the resource data
					
					LoadData();
				}
				
				//=====================================================================================================
				///	@brief	Destructor. Destroys the resource.
				//=====================================================================================================
				
				~Resource(){ FreeData(); }
				
				//=====================================================================================================
				///	@brief		Returns the actual data of the resource.
				/// @returns	Actual resource data.
				//=====================================================================================================
				
				inline ResourceType GetData() const { return m_data; }
				
				//=====================================================================================================
				///	@brief		Returns the user data for the resource.
				/// @returns	Resource user data.
				//=====================================================================================================
				
				inline long GetUserData() const { return m_user_data; }
				
				//=====================================================================================================
				///	@brief		Returns the user data for the resource's manager.
				/// @returns	Resource manager user data.
				//=====================================================================================================
				
				inline long GetManagerUserData() const { return m_manager_user_data; }			
				
				//=====================================================================================================
				///	@brief		Returns the file name of the resource.
				/// @returns	Resource file name.
				//=====================================================================================================
				
				inline const std::string & GetName() const { return m_name; }
				
				//=====================================================================================================
				///	@brief		Tells if this resource is valid
				/// @returns	True if this resource is valid
				//=====================================================================================================			
				
				inline bool IsValid(){ return m_is_valid; }		

		protected:
	    
			//=========================================================================================================
			// Functions
			//=========================================================================================================    
	        
				/// Loads the resources data
				
				void LoadData();
				
				/// Frees the resource's data
				
				void FreeData();
	    
			//=========================================================================================================
			// Variables
			//=========================================================================================================

				/// Name of the resource

				std::string m_name;
				
				/// Set to true if the resource is valid
				
				bool m_is_valid;
				
				/// The actual resource data itself
				
				ResourceType m_data;
				
				/// User data specific to each type of resource. Inherited from parent texture manager data.
				
				long m_manager_user_data;
				
				/// User data for this particular resource.
				
				long m_user_data;
				
				/// Manager of this particular resource
				
				ResourceManager<ResourceType> * m_manager;
				
		private:
				
			//=========================================================================================================
			// Functions
			//=========================================================================================================
	        
				/// Resources exist only once and cannot be copied
				
				Resource( Resource<ResourceType> & other );
				
				/// Resources exist only once and cannot be copied
				
				Resource<ResourceType> operator = ( Resource<ResourceType> & other );			
	};	

};

#endif
