#include "stdafx.h"
#include "CompoundResourceMap.h"
#include "BookInfo.h"
#include "Function.h"
#include <string>
CCompoundResourceMap::CCompoundResourceMap(void)
{
	
	
	for(int n=0;n<3;n++)
	{
		vector<COMPOUNDRES*> vc_temp;
		int nRand=0;

		for(int nn=0;nn<2;nn++)
		{
			COMPOUNDRES* aa =new COMPOUNDRES;
			nRand =nn+n;
			CString strnum=L"";
			strnum.Format(L"RES10%d",nRand);

			aa->strResID =strnum;
			if(nn==0)
			{
				aa->nStatus =CompoundRes_State::CompoundRES_Download_OK;
			}else
			{
				aa->nStatus =CompoundRes_State::CompoundRES_Download_None;
				aa->llDownloadedSize =300;
				aa->llFileSize = 3000;
			}
			vc_temp.push_back(aa);
		}
		
		CString strBookId=L"";

		if(n==0)
		{
			strBookId.Format(L"UUid99%d",n);
		}else
		{
			strBookId.Format(L"UUid00%d",n);
		}
		
		AddResData(strBookId,vc_temp);
	
	}

	COMPOUNDRES* bb = new COMPOUNDRES;
	GetFirstNeedDownLoadData(*bb);
	if(bb->strResID !="")
	{
		AfxMessageBox(bb->strResID);
	}
	DeleteResData(L"RES003");
	

}


CCompoundResourceMap::~CCompoundResourceMap(void)
{
}

BOOL CCompoundResourceMap::AddResData(CString strKey,vector<COMPOUNDRES*> v_pCompoundRes)
{
	//�ж��Ƿ���KEY ������Erase ��������
	//for(int nSize=0;nSize<v_pCompoundRes.size();nSize++)
	//{
	//	if(strKey == )
	//}student.insert(map<int,string>::value_type(1,"liMing"));
	CFunction fun;
	std::pair<std::unordered_map<std::string,vector<COMPOUNDRES*>>::iterator,bool>ret;
	ret =m_map_CompoundResData.insert(std::unordered_map<std::string,vector<COMPOUNDRES*>>::value_type(fun.UnicodeToAscii(strKey),v_pCompoundRes));
	
	//ret =m_map_CompoundResData.insert(pair<CString,vector<COMPOUNDRES*>>(strKey,v_pCompoundRes));
	return ret.second;
}

void CCompoundResourceMap::ClearAllData()
{
	
	BOOL bRet=FALSE;

	if(!m_map_CompoundResData.empty())
	{
		//FirstRes =m_map_CompoundResData.at(0);
		std::unordered_map<std::string,std::vector<COMPOUNDRES*>>::iterator it;
		for (it = m_map_CompoundResData.begin(); it != m_map_CompoundResData.end(); it++)
		{

			std::string strBookId = it->first;
			std::vector<COMPOUNDRES*> vc_AllResTemp =it->second;
			for(int nSize=0;nSize<vc_AllResTemp.size();nSize++)
			{
				COMPOUNDRES* p_CompoundResData= vc_AllResTemp.at(nSize);
				if(p_CompoundResData)
				{
					delete p_CompoundResData;
					p_CompoundResData=NULL;
				}
			}
			vc_AllResTemp.clear();
		}
		m_map_CompoundResData.clear();
	}
	
}

int CCompoundResourceMap::GetSizeOfMap()
{
	return m_map_CompoundResData.size();
}

BOOL CCompoundResourceMap::GetFirstNeedDownLoadData(COMPOUNDRES& FirstRes)
{
	BOOL bRet=FALSE;
	//�ҵ���һ�� �����ص���Դ
	if(!m_map_CompoundResData.empty())
	{
		//FirstRes =m_map_CompoundResData.at(0);
		std::unordered_map<std::string,std::vector<COMPOUNDRES*>>::iterator it;
		for (it = m_map_CompoundResData.begin(); it != m_map_CompoundResData.end(); it++) {

			std::string strBookId = it->first;
			std::vector<COMPOUNDRES*> vc_AllResTemp =it->second;
			for(int nSize=0;nSize<vc_AllResTemp.size();nSize++)
			{
				COMPOUNDRES* p_CompoundResData= vc_AllResTemp.at(nSize);
				//�ж�����״̬
				if((p_CompoundResData->nStatus!=CompoundRes_State::CompoundRES_Download_OK)&&(p_CompoundResData->llDownloadedSize<p_CompoundResData->llFileSize))
				{
					//��Ҫ���ص���Դ
					FirstRes = *p_CompoundResData;
					return TRUE;
				}
			}
		}

		bRet =TRUE;
	}
	return bRet;
}

BOOL CCompoundResourceMap::FindResData(CString strResID,COMPOUNDRES& FindRes)
{
	//������Map ���� 
	BOOL bRet=FALSE;
	//�ҵ����ϵ���Դ
	if(!m_map_CompoundResData.empty())
	{
		//FirstRes =m_map_CompoundResData.at(0);
		std::unordered_map<std::string,std::vector<COMPOUNDRES*>>::iterator it;
		for (it = m_map_CompoundResData.begin(); it != m_map_CompoundResData.end(); it++)
		{

			std::string strBookId = it->first;
			std::vector<COMPOUNDRES*> vc_AllResTemp =it->second;
			for(int nSize=0;nSize<vc_AllResTemp.size();nSize++)
			{
				COMPOUNDRES* p_CompoundResData= vc_AllResTemp.at(nSize);
				
				if((p_CompoundResData->strResID==strResID))
				{
					//���ҵ���Դ
					FindRes = *p_CompoundResData;
					return TRUE;
				}
			}
		}
	}
	return bRet;
	//���ظı�����
}

void CCompoundResourceMap::DeleteResData(CString strResID)
{
	//������Map ���� 
	BOOL bRet=FALSE;
	//�ҵ����ϵ���Դ
	if(!m_map_CompoundResData.empty())
	{
		//FirstRes =m_map_CompoundResData.at(0);
		std::unordered_map<std::string,std::vector<COMPOUNDRES*>>::iterator it;
		for (it = m_map_CompoundResData.begin(); it != m_map_CompoundResData.end(); it++)
		{

			std::string strBookId = it->first;
			std::vector<COMPOUNDRES*> vc_AllResTemp =it->second;
			std::vector<COMPOUNDRES*>::iterator it2 =vc_AllResTemp.begin();
			for(it2 = vc_AllResTemp.begin(); it2 != vc_AllResTemp.end(); it2++)
			{
				COMPOUNDRES* p_CompoundResData= *it2;
				if((p_CompoundResData->strResID==strResID))
				{
					//��Ҫɾ������Դ
					if(p_CompoundResData)
					{
						delete p_CompoundResData;
						p_CompoundResData=NULL;
					}
					vc_AllResTemp.erase(it2++);

					return;
				}

			}
		}
	}
}