/** @file asyncfileloader.h
 *	Definition for a class that loads a file asynchronously using an IFileHandler instance
 *	@author Nick Beatty (nbeatty@ea.com)
 *
 *	UX
 *	(c) 2011 Electronic Arts Inc.
 */

#ifndef EABASETYPESPLUS_ASYNCFILELOADER_H_
#define EABASETYPESPLUS_ASYNCFILELOADER_H_

#include "EABaseTypes/string.h"

#include "EABaseTypesPlus/asyncfilehandler.h"

namespace EA { namespace Thread { class Thread; }}
namespace EA { namespace Allocator { class ICoreAllocator; }}
namespace EA 
{ 
namespace Types 
{
	class IFileHandlerCallback;
	class IFileResource;
	
	/*! @class	AsyncFileLoader asyncfileloader.h	"ux_core/asyncfileloader.h"
	*	@brief	An async file loader that uses an IFleHandler instance to access the file system
	*/
	class AsyncFileLoader
	{
	public:

		/*!
		*	C'tor for AsyncFileLoader
		*	@param[in]	pAllocator		The allocator to use
		*	@param[in]	pFileHandler	The IFileHandler instance use. 
		*	@param[in]	pCallback		The IFileHandlerCallback instance registered for this file load
		*	@param[in]	pFileName		The path to file to load
		*/
		AsyncFileLoader(EA::Allocator::ICoreAllocator* pAllocator
								, EA::Types::AsyncFileHandler *pFileHandler
								, EA::Types::IFileHandlerCallbackIn pCallback
								, const char* pFileName);

		~AsyncFileLoader();

		/*!
		*	Starts the thread to perform the file load
		*/
		void StartLoad();

		/*!
		*	Get the IFileHandlerCallback registered to this load instance
		*	@return		IFileHandlerCallback	The callback instance
		*/
		EA::Types::IFileHandlerCallbackReturn		GetCallback() { return mCallback; }

		/*!
		*	Get the IFileResource instance that was loaded
		*	@return		IFileResource	The loaded file resource
		*
		*	@attention	This will be NULL until the load is finished 
		*/
		EA::Types::IFileResourceReturn				GetResource() { return mResource; }

		/*!
		*	Get the Allocator instance registered to this load
		*	@return		EA::Allocator::ICoreAllocator	The alloactor instance
		*/
		EA::Allocator::ICoreAllocator*	GetAllocator() { return mAllocator; }

	private:
		void EndThread();

		//thread function
		static intptr_t Run(void* pContext);
		
		EA::Allocator::ICoreAllocator*			mAllocator;
		EA::Types::AsyncFileHandler*			mFileHandler;
		EA::Types::IFileHandlerCallbackRef		mCallback;
		EA::Types::IFileResourceRef				mResource;
		EA::RawString							mFileName;
		EA::Thread::Thread*						mThread;
	};
}
}
#endif //EABASETYPESPLUS_ASYNCFILELOADER_H_
