#ifndef COMMON_DOWNLOADMANAGERIMPL_H_
#define COMMON_DOWNLOADMANAGERIMPL_H_

#include "DownloadManager.h"
#include "../common/URLEngine/UEFactory.h"
#include <vector>
#include <map>
#include <Windows.h>

using namespace nsURLEngine;

class DownloadManagerImpl : 
	public DownloadManager,
	public nsURLEngine::IUETaskCallback,
	public ParamConstructor
{
	struct TaskData{
		IUETaskPtr spTask;
		std::string strFile;
		std::vector<unsigned char> vecData;
	};

	typedef boost::shared_ptr<TaskData> TaskDataPtr;
	typedef std::map<TaskID, TaskDataPtr> TaskDataMap;
public:
	~DownloadManagerImpl(void);
	static DownloadManagerImpl& getInstance(void)
	{
		static DownloadManagerImpl s_instance;
		return s_instance;
	}

	virtual TaskID CreateTask(TaskInitData* pInit);
	virtual bool StartTask(TaskID taskID);
	virtual bool StopTask(TaskID taskID);

	void init(HWND hWnd);
	void uninit(void);

	//任务进度回调
	virtual void OnTaskProgress(double fNow,double fTotal,UETaskID taskid);
	//任务事件回调
	virtual void OnTaskEvent(emTaskEvent event,emEngineErrCode errCode,UETaskID taskid);
	//任务数据回调
	virtual void OnTaskData(UEVoidPtr ptr,unsigned int nLen,UETaskID taskid);

	virtual void ParamAlloc(
		unsigned int nEvent,
		unsigned int nSubEvent,
		void** param1,
		void** param2
		);
	virtual void ParamFree(
		unsigned int nEvent,
		unsigned int nSubEvent,
		void* param1,
		void* param2
		);

protected:
	void* AllocCompleteBuffer(TaskID taskID);

private:
	TaskDataMap mmTasks;
	IUETaskManagerPtr mspTaskManager;
	HWND m_hParent;
	DownloadManagerImpl(void);
};

#define GetDownloadManager() DownloadManagerImpl::getInstance()

#endif //COMMON_DOWNLOADMANAGERIMPL_H_